Abdul Rehman
Abdul Rehman

Reputation: 5654

Retrieve gs:// path for a storage Bucket and Object at GCS using Python api client

I'm working on a script to create google cloud functions using api, in which i need to get gs:// form of path to the storage bucket OR Object,

Here's what i have tried:

svc = discovery.build('storage', 'v1', http=views.getauth(), cache_discovery=False)
            svc_req = svc.objects().get(bucket=func_obj.bucket, object=func_obj.fname, projection='full')
            svc_response = svc_req.execute()
            print(' Metadata is coming below:')
            print(svc_response)

It returns metadata which doesn't include any link in the form of gs:// , how can i get the path to a bucket OR Object in the form of "gs://"

Help me, please!

Thanks in advance!

Upvotes: 0

Views: 2678

Answers (1)

A.Queue
A.Queue

Reputation: 1572

If you go to API Explorer and simulate your request you will see that this library returns you exactly the same output in python data structures. In there ID looks the most like gs:// link:

"id": "test-bucket/text.txt/0000000000000000"

or in python it looks like:

u'id': u'test-bucket/text.txt/00000000000000000'

Simple use this to transform it into a gs:// link:

import os
u'gs://' + os.path.dirname(svc_response['id'])

which would return:

 u'gs://test-bucket/text.txt'

If you want to use the google-cloud-python you will be facing the same issue.

Upvotes: 1

Related Questions