简体   繁体   English

无法通过 java sdk 在谷歌云存储中连接和创建存储桶

[英]Unable to connect and create bucket in google cloud storage via java sdk

I am unable to create a google cloud storage bucket using the java sdk, below is the code I am using:我无法使用 java sdk 创建谷歌云存储桶,以下是我使用的代码:

public class App 
{
    public static void main(String... args) throws Exception {

         String projectId = "**********";

         Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
            Page<Bucket> buckets = storage.list();

            for (Bucket bucket : buckets.iterateAll()) {
              System.out.println(bucket.getName());
            }

      }
}

And this is the error message I am getting:这是我收到的错误消息:

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized { "code" : 401, "errors" : [ { "domain" : "global", "location" : "Authorization", "locationType" : "header", "message" : "Anonymous caller does not have storage.buckets.list access to project 833568054484.", "reason" : "required" } ], "message" : "Anonymous caller does not have storage.buckets.list access to project 833568054484."引起:com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized { "code" : 401, "errors" : [ { "domain" : "global", "location" : "Authorization", "locationType " : "header", "message" : "匿名调用者没有项目 833568054484 的 storage.buckets.list 访问权限。", "reason" : "required" } ], "message" : "匿名调用者没有存储空间。 buckets.list 对项目 833568054484 的访问权限。” } }

You are not authenticating, you can check how to add permissions to your bucket on the Using Cloud IAM permissions documentation .您没有进行身份验证,您可以在使用 Cloud IAM 权限文档中查看如何向您的存储桶添加权限。

If you want to give access to authenticated users you have to select the user group allAuthenticatedUsers and give them the Storage Object Viewer Role.如果您想向经过身份验证的用户授予访问权限,您必须选择用户组allAuthenticatedUsers并为他们提供Storage Object Viewer角色。 This will allow all authenticated users, even anonymously, view/list permissions in your bucket.这将允许所有经过身份验证的用户,甚至匿名用户,查看/列出您存储桶中的权限。

NOTE: As you can see on this documentation , the allAuthenticatedUsers member types should only be used when it is acceptable for anyone on the Internet to read and analyze your data.注意:正如您在本文档allAuthenticatedUsersallAuthenticatedUsers成员类型仅应在 Internet 上的任何人都可以阅读和分析您的数据时使用。 So you should consider a different access management type if that is not the case for you.因此,如果您不是这种情况,则应考虑使用不同的访问管理类型。

1. Create a service account 1. 创建服务帐号

2. Creating a service account key for the service account just created. 2. 为刚刚创建的服务帐户创建服务帐户密钥 Remember the path where you store the key.json file.记住存储 key.json 文件的路径。

3. Assign to the service account the role Storage Admin 3. 角色 Storage Admin 分配给服务帐户

4. Provide credentials to your application 4.为您的应用程序提供凭据

a.一种。 Using env variable使用环境变量

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json" 

b.Explicit in code代码中明确


static void authExplicit(String jsonPath) throws IOException {
  // You can specify a credential file by providing a path to GoogleCredentials.
  // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
  GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
  Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

  System.out.println("Buckets:");
  Page<Bucket> buckets = storage.list();
  for (Bucket bucket : buckets.iterateAll()) {
    System.out.println(bucket.toString());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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