Sachu
Sachu

Reputation: 359

AWS SDK for Java S3 bucket Access Key Id issue

I am trying to access an Amazon S3 bucket programmatically through Java libraries. (to do basic cloud management from a third-party application). As a first step, I tried to print whether a bucket exists or not(3rd line)

AWSCredentials credentials=new BasicAWSCredentials("my-Access- Key","My- Secret-Key");
AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withRegion(Regions.AP_EAST_1).build();
String bucketExists=String.valueOf(s3client.doesBucketExistV2("newBucketName"));

When I run this line of code, I am getting an exception saying that

com.amazonaws.services.s3.model.AmazonS3Exception: The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID:RequestId...)

I don't want to maintain a credentials file in the .aws folder for the following reason:

I am trying to variablilize the access credentials based on the logged-in user from a secure LDAP system, so I can confirm the feasibility only when I test it with hard-coded credentials.

I have checked that the issue is not one of the below

Please let me know what the issue might be. My apologies if it is an amateur issue.

Upvotes: 2

Views: 6324

Answers (2)

jccampanero
jccampanero

Reputation: 53381

As indicated in the question comments, your code looks fine and it should work properly.

The most likely reason of the problem is that AWS is picking up other credentials from somewhere else. Please, try removing other credentials like the ones stored in the home directory in order to be sure that the SDK is using the right credentials when contacting S3.

In addition, please, verify that you are providing the right region according to your S3 bucket as well.

Upvotes: 1

smac2020
smac2020

Reputation: 10704

You are using the older V1 API. The Service Client name for V1 is AmazonS3. Likewise, the V2 service client name is S3Client. Amazon strongly recommends moving to V2:

The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.

Try following this step by step set of instructions, which is based on V2.

Get started with the AWS SDK for Java 2.x

WHen working with V2, you can put your creds in a file located in a .aws folder named credentials, as explained in this document. Once you follow all steps in this document, you can programmatically access an Amazon S3 bucket.

Upvotes: 0

Related Questions