Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用 IAM 用户临时凭证创建请求 - AWS SDK for Java

IAM 用户或 AWS 账户可以使用AWS SDK for Java请求临时安全凭证 (请参阅创建请求),然后使用这些凭证访问 Amazon S3。在指定会话持续时间结束后,这些凭证将过期。要使用 IAM 临时安全凭证,请执行以下操作:

  1. 创建 AWSSecurityTokenServiceClient 类的实例。有关提供凭证的信息,请参阅使用 AWS 开发工具包、CLI 和 Explorer

  2. 通过调用安全令牌服务 (STS) 客户端的 assumeRole() 方法代入所需角色。

  3. 通过调用 STS 客户端的 getSessionToken() 方法开始会话。您可以使用 GetSessionTokenRequest 对象向此方法提供会话信息。

    此方法将返回临时安全凭证。

  4. 将临时安全凭证打包到 BasicSessionCredentials 对象中。您可以使用此对象向您的 Amazon S3 客户端提供临时安全凭证。

  5. 使用临时安全凭证创建 AmazonS3Client 类的实例。 可使用此客户端向 Amazon S3 发送请求。如果使用过期凭证发送请求,Amazon S3 将返回错误。

注意

如果使用 AWS 账户安全凭证获取临时安全凭证,则临时凭证的有效期只有一小时。只有当您使用 IAM 用户凭证请求会话时,您才可以指定会话持续时间。

以下示例列出了指定存储桶中的一组对象键。该示例将为两小时的会话获取临时安全凭证,然后使用这些凭证向 Amazon S3 发送经身份验证的请求。

如果您想要使用 IAM 用户凭证测试示例,需要在您的 AWS 账户下创建一个 IAM 用户。有关如何创建 IAM 用户的更多信息,请参阅 IAM 用户指南 中的创建您的第一个 IAM 用户和管理员组

有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicSessionCredentials; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectListing; import com.amazonaws.services.securitytoken.AWSSecurityTokenService; import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder; import com.amazonaws.services.securitytoken.model.AssumeRoleRequest; import com.amazonaws.services.securitytoken.model.Credentials; import com.amazonaws.services.securitytoken.model.GetSessionTokenRequest; import com.amazonaws.services.securitytoken.model.GetSessionTokenResult; public class MakingRequestsWithIAMTempCredentials { public static void main(String[] args) { String clientRegion = "*** Client region ***"; String roleARN = "*** ARN for role to be assumed ***"; String roleSessionName = "*** Role session name ***"; String bucketName = "*** Bucket name ***"; try { // Creating the STS client is part of your trusted code. It has // the security credentials you use to obtain temporary security credentials. AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Assume the IAM role. Note that you cannot assume the role of an AWS root account; // Amazon S3 will deny access. You must use credentials for an IAM user or an IAM role. AssumeRoleRequest roleRequest = new AssumeRoleRequest() .withRoleArn(roleARN) .withRoleSessionName(roleSessionName); stsClient.assumeRole(roleRequest); // Start a session. GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest(); // The duration can be set to more than 3600 seconds only if temporary // credentials are requested by an IAM user rather than an account owner. getSessionTokenRequest.setDurationSeconds(7200); GetSessionTokenResult sessionTokenResult = stsClient.getSessionToken(getSessionTokenRequest); Credentials sessionCredentials = sessionTokenResult.getCredentials(); // Package the temporary security credentials as a BasicSessionCredentials object // for an Amazon S3 client object to use. BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials( sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(), sessionCredentials.getSessionToken()); // Provide temporary security credentials so that the Amazon S3 client // can send authenticated requests to Amazon S3. You create the client // using the basicSessionCredentials object. AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(basicSessionCredentials)) .withRegion(clientRegion) .build(); // Verify that assuming the role worked and the permissions are set correctly // by getting a set of object keys from the bucket. ObjectListing objects = s3Client.listObjects(bucketName); System.out.println("No. of Objects: " + objects.getObjectSummaries().size()); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }

相关资源

本页内容: