Tracking Google Cloud Storage Upload progress with PHP Library

807 views
Skip to first unread message

Paulo Henrique

unread,
Jul 5, 2017, 1:48:40 PM7/5/17
to Google App Engine

I'm using Gcloud PHP library for uploading files to storage buckets and it works fine. But I want to track progress from this upload process, trying to use the uploadProgressCallback option provided and documented here: https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.33.0/storage/bucket?method=getResumableUploader

But it's not working, the callable isn't fired at all. I'm new to using callable functions, but I think it's pretty simple. Here's my code below.

function uploadObject($bucketName, $objectName, $source)
{
    $storage = new StorageClient();
    $bytes = 0;
    $bucket = $storage->bucket($bucketName);
    $uploader = $bucket->getResumableUploader (
        $file = fopen($source, 'r'),
        [
            'name' => $objectName,
            'resumable' => true,
            'chunkSize' => 524288,
            'uploadProgressCallback' => array($this, 'uploadProgress')
        ]
    );  


    try {
        $object = $uploader->upload();

    } catch ( GoogleException $ex ) {
        $resumeUri = $uploader->getResumeUri();
        $object = $uploader->resume($resumeUri);
    }

}

function uploadProgress($bytes) {

    $_SESSION['uploadProgress'] = $bytes;    
}

Any help would be greatly appreciated...

Yannick (Cloud Platform Support)

unread,
Jul 5, 2017, 3:41:58 PM7/5/17
to Google App Engine
Hello Paulo, there are several different ways to define a callable detailed in the PHP documentation. The one you used allowing you to direct a callback to a method of an instantiated object, is that the case in your situation?

If it's not and this script isn't part of an object at all you might want to define your callback as a simple string:

            'uploadProgressCallback' => 'uploadProgress'

Nizar Bens

unread,
Sep 2, 2017, 11:56:20 AM9/2/17
to Google App Engine
Hey,
How can implement the uploadProgressCallback ?
Can you give me examples using it with Ajax requests?
Thanks,
Reply all
Reply to author
Forward
0 new messages