Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get URI of a blob in a google cloud storage (Python)

If I have a Blob object how can I get the URI (gs://...)?

The documentation says I can use self_link property to get the URI, but it returns the https URL instead (https://googleapis.com...) I am using python client library for cloud storage.

Thank you

like image 863
ANIVGames Avatar asked Apr 30 '20 21:04

ANIVGames


People also ask

How do I use Google Cloud Storage URI?

To load data from a Cloud Storage data source, you must provide the Cloud Storage URI. The Cloud Storage URI comprises your bucket name and your object (filename). For example, if the Cloud Storage bucket is named mybucket and the data file is named myfile. csv , the bucket URI would be gs://mybucket/myfile.csv .

What is Blob name in GCS?

The name of the blob. This corresponds to the unique path of the object in the bucket. If bytes, will be converted to a unicode object. Blob / object names can contain any sequence of valid unicode characters, of length 1-1024 bytes when UTF-8 encoded.


Video Answer


1 Answers

Since you are not sharing with us how exactly are you trying to achieve this I did a quick script in Python to get this info

There is no specific method in blob to get the URI as gs:// in Python but you can try to script this by using the path_helper

def get_blob_URI():
    """Prints out a bucket's labels."""
    # bucket_name = 'your-bucket-name'
    storage_client = storage.Client()
    bucket_name = 'YOUR_BUCKET'
    blob_name = 'YOUR_OBJECT_NAME'
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)
    link = blob.path_helper(bucket_name, blob_name)
    pprint.pprint('gs://' + link)

If you want to use the gsutil tool you can also get all the gs:// Uris of a bucket using the command gsutil ls gs://bucket

like image 192
Chris32 Avatar answered Oct 26 '22 15:10

Chris32