【问题标题】:Google Storage Python ACL Update not Working谷歌存储 Python ACL 更新不起作用
【发布时间】:2021-03-18 18:06:48
【问题描述】:

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

  #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 秒。

感谢任何帮助。

【问题讨论】:

    标签: google-cloud-storage google-cloud-python


    【解决方案1】:

    如果您要将上传的文件从 upload_from_filename() 更改为公开,则可以重复使用上传的 blob。此外,在更改权限之前添加重新加载 acl。这一切都是在 Jupyter Notebook 中使用 GCP AI 平台在 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"]))
    

    输出:

    【讨论】:

    • 非常感谢,它解决了我的问题。我能够重用 blob 对象来更新 acl 权限。谢谢!
    猜你喜欢
    • 2017-06-12
    • 2013-04-22
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2022-11-10
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多