How to set project ID in google cloud storage builder?

Viewed 3226

I am trying to upload an Image to Google Cloud storage but while creating StorageOptions I am getting project must be specified. exception.

I followed the official documentation and choose to use Json credentials after performing following steps:

  1. Create a new Service Account Key
  2. In Role I have selected Owner (change it later)
  3. Download the Json key and using following sample to connect with it

    public static void main(String[] args) throws IOException {
    Storage storage = StorageOptions.newBuilder()
            .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/Users/test/sample/app/project-936c0c6ef960.json")))
            .build()
            .getService();
    
    String bucketName = "Sample";
    Bucket bucket = storage.create(BucketInfo.of(bucketName));
    System.out.printf("Bucket %s created.%n", bucket.getName());
    }
    

    What am I doing wrong here?

1 Answers

Hrm, looks like you're right. There doesn't seem to be a way to set a project when creating a bucket with the Java client. I filed a bug: https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2643

You can probably get around this by specifying the project when setting up the StorageOptions:

StorageOptions.newBuilder().setProjectId("my-project-id")
    //continue as normal
Related