如何让用户从谷歌应用引擎直接上传到谷歌数据存储?

时间:2016-08-03 21:16:05

标签: php google-app-engine google-cloud-storage codeigniter-3

我无法让用户将文件上传到我在谷歌应用引擎上运行的php程序。我正在尝试关注this google manual

我正在使用codeIgniter 3,我设置了以下控制器:

Projecteditor.php:

public function index( $cluster_id )
{
    if( $this->require_min_level( 1 ) )
    {
        $project_access_lvl = $this->check_access_lvl( $cluster_id );
        if( $project_access_lvl >= 0 )
        {
            // Upload files to Google Cloud Storage
            $my_bucket = 'THE_ID.appspot.com';
            $options = ['gs_bucket_name' => $my_bucket]; //todo app id
            $upload_url = CloudStorageTools::createUploadUrl('/main.php/upload/handler', $options);
            $view_data['upload_url'] = $upload_url;

            // Display webpage
            $cluster = $this->cluster_model->get_cluster_by_id( $cluster_id )[0];
            $project = $this->project_model->get_projects( $cluster['projecten_project_id'])[0];

            $view_data['fase_details'] = $this->get_fase_details( $cluster['fase'] );
            $view_data['project_name'] = $project['naam'];

            echo $this->load->view('templates/header', '', TRUE);
            echo $this->load->view('projecteditor/index', ( isset( $view_data ) ) ? $view_data : '', TRUE );
            echo $this->load->view('templates/footer', '', TRUE);
        }
        else
        {
            // TODO syslog
        }
    }
}

upload.php的:

public function handler()
{
    $my_bucket = 'THE_ID.appspot.com';
    # [START move_uploaded_file]
    $file_name = $_FILES['uploaded_files']['name'];
    $temp_name = $_FILES['uploaded_files']['tmp_name'];
    move_uploaded_file($temp_name, "gs://${my_bucket}/${file_name}.txt");
    # [END move_uploaded_file]
    return sprintf('Your file "%s" has been uploaded.', $file_name);
}

当然我用正确的存储桶ID替换了THE_ID。

视图内与Projecteditor控制器对应的表单:

<?php
    echo form_open($upload_url, array('enctype' => 'multipart/form-data'));
?>
<div class="col-lg-12">
     <div id="my-dropzone" class="dropzone" action="#">
         <div class="dropzone-previews"></div>
             <button type="submit" class="btn btn-success pull-right"><i class="fa fa-upload"></i>&nbsp;&nbsp;<span class="bold">Upload</span></button>              
         </div>
     </div></div>
<?php echo form_close(); ?>

使用jQuery dropbox插件上传文件。 我怀疑我在CloudStorageTools :: createUploadUrl中输入了错误的相对URL,但我无法找到我应该输入的相对URL。

两个控制器的绝对URL是:

Projecteditor.php: https://example.com/projecteditor/index/SOME_VARIABLE

upload.php的: https://example.com/main.php/upload/handler

当我的代码执行时,我会向Projecteditor.php发送帖子,但不会向Upload.php发送帖子,数据存储区也会保持空白。

我还使用了关于Google存储空间的this source

如果我做错了或有其他方法(更简单的方法)存储我的数据,请告诉我。

提前致谢

1 个答案:

答案 0 :(得分:0)

显然,jQuery dropbox插件中的配置错误导致文件无法上传,在PHP方面一切正常。