ishanbakshi
ishanbakshi

Reputation: 1965

PermanentRedirect error while uploading to S3 bucket with aws-sdk-java

I am trying to upload a file from a java class to aws S3.

I am using the exact code as given here

The only parts I changed are these:

private static String bucketName     = "s3-us-west-2.amazonaws.com/<my-bubket-name>";
private static String keyName        = "*** Provide key ***";
private static String uploadFileName = "/home/...<localpath>.../test123";

I am not sure what to add in Provide Key . But even if I leave it this way, i get an error like this :

Error Message: The bucket is in this region: null.Please use this region to retry the request (Service: Amazon S3; Status Code: 301; Error Code: PermanentRedirect; Request ID: *******) HTTP Status Code: 301 AWS Error Code: PermanentRedirect Error Type: Client

Upvotes: 3

Views: 14976

Answers (5)

Smolkis
Smolkis

Reputation: 11

For me this was caused by changing from version v2 to v3 of the sdk, see the migration hints. You may need to specify region, or set followRegionRedirects.

Upvotes: 0

user25750731
user25750731

Reputation: 1

TL;DR, check the AWS SDK version you are using and make sure your Lambda is in the same region as the S3 bucket. If you are using cross region, you will have to get a work around. There should be a process for that.

Ran into the same issue. We had 2 Lambdas. One running Node 16 with AWS SDK V2, the other running Node 18 with AWS SDK V3.

With V2, you are able to seamlessly write to Cross region S3. I.e. with V2 and a simple LambdaS3 role, A lambda running in US-East-1, you can write to S3s in US-East-1 as well as US-West-1.

But with V3, you are not able to do that all things equal.

In our situation, we had a long existing S3 bucket in California with a Lambda in Ohio. With Node 16 & SDK V2, there was no problem. Later We created a new lambda for a different task but using the same bucket. AWS V3 Node 18.

{
"errorType": "PermanentRedirect",
"errorMessage": "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.",
"name": "PermanentRedirect",
"$fault": "client",
"$metadata": {
    "httpStatusCode": 301,
    "requestId": "JYSJYFP5GQRVDYZ1",
    "extendedRequestId": "KSan7O2/qzk0wZpvq7IwreS+VEqOT3150obLoYVchAQwLXPqzuESfWafqHgiBJa/a6bxpJBkYkw=",
    "attempts": 1,
    "totalRetryDelay": 0
},
"Code": "PermanentRedirect",
"Endpoint": "bucket-us-west-1.s3-us-west-1.amazonaws.com",
"Bucket": "bucket-us-west-1",
"RequestId": "JYSJYFP5GQRVDYZ1",
"HostId": "KSan7O2/qzk0wZpvq7IwreS+VEqOT3150obLoYVchAQwLXPqzuESfWafqHgiBJa/a6bxpJBkYkw=",
"message": "The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.",
"stack": [
    "PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.",
    "    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)",
    "    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5",
    "    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-s3/dist-cjs/index.js:4756:14)",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
    "    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20",
    "    at async /var/runtime/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:225:18",
    "    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38",
    "    at async /var/runtime/node_modules/@aws-sdk/middleware-flexible-checksums/dist-cjs/index.js:173:18",
    "    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:97:20",
    "    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:120:14"
]
}

Upvotes: 0

aloke kumar
aloke kumar

Reputation: 31

Ensure you have used the correct region. Changing the region from us-west-2 to us-east-1 worked for me.

Upvotes: 3

Ankit Singh
Ankit Singh

Reputation: 345

You need to specify the name of your bucket. KeyName is the place inside your specified bucket where you wanna store your file. It should something like this:

private static String bucketName = "xyz";// xyz is my bucket name at s3
private static String keyName = "upload/test/";// this is location where 
//file will be stored
private static String uploadFileName = "/home/...<localpath>.../test123";

Upvotes: 1

Cagatay Gurturk
Cagatay Gurturk

Reputation: 7246

Instead of s3-us-west-2.amazonaws.com/<my-bucket-name> you should put <my-bucket-name>.

Upvotes: 4

Related Questions