如何解决Google Storage Python ACL Udate 不起作用
#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 中;它运行良好,文件公开可用。
我试过了:
感谢任何帮助。
解决方法
如果您要将上传的文件从 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"]))
输出:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。