Google Cloud Storage | PHP | Uploaded files are at 0 Bytes

只愿长相守 提交于 2021-01-29 07:31:43

问题


I am using a Codeigniter application and trying to provide access for users to upload files. the server is setup on a Google Compute Engine VM running CentOS 7 and Apache 2, I am trying to use Google Cloud Storage for user upload. Currently when I upload files only the file names are being uploaded into GCS Bucket with the file size as 0 bytes.

My Frontend Code:

<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>

The Controller Function:

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);
    }
}

I would appreciate any help to get this resolved.

Thanks in Advance. Naveen


回答1:


You’re assigning to the variable gs_name the return value of the function file_get_contents and you're passing that variable to the fopen function.

If there isn’t a path name which is the same as the contents of such file, which is normally expected, fopen returns the value false.

So you are passing the value false as a first parameter to $bucket->upload which effectively creates a blob of 0 bytes with the name you provided on the respective bucket.

So bottom line, the following should work:

    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
            ]);
        }
    }

Also keep in mind that you can use the return value of the upload method like:

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

To further processing.




回答2:


@fhenriques... again i would like to thank you for your help and guidance. I did play around a bit and made some changes.

    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;
        }
    }

Now this helped me in getting the uploads working. thank you so much for your guidance. I appreciate your help.



来源:https://stackoverflow.com/questions/55448688/google-cloud-storage-php-uploaded-files-are-at-0-bytes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!