首页 > 解决方案 > 谷歌存储 Python ACL 更新不起作用

问题描述

我已将一个图像文件上传到我的谷歌存储桶。

  #Block 1
  #Storing the local file inside the bucket
  blob_response = bucket.blob(cloud_path) 
  blob_response.upload_from_filename(local_path, content_type='image/png')  

文件上传正常。我验证存储桶中的文件。上传文件后,以相同的方法,我正在尝试更新文件的 acl 以公开访问:

  #Block 2
  blob_file = storage.Blob(bucket=bucket20, name=path_in_bucket)
  acl = blob_file.acl
  acl.all().grant_read()
  acl.save()

这不会使文件公开。

奇怪的是,在我运行上面的上传方法之后,如果我只是调用#Block 2 代码。分别在 jupyter notebook 中;它工作正常,文件公开可用。

我努力了:

任何帮助表示赞赏。

标签: google-cloud-storagegoogle-cloud-python

解决方案


如果要将上传的文件更改upload_from_filename()为公开文件,则可以重用上传的 blob。此外,在更改权限之前添加重新加载 acl。这一切都是使用 GCP AI 平台在 Jupyter Notebook 中的 1 个块中完成的。

# Block 1
bucket_name = "your-bucket"
destination_blob_name = "test.txt"
source_file_name = "/home/jupyter/test.txt"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)

print(blob) #prints the bucket, file uploded
blob.acl.reload() # reload the ACL of the blob
acl = blob.acl
acl.all().grant_read()
acl.save()

for entry in acl:
        print("{}: {}".format(entry["role"], entry["entity"]))

输出:

在此处输入图像描述


推荐阅读