'How to get project name/id from Google Cloud Storage bucket?
I'm given a Google Cloud Storage bucket address (gs://some_bucket_name
) to which I've already been granted read access. The bucket belongs to another project. Is there any way for me to find out what the project name or id that the bucket belongs to?
Solution 1:[1]
A good question.
Buckets don't have obvious scoping to projects and their globally unique names aren't presented as part of hierarchy that includes project identifiers.
However... acls may be a good bet... I'm not certain that this definitive, please check:
gsutil acl get gs://[[BUCKET-NAME]]
Which yields the projectNumber
:
[
{
"entity": "project-owners-123456789012",
"projectTeam": {
"projectNumber": "123456789012",
"team": "owners"
},
"role": "OWNER"
},
{
"entity": "project-editors-123456789012",
"projectTeam": {
"projectNumber": "123456789012",
"team": "editors"
},
"role": "OWNER"
},
{
"entity": "project-viewers-123456789012",
"projectTeam": {
"projectNumber": "123456789012",
"team": "viewers"
},
"role": "READER"
}
]
Solution 2:[2]
You can get the bucket project number using the Storage JSON API. More specifically, you can request GET https://storage.googleapis.com/storage/v1/b/<BUCKET_NAME>
. For more details and to test this request, check this document: https://cloud.google.com/storage/docs/json_api/v1/buckets/get
Once you get the project number you can get the project_id and / or name using either gcloud
or the resource manager API
Here is a complete example to get the project_id and name of a bucket:
authorization flow
# the user <ACCOUNT> must have these permission
# storage.buckets.get
# resourcemanager.projects.get on the bucket project
gcloud auth login <ACCOUNT>
BUCKET_NAME=<BUCKET_NAME>
ACCESS_TOKEN=$(gcloud auth print-access-token)
PROJECT_NUMBER=$(curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" https://storage.googleapis.com/storage/v1/b/${BUCKET_NAME} | jq -r .projectNumber)
gcloud projects list --filter="PROJECT_NUMBER=${PROJECT_NUMBER}" --format="value(PROJECT_ID, NAME)"
Solution 3:[3]
Thanks to everyone's answers I was able to craft the perfect gcloud command which does NOT require the project to be the current project:
- Get project id from project number 1234567890:
gcloud projects list --filter PROJECT_NUMBER=1234567890 --format="value(PROJECT_ID)"
- Get project number from project id
pincopallo
:
gcloud projects list --filter PROJECT_ID=pincopallo --format="value(PROJECT_NUMBER)"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | DazWilkin |
Solution 2 | MBHA Phoenix |
Solution 3 | Riccardo |