使用带有 Java 的服务帐户访问云存储的 S3 API

时间:2021-01-26 13:53:38

标签: authentication amazon-s3 oauth-2.0 google-cloud-storage service-accounts

我想使用 Java AWS S3 API 访问我的 GCP Cloud Storage 存储桶以实现互操作性,因此不想在我的代码中使用 Google API。在访问 Google Cloud Storage 时,我已经看到了使用 S3 API 进行基本身份验证的示例。我想通过我的服务帐户 json 密钥 ( OAuth 2 ) 进行身份验证,而不是为 Cloud Storage 创建另一个 HMAC 密钥。任何人都可以给我指出一个使用 Java 的例子。推荐的方法是什么 - 我认为 json oauth 2 身份验证是一个很好的方法。

基本身份验证如下:

                // Create a BasicAWSCredentials using Cloud Storage HMAC credentials.
                BasicAWSCredentials googleCreds =
                    new BasicAWSCredentials(googleAccessKeyId, googleAccessKeySecret);

                // Create a new client and do the following:
                // 1. Change the endpoint URL to use the Google Cloud Storage XML API endpoint.
                // 2. Use Cloud Storage HMAC Credentials.
                AmazonS3 interopClient =
                    AmazonS3ClientBuilder.standard()
                        .withEndpointConfiguration(
                                new AwsClientBuilder.EndpointConfiguration("https://storage.googleapis.com", "auto"))
                        .withCredentials(new AWSStaticCredentialsProvider(googleCreds))
                        .build();

但我想使用为服务帐户生成的 json 密钥进行身份验证。该服务帐号有权访问 Cloud Storage Viewer 和 Cloud Storage Creator。

谢谢

0 个答案:

没有答案