Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access google cloud storage bucket from other project using python

lets suppose I have google cloud storage bucket in project X and want to upload object in the bucket which is in project X from Code(Python) which is deployed on project Y. Both project X and Y are under same credentials(login id).

Is it achievable using OAuth2.0 or any other suggestion? I have tried using Service Account,AppAssertionCredentials & OAuth2DecoratorFromClientSecrets but failed.

credentials = GoogleCredentials.get_application_default()
service = discovery.build('storage', 'v1', credentials=credentials)
req = service.objects().insert(
            bucket=bucket_name,
            name=fileName,
            media_body=media)   
like image 686
Hardik Sachdeva Avatar asked May 03 '26 12:05

Hardik Sachdeva


1 Answers

This is a very common use case. You don't need to do anything special in your code to access buckets in other projects. Bucket names are globally unique, so your app will refer to an existing bucket in another project in the same way that it refers to buckets in its own project.

In order for that insert call to succeed, though, you'll need to make the account that is running that code an OWNER of the bucket that you're writing to.

Is that app engine code? App engine code runs as a particular service account. You'll need to grant permission to that service account. Head over to https://console.developers.google.com/permissions/serviceaccounts?project=_ to find out the name of that service account. It's probably something like It's probably [email protected].

Now, using the GCS UI, or via gsutil, give that account full control over the bucket:

gsutil acl ch -u [email protected]:FC gs://myBucketName
like image 134
Brandon Yarbrough Avatar answered May 05 '26 08:05

Brandon Yarbrough



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!