Home > database >  Issuing uploading file to Amazon S3 Bucket. using the AWS SDK for Java v2
Issuing uploading file to Amazon S3 Bucket. using the AWS SDK for Java v2

Time:07-13

I am trying to upload a file to an AWS S3 Bucket using the AWS SDK 2.0 for Java, but I am getting an error when trying to do so.

software.amazon.awssdk.services.s3.model.S3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method.

I am not sure what I am missing. I have tried adding in a key, but I am not even sure what I need to put in there, think it is just a name to refer to what has been uploaded though.

private S3Client s3Client;

private void upload() {
    setUpS3Client();

     PutObjectRequest putObjectRequest = PutObjectRequest.builder()
    .bucket(bucketName) //name of the bucket i am trying to upload to
    .key("testing") //No idea what goes in here.
    .build();

    byte[] objectByteArray = getObjectByteArray(bucketRequest.getPathToFile()); //bucketRequest just holds the data that will be sent
    PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, RequestBody.fromBytes(objectByteArray));
}

  private void setUpS3Client() {

    Region region = Region.AF_SOUTH_1;

    s3Client = S3Client.builder()
    .region(region)
    .credentialsProvider(createStaticCredentialsProvider())
    .build();

    this.s3Client = s3Client;
}

Does anyone know what this error is referring to and what I need to change to get the file to upload? Any help will be appreciated.

CodePudding user response:

This Java example works fine. Here is a screenshot of this Java code working:

enter image description here

You state:

key("testing") //No idea what goes in here.
    .build();

The key is the name of the object to update. For example, book.pdf to upload a PDF file. All input to Java AWS V2 examples is documented at start in the main method.

enter image description here

Now for your problem - make sure you have the required dependencies in the POM file. Use the POM file located here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3

Also at start of every program, there is a link to Java V2 Dev guide that talks about setting up your DEV Environment, including your creds.

CodePudding user response:

The problem was my credentials. The secret access key I was proving was incorrect. :(

  • Related