upload image file to google cloud storage

2019-05-30 17:38发布

问题:

I was trying to upload an image file to google cloud storage, following the google cloud storage documentation here, but without success.

The bucket that I created in my google cloud storage is named: test_app-111

This is my app.yaml file:

application: test_app-111  
version: 1
runtime: php
api_version: 1

handlers:

# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$
  application_readable: true

- url: /img.php
  script: img.php 

This is my php file called img.php :

<?php

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

$options = [ 'gs_bucket_name' => 'https://storage.googleapis.com/test_app-111.appspot.com' ];
$upload_url = CloudStorageTools::createUploadUrl('/upload_handler.php', $options);

?>


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

Any idea what am I missing? In the documentation it says "Create the application specific upload URL, using the method CloudStorageTools::createUploadUrl()". Do I have to create myself an upload URL? Anyone got an idea how to move on?

回答1:

gs_bucket_name should be set to your bucket name, not the full url, i.e.

$options = [ 'gs_bucket_name' => 'test_app-111.appspot.com' ];

You can also use CloudStorageTools::getDefaultGoogleStorageBucketName() to retrieve the default bucket name instead of hard-coding in one.