AuthUsingTempSessionTokenJava
Making Requests Using IAM User Temporary Credentials - AWS SDK for Java
An IAM user or an AWS Account can request temporary security credentials (see Making Requests) using the AWS SDK for Java and use them to access Amazon S3. These credentials expire after the specified session duration. To use IAM temporary security credentials, do the following:
Create an instance of the
AWSSecurityTokenService
class. For information about providing credentials, see Using the AWS SDKs, CLI, and Explorers.Retrieve the temporary security credentials for the desired role by calling the
assumeRole()
method of the Security Token Service (STS) client.Package the temporary security credentials into a
BasicSessionCredentials
object. You use this object to provide the temporary security credentials to your Amazon S3 client.Create an instance of the
AmazonS3Client
class using the temporary security credentials. You send requests to Amazon S3 using this client. If you send requests using expired credentials, Amazon S3 will return an error.
Note
If you obtain temporary security credentials using your AWS account security credentials, the temporary credentials are valid for only one hour. You can specify the session duration only if you use IAM user credentials to request a session.
The following example lists a set of object keys in the specified bucket. The example obtains temporary security credentials for a session and uses them to send an authenticated request to Amazon S3.
If you want to test the sample using IAM user credentials, you will need to create an IAM user under your AWS Account. For more information about how to create an IAM user, see Creating Your First IAM User and Administrators Group in the IAM User Guide.
For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.