Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Allowing Users to Upload Files

When you upload directly to GCS, you make an HTTP POST to a specific URL, which we'll describe in a moment. App Engine then uses a specific upload service to handle the post and write the file to GCS. When the file write is complete, App Engine notifies your app that the upload is complete. Because your app is invoked only upon completion, you can use this method to upload very large files, up to the current maximum of 100 Terabytes.

Moreover, user upload of files directly to GCS is faster and more cost-effective than writing to GCS from your App Engine app, because this consumes instance hours and incurs cost.

Note that in the user upload, the file write does not occur within a request to the application. Therefore it is exempt from the 60 second limit that would otherwise apply and allows uploads of very large files.

Implementing file uploads

To implement user file upload:

  1. Create the application specific upload URL, using the method CloudStorageTools::createUploadUrl() as follows:

    require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
    use google\appengine\api\cloud_storage\CloudStorageTools;
    
    $options = [ 'gs_bucket_name' => 'my_bucket' ];
    $upload_url = CloudStorageTools::createUploadUrl('/upload_handler.php', $options);
    

    See createUploadUrl options for details about available options.

  2. Note that you must start uploading to this URL within 10 minutes of its creation. Also, you cannot change the URL in any way - it is signed and the signature is checked before your upload begins.

  3. Use this URL as the action for the form you use to accept uploads, for example:

    <form action="<?php echo $upload_url?>" enctype="multipart/form-data" method="post">
        Files to upload: <br>
       <input type="file" name="uploaded_files" size="40">
       <input type="submit" value="Send">
    </form>
    

After the file(s) upload, a POST is made to the path specified as the first parameter to createUploadUrl ; in the example above, this is /upload_handler.php . The PHP runtime forms the correct $_FILES super global, and tmp_filename refers to the filename of the newly uploaded file in GCS.

For example, suppose the content of upload_handler.php is the following:

<?php
var_dump($_FILES);
?>

Uploading a file called hello.txt might result in the following output:

array(1) {
  ['uploaded_files']=>
  array(5) {
    ['name']=>    string(14) 'hello.txt'
    ['type']=>    string(10) 'text/plain'
    ['tmp_name']=>    string(73) 'gs://my_bucket/L2FwcHMtdXBsb2FkL2Jsb2JzL2IxNUFBVGNJNXNTd0VqR0tFSUtDRGxadGc'
    ['error']=>    int(0)
    ['size']=>    int(1452)
  }
}

After the upload is complete, you can read the uploaded file using the gs:// stream wrapper. You use move_uploaded_file like you normally would for any other uploaded file, for example:

$gs_name = $_FILES['uploaded_files']['tmp_name'];
move_uploaded_file($gs_name, 'gs://my_bucket/new_file.txt');

You can also then rename the file, for example:

// assumes the same $ctx object as shown earlier
if (false == rename('gs://my_bucket/oldname.txt', 'gs://my_bucket/newname.txt', $ctx)) {
  die('Could not rename.');
}

createUploadUrl options

Valid createUploadUrl options are shown in the following table:

Option Description
max_bytes_per_blob Integer. Default value: unlimited . The value of the largest size allowed for an uploaded blob.
max_bytes_total Integer. Default value: unlimited . The total size of all uploaded blobs.
gs_bucket_name String. The name of a Google Cloud Storage bucket that the blobs should be uploaded to. If you don't specify a value, the blob is uploaded to the application's default bucket.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.