Accessing s3 using credentials stored in laptop in .aws folder through Java

0

I am trying to access to s3 using Java. the credentials are stored in my laptop, but when i specify the credentials in code, its showing that the key doesn't exist.

Error:The AWS Access Key Id you provided does not exist in our records. (Service: S3, Status Code: 403, Request ID: ,,,,,,) Code written: Region region = Region.US_EAST_1;

String bucketname="x"; String keyname="y"; String Accesskey="z"; String Secretkey="t";

AwsBasicCredentials credentials=AwsBasicCredentials.create(Accesskey,Secretkey); AwsCredentialsProvider provider = StaticCredentialsProvider.create(credentials);

S3Client client = S3Client.builder().region(region) .credentialsProvider(provider) .build();

GetObjectRequest objectRequest = GetObjectRequest.builder() .bucket(bucketname) .key(keyname) .build();

1 Answer
0

Hi,

Have a look at the Java default credentials chain to see all possible ways to supply credentials to the AWS Java SDK: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials-chain.html

I personally use the env variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to avoid storing my credentials in my source code, which is a bad practice.

Best,

Didier

profile pictureAWS
EXPERT
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions