使用 AWSCredentials 构建 AmazonS3Client 实例的首选方式
Preferred way build an instance of AmazonS3Client with AWSCredentials
AmazonS3Client has been deprecated in favor of AmazonS3ClientBuilder。 AmazonS3Client 构造函数接受了 AWSCredentials 的实例,客户端可以通过该实例传递 AWS 访问和密钥。
AWSCredentials credentialsProvider = ... ;
AmazonS3Client amazonS3Client = new AmazonS3Client(credentialsProvider.getCredentials());
使用 AmazonS3ClientBuilder 时,使用必要的 AWS 凭据实例化 AmazonS3 客户端的首选方法是什么?
根据AWS Documentation,可以使用AmazonS3ClientBuilder
如下:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new EnvironmentVariableCredentialsProvider())
.build();
这里使用的EnvironmentVariableCredentialsProvider,
provides credentials by looking at the: AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY) environment variables.
来源:AWS Documentation EnvironmentVariableCredentialsProvider
或者,您可以根据自己的实施调整 AWSCredentialsProvider。
You can use this technique to supply credential providers or provider chains that you create by using your own credential provider that implements the AWSCredentialsProvider interface
或者您可以使用亚马逊提供的实现,例如BasicAWSCredentials class 提供:
BasicAWSCredentials credentials = new BasicAWSCredentials("access_key_id", "secret_key_id");
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_WEST_2)
.build();
实现该接口的更多官方 class 是:
- AnonymousAWSCredentials: 允许匿名凭据的实现
- BasicSessionCredentials:带有密钥和会话令牌的会话凭证。
- PropertiesCredentials:从属性文件中读取 AWS 访问密钥。 AWS 访问密钥应在 "accessKey" 属性 中,AWS 密钥 ID 应在 "secretKey" 属性.
中
- STSSessionCredentials:AWS SecurityTokenServic 定期刷新会话凭证
AmazonS3Client has been deprecated in favor of AmazonS3ClientBuilder。 AmazonS3Client 构造函数接受了 AWSCredentials 的实例,客户端可以通过该实例传递 AWS 访问和密钥。
AWSCredentials credentialsProvider = ... ;
AmazonS3Client amazonS3Client = new AmazonS3Client(credentialsProvider.getCredentials());
使用 AmazonS3ClientBuilder 时,使用必要的 AWS 凭据实例化 AmazonS3 客户端的首选方法是什么?
根据AWS Documentation,可以使用AmazonS3ClientBuilder
如下:
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new EnvironmentVariableCredentialsProvider())
.build();
这里使用的EnvironmentVariableCredentialsProvider,
provides credentials by looking at the: AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY) environment variables.
来源:AWS Documentation EnvironmentVariableCredentialsProvider
或者,您可以根据自己的实施调整 AWSCredentialsProvider。
You can use this technique to supply credential providers or provider chains that you create by using your own credential provider that implements the AWSCredentialsProvider interface
或者您可以使用亚马逊提供的实现,例如BasicAWSCredentials class 提供:
BasicAWSCredentials credentials = new BasicAWSCredentials("access_key_id", "secret_key_id");
AmazonS3 s3 = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_WEST_2)
.build();
实现该接口的更多官方 class 是:
- AnonymousAWSCredentials: 允许匿名凭据的实现
- BasicSessionCredentials:带有密钥和会话令牌的会话凭证。
- PropertiesCredentials:从属性文件中读取 AWS 访问密钥。 AWS 访问密钥应在 "accessKey" 属性 中,AWS 密钥 ID 应在 "secretKey" 属性. 中
- STSSessionCredentials:AWS SecurityTokenServic 定期刷新会话凭证