Home > Software design >  How can I get the unique identifier for a Google Cloud Platform blob?
How can I get the unique identifier for a Google Cloud Platform blob?

Time:01-05

I'm trying to store the file identifier in my DB but I'm not sure how to access it via the blob?

project_id = os.environ.get("GOOGLE_CLOUD_PROJECT", None)
bucket_name_for_file_uploads = os.environ.get("GS_BUCKET_NAME_FILE_UPLOADS", None)
client = storage.Client(project=project_id)
bucket = client.get_bucket(bucket_name_for_file_uploads)

# Upload file to file hosting container.
blob = bucket.blob(filename)
with open(filename, "rb") as my_file:
    blob.upload_from_file(my_file)

CodePudding user response:

When you create the Blob using bucket.blob(filename) you're assigning the Cloud Storage object with a name (filename).

This is the unique identifier of the Object within the Bucket.

A globally unique identifier is formed with gs://BUCKET_NAME/OBJECT_NAME

In your case, replacing with their values: gs://{bucket_name_for_file_uploads}/{filename}

NOTE Cloud Storage permits Object names to include "/". Using "/" in Object names as how folder paths are formed on other file-systems, causes e.g. Google's Storage Browser to present these Objects to you as if they were (they're not) in a folder hierarchy.

  •  Tags:  
  • Related