Closed
Description
Page Name: storage-acl
Release: 0.8.0
Please add an explanation and code snippet to illustrate how to set the ACL for an object. The referenced page only talks about setting bucket-level permissions, and it isn't easy to find whether a blob object also has the acl
property, and whether it works the same way as it does with buckets. I would suggest a code snippet such as the one below, which uploads a new blob and sets its ACL to "public read". It's also important to note that the blob needs to be uploaded before its ACL can be set (i.e. if I move the upload_from_string()
command below acl.save()
, the latter will not have any effect, nor will it fail with any errors).
def put_object(bucket_name, object_name, object, public_read = True):
client = storage.Client(PROJECT)
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(object_name)
blob.upload_from_string(object,content_type = 'image/jpeg', client = client)
# set ACL to public-read so the image can be accessed by any user
if (public_read):
acl = blob.acl
acl.all().grant_read()
acl.save()