【问题标题】:Google Cloud Storage | PHP | Uploaded files are at 0 Bytes谷歌云存储 | PHP |上传的文件为 0 字节
【发布时间】:2019-04-02 13:58:00
【问题描述】:

我正在使用 Codeigniter 应用程序并尝试为用户提供上传文件的访问权限。服务器设置在运行 CentOS 7 和 Apache 2 的 Google Compute Engine VM 上,我正在尝试使用 Google Cloud Storage 进行用户上传。目前,当我上传文件时,只有文件名被上传到 GCS Bucket,文件大小为 0 字节。

我的前端代码:

<body>
    <form action="/includes/includes_gc/do_upload" class="dropzone" id="pfUploads" enctype="multipart/form-data">
    </form>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script>
    <script>
        Dropzone.options.pfUploads = {
            paramName: "file",
            uploadMultiple: true,
            init: function () {
                this.on('complete', function (data) {
                    console.log(data);
                });
            }
        };
    </script>
</body>

控制器功能:

public function do_upload() {

    if ($_FILES) {
        $filename = $_FILES['file']['name'];
        $gs_name = file_get_contents($_FILES['file']['tmp_name']);
        $bucketName = 'xxxxxx.appspot.com';
        $kdir = dirname(getcwd(), 2);
        $storage = new StorageClient([
            'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
            'projectId' => 'xxxxxx'
        ]);
        $file = fopen($gs_name, 'r');
        $bucket = $storage->bucket($bucketName);
        $bucket->upload($file, [
            'name' => $filename
        ]);

        $test = array();
        array_push($test, basename($gs_name));
        array_push($test, $bucketName);
        array_push($test, $filename);
        echo json_encode($test);
    }
}

对于解决此问题的任何帮助,我将不胜感激。

提前致谢。 纳文

【问题讨论】:

    标签: codeigniter google-app-engine upload google-cloud-storage


    【解决方案1】:

    您将函数file_get_contents 的返回值分配给变量gs_name,并将该变量传递给fopen 函数。

    如果不存在与该文件内容相同的路径名,这通常是预期的,fopenreturns 值为 false。

    因此,您将值 false 作为第一个参数传递给 $bucket->upload,这有效地创建了一个具有您在相应存储桶上提供的名称的 0 字节的 blob。

    所以底线,以下应该工作:

        public function do_upload() {
    
            if ($_FILES) {
                $filename = $_FILES['file']['name'];
                $gs_name = $_FILES['file']['tmp_name'];
                $file = file_get_contents($gs_name);       
                $bucketName = 'xxxxxx.appspot.com';
                $kdir = dirname(getcwd(), 2);
                $storage = new StorageClient([
                    'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
                    'projectId' => 'xxxxxx'
                ]);
                $bucket = $storage->bucket($bucketName);
                $bucket->upload($file, [
                    'name' => $filename
                ]);
            }
        }
    

    另外请记住,您可以使用 upload 方法的返回值,例如:

    $storage_object = $bucket->upload($file, ['name' => $filename]);
    

    进一步processing

    【讨论】:

    • 非常感谢您的回复 fhenriques....... 我尝试了您提到的所有方法,但不幸的是结果仍然相同。 o 上传字节数。我不确定我是否做错了什么。
    【解决方案2】:

    @fhenriques...再次感谢您的帮助和指导。我确实玩了一下,做了一些改变。

        if (!empty($_FILES)) {
            foreach ($_FILES['file']['tmp_name'] as $key => $tmp_name) {
                $filename = $_FILES['file']['name'][$key];
                $gs_name = $_FILES['file']['tmp_name'][$key];
                $file = file_get_contents($gs_name);
                $bucketName = 'xxxxxx.appspot.com';
                $kdir = dirname(getcwd(), 2);
                $storage = new StorageClient([
                    'keyFile' => json_decode(file_get_contents($kdir . DIRECTORY_SEPARATOR . 'xxxxxx.json'), true),
                    'projectId' => 'xxxxxx'
                ]);
                $bucket = $storage->bucket($bucketName);
                $bucket->upload($file, [
                    'name' => $filename
                ]);
                $storage_object = $bucket->upload($file, ['name' => $filename]);
                return $storage_object;
            }
        }
    

    现在这有助于我上传文件。非常感谢您的指导。我感谢您的帮助。

    【讨论】:

    • 很高兴听到这个消息,请记住您拨打了两次$bucket-&gt;upload,您可以删除其中一个电话。
    猜你喜欢
    • 2020-09-22
    • 2016-12-30
    • 1970-01-01
    • 2018-10-10
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多