简体   繁体   中英

How to get project id in Google Cloud Storage

I have activated Google Cloud Storage on my Gmail account, then created a new Project. As far as I see, and read from tutorials ( https://developers.google.com/storage/docs/json_api/v1/json-api-java-samples ), Client Secrets (client_secrets.json) are related to Projects, not to my account.

Now, while using Java API, there is a way to receive the list of buckets,

https://developers.google.com/storage/docs/json_api/v1/buckets/list

Though it asks me to enter the project identifier as parameter.

I checked API documentation, the download client secrets file (client_secrets.json), and Java methods, though nothing is providing me the "Project Identifier" that is related to authenticated client secret's project.

Is there any method to find out on which project I am actively authenticated? I wouldn't want programme user to remember each project's identifier every time they authenticate in Java programme. The client secrets file is already related to a single project.


Extra Information: Currently I developed a GUI Java application. By using Open File Dialog, user selects the downloaded "client_secrets.json" file. Then I use that example provided "authorise" method to get Credentials and Storage objects.

After this, I want to list all buckets those are available in current project programmatically. (Current Project = The client_secrets.json is downloaded from a specific project.). But API requires me to enter the project name which doesn't seem to get programmatically.

You should be able to see it in the Developer Console . Click on a project, and at the top it will tell you the project ID.

(If you were looking for a way of getting this programmatically, please clarify this in your question - it's not clear what information you "have in your hand" when you want to get the project ID... or whether you're expecting your users to create their own projects.)

Run

gsutil ls -L -b gs://<bucket-name>

to get the bucket info, you can find the project number from the output. Then you could run

gcloud projects list --filter "PROJECT_NUMBER=<project-number>"

to find out the project ID.

Update: The current version of the json-file (used for authentication) contains the ProjectId.

The suggested way is to create a "service account key" json file, like so: https://cloud.google.com/docs/authentication/production#creating_a_service_account (p12 is only there for backward-compatibility)

Thus, you can read it from the json file like so: (C# Code, but Java Code should be very close) (Note: static constructor is called before first instantiation of class)

    public class YourClass
    {
        private static readonly string GoogleProjectId;

        static YourClass()
        {
            var GoogleAutFilePath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            var GContent = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(GoogleAutFilePath));
            GoogleProjectId = GContent["project_id"].ToString();
        }
    }

If you have jq :

project_number=$(gsutil acl get gs://<bucket> | jq ".[0].projectTeam.projectNumber")
gcloud projects list --filter "PROJECT_NUMBER=$project_number"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM