上传的图片没有设置为公开的谷歌云存储-谷歌应用引擎


Uploaded image does not set to public on Google Cloud Storage - Google App Engine

我遇到了一个问题。我确实遵循了谷歌应用引擎的两个答案——上传的文件在谷歌云存储上不是公开的,但仍然没有运气。当图像上传到bucket

时,它没有设置为public

这是我的代码:

// 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! ");

我做错了什么?

最简单的方法是更改该bucket上的默认对象ACL:

  1. 进入https://console.developers.google.com/project/_/storage/browser
  2. 点击桶名右边的按钮,看起来像3个垂直点
  3. 单击编辑对象默认权限
  4. 将allUsers添加为具有权限的User

我在一个python项目中使用它,像这样(它可能在php中类似):

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