sandeepsign
sandeepsign

Reputation: 616

Google Storage Python ACL Update not Working

I have uploaded one image file to my google storage bucket.

  #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')  

File gets uploaded fine. I verify the file in bucket. After uploading the file, in the same method, I am trying to update the acl for file to be publicly accessible as:

  #Block 2
  blob_file = storage.Blob(bucket=bucket20, name=path_in_bucket)
  acl = blob_file.acl
  acl.all().grant_read()
  acl.save()

This does not make the file public.

Strange thing is that,after I run the above upload method, if I just call the #Block 2 code. separately in jupyter notebook; It is working fine and file become publicly available.

I have tried:

Any help is appreciated.

Upvotes: 0

Views: 752

Answers (1)

Ricco D
Ricco D

Reputation: 7287

If you are changing the file uploaded from upload_from_filename() to public, you can reuse the blob from your upload. Also, add a reloading of acl prior to changing the permission. This was all done in 1 block in Jupyter Notebook using GCP AI Platform.

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

Output:

enter image description here

Upvotes: 1

Related Questions