首页 > 解决方案 > 使用服务帐户 HMAC 密钥进行身份验证时的跨项目 ListBuckets

问题描述

我正在尝试使用 AWS Java SDK 访问 Google Cloud Storage。我的特定场景是我想在项目“A”中使用服务帐户来列出项目“B”中的存储桶。Google 的指南没有涵盖这种类型的使用服务帐户的跨项目访问。

我尝试x-goog-project-id明确设置标题:

        // The access id and secret key below are for a service account in project "A".
        BasicAWSCredentials googleCreds = new BasicAWSCredentials(
                "my access id",
                "my secret key");

        AmazonS3 interopClient = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(
                        new AwsClientBuilder.EndpointConfiguration(
                                "https://storage.googleapis.com", "auto"))
                .withCredentials(new AWSStaticCredentialsProvider(googleCreds))
                .build();

        ListBucketsRequest listBucketsReq = new ListBucketsRequest();
        // The project id below is for project "B"
        listBucketsReq.putCustomRequestHeader("x-goog-project-id", "my project id");
        List<Bucket> buckets = interopClient.listBuckets(listBucketsReq);

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

但是我收到有关重复标头值的以下错误:

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: Multiple HTTP header values where one was expected. (Service: Amazon S3; Status Code: 400; Error Code: ExcessHeaderValues; Request ID: null; S3 Extended Request ID: null), S3 Extended Request ID: null

有没有办法让这个场景工作?

标签: google-cloud-storageaws-java-sdk

解决方案


根据这个线程中的信息,您必须将在项目A中创建的服务帐户添加到项目B中的IAM页面。然后添加相应的角色。


推荐阅读