有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在google cloud storage builder中设置项目ID?

我正试图将图像上传到Google Cloud storage,但在创建StorageOptions时,我遇到了project must be specified.异常

我遵循官方文档,在执行以下步骤后选择使用Json凭证:

  1. 创建一个新的Service Account Key
  2. Role中,我选择了Owner(稍后更改)
  3. 下载Json键,并使用以下示例与之连接

    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());
    }
    

    我做错了什么


共 (1) 个答案

  1. # 1 楼答案

    人力资源经理,看来你是对的。在使用Java客户机创建bucket时,似乎没有一种设置项目的方法。我提交了一个错误:https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2643

    在设置StorageOptions时,您可能可以通过指定项目来解决这个问题:

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