0

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:

  • Checked existence of blob file in bucket after upload code.
  • Introducing 5 seconds delay after upload.

Any help is appreciated.

london_utku
  • 1,070
  • 2
  • 16
  • 36
sandeepsign
  • 539
  • 6
  • 11

1 Answers1

1

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

Ricco D
  • 6,873
  • 1
  • 8
  • 18
  • 1
    Thanks a lot, it solved my problem. I am able to reuse the blob object to updated the acl permission. thanks! – sandeepsign Mar 19 '21 at 03:14