Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/java_examples/S3Examples/MakingRequestsWithIAMTempCredentials.java
4084 views
1
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
3
4
import com.amazonaws.AmazonServiceException;
5
import com.amazonaws.SdkClientException;
6
import com.amazonaws.auth.AWSStaticCredentialsProvider;
7
import com.amazonaws.auth.BasicSessionCredentials;
8
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
9
import com.amazonaws.services.s3.AmazonS3;
10
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
11
import com.amazonaws.services.s3.model.ObjectListing;
12
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
13
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
14
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
15
import com.amazonaws.services.securitytoken.model.Credentials;
16
import com.amazonaws.services.securitytoken.model.GetSessionTokenRequest;
17
import com.amazonaws.services.securitytoken.model.GetSessionTokenResult;
18
19
public class MakingRequestsWithIAMTempCredentials {
20
public static void main(String[] args) {
21
String clientRegion = "*** Client region ***";
22
String roleARN = "*** ARN for role to be assumed ***";
23
String roleSessionName = "*** Role session name ***";
24
String bucketName = "*** Bucket name ***";
25
26
try {
27
// Creating the STS client is part of your trusted code. It has
28
// the security credentials you use to obtain temporary security credentials.
29
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
30
.withCredentials(new ProfileCredentialsProvider())
31
.withRegion(clientRegion)
32
.build();
33
34
// Assume the IAM role. Note that you cannot assume the role of an AWS root account;
35
// Amazon S3 will deny access. You must use credentials for an IAM user or an IAM role.
36
AssumeRoleRequest roleRequest = new AssumeRoleRequest()
37
.withRoleArn(roleARN)
38
.withRoleSessionName(roleSessionName);
39
stsClient.assumeRole(roleRequest);
40
41
// Start a session.
42
GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest();
43
// The duration can be set to more than 3600 seconds only if temporary
44
// credentials are requested by an IAM user rather than an account owner.
45
getSessionTokenRequest.setDurationSeconds(7200);
46
GetSessionTokenResult sessionTokenResult = stsClient.getSessionToken(getSessionTokenRequest);
47
Credentials sessionCredentials = sessionTokenResult.getCredentials();
48
49
// Package the temporary security credentials as a BasicSessionCredentials object
50
// for an Amazon S3 client object to use.
51
BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(
52
sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(),
53
sessionCredentials.getSessionToken());
54
55
// Provide temporary security credentials so that the Amazon S3 client
56
// can send authenticated requests to Amazon S3. You create the client
57
// using the basicSessionCredentials object.
58
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
59
.withCredentials(new AWSStaticCredentialsProvider(basicSessionCredentials))
60
.withRegion(clientRegion)
61
.build();
62
63
// Verify that assuming the role worked and the permissions are set correctly
64
// by getting a set of object keys from the bucket.
65
ObjectListing objects = s3Client.listObjects(bucketName);
66
System.out.println("No. of Objects: " + objects.getObjectSummaries().size());
67
}
68
catch(AmazonServiceException e) {
69
// The call was transmitted successfully, but Amazon S3 couldn't process
70
// it, so it returned an error response.
71
e.printStackTrace();
72
}
73
catch(SdkClientException e) {
74
// Amazon S3 couldn't be contacted for a response, or the client
75
// couldn't parse the response from Amazon S3.
76
e.printStackTrace();
77
}
78
}
79
}
80
81