4
votes

I'm facing a problem here. I did follow both the answers from Google app-engine - Uploaded files not public on google cloud storage, but still no luck. The image didn't set to public whenever it is uploaded into the bucket

This is my code:

// Google API to create an absolute URL that can be used by a user to asynchronously upload a large blob
require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

// The name of cloud storage bucket
$options = [ 'gs_bucket_name' => 'example_images' ];
$upload_url = CloudStorageTools::createUploadUrl('/test.php', $options);

$fileName = 'gs://example_images/'.$_FILES['file']['name'];
echo $fileName."\n";

$options = array('gs'=>array('acl'=>'public-read','Content-Type' => $_FILES['file']['type']));
$ctx = stream_context_create($options);

if (false == rename($_FILES['file']['tmp_name'], $fileName, $ctx)) {
    die('Could not rename.');
}

$object_public_url = CloudStorageTools::getPublicUrl($fileName, false);

print("Successfull! ");

What did I do wrong? Please help me

1

1 Answers

4
votes

The easiest way is to change the default object ACL on that bucket:

  1. Go to https://console.developers.google.com/project/_/storage/browser
  2. click on the button on the right of the bucket name that looks like 3 vertical points
  3. click Edit object default permissions
  4. add allUsers as User with permission READ

I used it in a python project like this (it might be similar in php):

    "acl": [{"entity": "allUsers", "role": "READER"}],
    "contentType": "application/pdf",
    "contentEncoding": "UTF8"