Google Storage Python ACL Udate 不起作用

问题描述

我已将一张图片文件上传到我的 Google 存储桶。

  #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"]))

输出:

enter image description here