The AWS Access Key Id you provided does not exist in our records, when loading in corect AWS credentials

Viewed 605

I'm creating a spring boot backend, that can't seem to upload an image with a presigned url i have tried from three different clients now (postman, react, the backend itself).

All clients returns the same client.

In the very top i have declared a configuration like such:


    @Bean
    @Scope("singleton")
    public S3Service provideS3Service(){

         String secretKey = environment.getProperty("aws.key.secret");
         String accessKey = environment.getProperty("aws.key.access");
         String bucketName = environment.getProperty("aws.bucket.name");
         AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

         String regionName = environment.getProperty("aws.bucket.region");
         Regions clientRegion = Regions.valueOf(regionName);

         System.out.println(awsCredentials.getAWSAccessKeyId());
         System.out.println(awsCredentials.getAWSSecretKey());
         AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withRegion(clientRegion)
                .build();
         S3Service s3Service = new S3Service(bucketName, s3Client);


         return s3Service;


    }

Inside my S3 Service I have the following function to make the request. Right now i have also mocked the request which writes to aws.


    public PresignedUrlResponse createSignedUrlPost() throws IOException {
        String objectKey = UUID.randomUUID().toString();
        LocalDateTime expiration = getExpirationDate();

        Date expirationDate = Date.from(expiration.atZone(ZoneId.systemDefault()).toInstant());
        GeneratePresignedUrlRequest generatePresignedUrlRequest =
                new GeneratePresignedUrlRequest(this.bucketName, objectKey)
                .withMethod(HttpMethod.POST)
                .withExpiration(expirationDate);

        URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest);

        // Create the connection and use it to upload the new object using the pre-signed URL.
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
        out.write("This text uploaded as an object via presigned URL.");
        out.close();

        // Check the HTTP response code. To complete the upload and make the object available,
        // you must interact with the connection object in some way.
        connection.getResponseCode();
        System.out.println("HTTP response code: " + connection.getResponseCode());

        // Check to make sure that the object was uploaded successfully.
        S3Object object = amazonS3.getObject(bucketName, objectKey);
        System.out.println("Object " + object.getKey() + " created in bucket " + object.getBucketName());


        return new PresignedUrlResponse(url.toString(), expiration);
    }
    

This corresponds to the followin error:

{
  "timestamp": "Jul 18, 2020, 7:46:43 PM",
  "status": 500,
  "error": "Internal Server Error",
  "message": "The AWS Access Key Id you provided does not exist in our records. (Service: Amazon S3; Status Code: 403; Error Code: InvalidAccessKeyId; Request ID: DD94BBA0C28610CA; S3 Extended Request ID: tyyQDCnKD6ni+6uwumWh3psUvWM8TOVNguP8Y0dvkBPgmmUdebnrq+RBKqmXQcVQUyOxbqM5//o=; Proxy: null)",
  "path": "/chat/channels/signedurl"
}

I have ensured, that the credentials is correctly sat, and that they can be logged from the console aswell.


         System.out.println(awsCredentials.getAWSAccessKeyId());
         System.out.println(awsCredentials.getAWSSecretKey());

printed credentials

I have checked, that the credentials match that on my AWS account. Does anybody have any idea, what this issue, could be caused by?

I have tried everything now.

1 Answers

If you're printing out the access key and the secret key in that way, it looks as though they're the wrong way round.

The access key should always be the short one in uppercase A-Z and numbers, with the secret key being the longer string.

Check how you're passing them into your codebase to ensure you have not put them in, in the wrong order.

Related