title: connect to AWS S3 using ARN Role in Java
body: <p>i have the working python code that success to connect and get files from S3:</p>
  <pre><code>import boto3  # 1.7.4
  import sys
  
  AWS_ACCESS_KEY_ID = '....'
  AWS_SECRET_ACCESS_KEY = '...'
  ROLE_NAME = '...'
  EXTERNAL_ID = '...' 
  S3_BUCKET = 'my_bucket'
  
  ROLE_SESSION_NAME = 'data-platform1'
  BASE_ROLE_ARN = 'arn:aws:iam::794878508111:role/'
  ROLE_ARN = BASE_ROLE_ARN + ROLE_NAME
  DURATION_SECONDS = 3600
  client = boto3.client(
      'sts',
      aws_access_key_id=AWS_ACCESS_KEY_ID,
      aws_secret_access_key=AWS_SECRET_ACCESS_KEY
  )
  
  role = client.assume_role(
      RoleArn=ROLE_ARN,
      RoleSessionName=ROLE_SESSION_NAME,
      DurationSeconds=DURATION_SECONDS,
      ExternalId=EXTERNAL_ID
  )
  session = boto3.session.Session(
      aws_access_key_id=role['Credentials']['AccessKeyId'],
      aws_secret_access_key=role['Credentials']['SecretAccessKey'],
      aws_session_token=role['Credentials']['SessionToken']
  )
  S3 = session.resource('s3')
  my_bucket = S3.Bucket(S3_BUCKET)
  </code></pre>
  <p>When i'm using the java code:</p>
  <pre><code>AWSCredentials awsCredentials1 = new AWSCredentials() {
                  @Override public String getAWSAccessKeyId() {
                      return &quot;...&quot;;
                  }
                  @Override public String getAWSSecretKey() {
                      return &quot;...&quot;;
                  }
              };
  
              AWSCredentials awsCredentials = new BasicAWSCredentials(awsCredentials1.getAWSAccessKeyId(), awsCredentials1.getAWSSecretKey());
              AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
                      .withRegion(Regions.US_EAST_1)
                      .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                      .build();
  
              AssumeRoleRequest roleRequest = new AssumeRoleRequest()
                      .withRoleArn(&quot;arn:aws:iam::794878508111:role/DPA-Havasedge&quot;)
                      .withRoleSessionName(&quot;...&quot;)
                      .withDurationSeconds(3600)
                      .withExternalId(&quot;...&quot;);
  
              AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);
              BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(roleResponse.getCredentials().getAccessKeyId(), roleResponse.getCredentials().getSecretAccessKey(),
                      roleResponse.getCredentials().getSessionToken());
  
  
              AmazonS3Client s3Client = (AmazonS3Client) AmazonS3ClientBuilder.standard()
                      .withCredentials(new AWSStaticCredentialsProvider(basicSessionCredentials))
                      .withRegion(Regions.US_EAST_1)
                      .build();
  
              List&lt;Bucket&gt; buckets = s3Client.listBuckets();
  </code></pre>
  <p>I fail with the error:</p>
  <pre><code>com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 86CF26DA1AE92E98; S3 Extended Request ID: BqA2Pj9vgjrqanBKFY4XMtGgL7NwUCaiF+sC2jJvvRBlrmsdnFbI9XUDIY/NG3rke21xrCdvMYI=), S3 Extended Request ID: BqA2Pj9vgjrqanBKFY4XMtGgL7NwUCaiF+sC2jJvvRBlrmsdnFbI9XUDIY/NG3rke21xrCdvMYI=
  </code></pre>
  <p>I'm using the same connection parameters as in python:</p>
  <pre><code>(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,ROLE_NAME,EXTERNAL_ID,S3_BUCKET,ROLE_SESSION_NAME,BASE_ROLE_ARN,)
  </code></pre>
  <p>please help be to find out what is wrong with my java code</p>
  <p>Thanks</p>
  <hr />
  <p>the exception i receive :</p>
  <pre><code>com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 0100BB5ADF1A6E06; S3 Extended Request ID: S5KsCFckCOeyx6VGqPnrda56C+IMPttpeqfMLQ/dbAfYQlvKUJ+xhEt9ux1QgVzbHasXNA2yybI=), S3 Extended Request ID: S5KsCFckCOeyx6VGqPnrda56C+IMPttpeqfMLQ/dbAfYQlvKUJ+xhEt9ux1QgVzbHasXNA2yybI=
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1630)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1302)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1056)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
  at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
  at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4330)
  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4277)
  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4271)
  at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:930)
  at com.amazonaws.services.s3.AmazonS3Client.listBuckets(AmazonS3Client.java:936)
  </code></pre>