301 PermanentRedirect is returned from the URL obtained by getSignedURL to putObject

Viewed 1100

I'm trying to get signed URL to put a file from aws-sdk with Node.js. When I use the address from the SDK, I will get 301 PermanentRedirect.

<Error>
<script/>
<Code>PermanentRedirect</Code>
<Message>
The bucket you are attempting to access must be addressed using the 
specified endpoint. Please send all future requests to this endpoint.
</Message>
<Bucket>XXX.COM</Bucket>
<Endpoint>XXX.COM.s3.amazonaws.com</Endpoint>
<RequestId>A82721F2A44XXXX</RequestId><HostId>HPgWeSUk2cKu3AbijEu9+S41bHh7dUxSiwOEEVrS08dsrpJ3AvMP5V2iXXXXXX
</HostId>
</Error>

I think I have no authentication problems since I could access the other API before calling getSignedURL API. I have no idea why I could get this error message. This is the code I using for getting signed URL.

   public static getSignedURL(path: string, op: string): Promise<string> {
    return new Promise<string>((resolve, reject) => {
        s3.getSignedUrl(op, {
            Bucket: "XXX.COM",
            Key: path,
            Expires: 60 * 60 * 24 * 365 * 10,
            ContentType: "text/javascript",
            ACL: 'public-read'
        }, (err, url) => {
            if (err) {
                reject(err);
            } else {
                resolve(url);
            }
        })
    });
    console.log(await getSignedURL(`XXX/${id}/${libName}.js`, "putObject"));

Is there any problems on my code?

1 Answers

The problem is that you created the S3 client in one region and you are signing a URL which references a bucket in a different region.

When you create the S3 client insert this line before the client creation specifying the correct region.

AWS.config.update({region: 'us-east-1'});
Related