google-cloud-storage - 使用服务帐户 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
有没有办法让这个场景工作?
解决方案
根据这个线程中的信息,您必须将在项目A中创建的服务帐户添加到项目B中的IAM页面。然后添加相应的角色。
推荐阅读
- php - 不同的行背景颜色取决于 MySQL 数据
- php - 将文件上传到几个级别的文件夹 - PHP
- android - 抛出异常时Android CoroutineWorker重试
- javascript - 带有 ReactJS 的 Ace 编辑器 - 撤消功能
- angularjs - onChanges 不适用于组件绑定
- android - 与 ROOM 数据库的事务
- asp.net - asp.net MVC 应用程序中的更新数据库问题
- python - 从空数据框中删除列标签
- python - python 请求的 HTML(和 urllib)没有提供与原始 HTML 相同的 HTML
- c# - System.NullReferenceException 错误 Csharp