Getting error while downloading the file using Blob created from URI Google Cloud Storage

Viewed 1532
client = storage.Client()
blob = Blob.from_string("gs://current-project/images/myimage.jpeg")
print(blob.bucket.name)
fileName = blob.name.split('/')[-1]
print(fileName)
print(blob)
blob.download_to_filename('test1.jpeg')

I am able to print the bucket name, blob info and fileName as well. But when I try to download the image it gives me an error.

AttributeError: 'NoneType' object has no attribute '_connection'

I am not sure what I am missing. If I use this link to download the image and explicitly specify the bucket name and create blob object from bucket it works fine. I am not understanding the error.

1 Answers

You are having this error because you are using the blob created with a local constructor blob = Blob.from_string. With this method, you don't retrieve the blob object from your bucket, you just create an empty object with this name in your local environment.

As you have stated the code sample from the documentation you linked works fine since it gets the blob from the bucket with an internal API when calling the methods.

bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
Related