google-cloud-storage - 谷歌存储 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 中;它工作正常,文件公开可用。
我努力了:
- 上传代码后检查存储桶中是否存在 blob 文件。
- 上传后引入 5 秒延迟。
任何帮助表示赞赏。
解决方案
如果要将上传的文件更改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"]))
输出:
推荐阅读
- node.js - 构建 TypeScript monorepo 项目的问题
- python - 无法使用firefox驱动打开网站
- azure - hdfs dfs -setfacl 在 Azure Data Lake Store 上失败
- navigation - PugsJs 为 mixin 添加了额外的类
- javascript - 从字符串中替换所有出现的 \n\r
- oracle - 如何将 R 表导出到 Oracle 数据库
- java - 对于 api 低于 26 的设备,base 64 编码是否有替代方案?
- vb.net - 我如何将数据绑定到新的绑定
- excel - 如何使用 Excel 中的 DGET 函数从数据透视表中提取数据?
- r - 如何在 R 中的 tableGrob() 下对齐 ggparagraph() 文本