amazon web services - Error: Generating signed url for s3 bucket file with custom domain in node.js - Stack Overflow

Here is the node.js script that, I am using to generate a signed URL for a file in S3 with a custom dom

Here is the node.js script that, I am using to generate a signed URL for a file in S3 with a custom domain:

const tempCreds = await assumeRole(roleArn, roleSessionName); 
const s3 = new S3Client({
        region: process.env.AWS_REGION,
        endpoint: '',
        s3BucketEndpoint: false,
        signatureVersion: 'v4',
        credentials: {
                accessKeyId: tempCreds.AccessKeyId,
                secretAccessKey: tempCreds.SecretAccessKey,
                sessionToken: tempCreds.SessionToken,
        } 
}); 
const bucketName = "storage.mydomain"; 
const expirationTime = 5 * 3600; // 5 hour in seconds 
const command = new GetObjectCommand({
        Bucket: bucketName,
        Key: key, 
}); 
const signedUrl = await getSignedUrl(s3, command, { expiresIn: expirationTime });

It's generating a URL something like this: .mydomain/6703b8f18bd4d8/ap.png?X-Amz-Algorithm=AWS4-HMAC-SHA....

On accessing this route, I am getting an error like this:

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>storage.mydomain/6703b8f18bd4d8/ap.png</Key>
<RequestId>Y3AZXK8CT2W1EA7S</RequestId>
<HostId>H8/cJYWdZRr9JAOquyiJyaF4fee5seG2kzsA4C+IqDYe3zwUlRHXHWlm93fP2SsKXwyUJgKC6yo=</HostId>
</Error>

My file is stored at key : 6703b8f18bd4d8/ap.png. But AWS is considering my key as 'storage.mydomain/6703b8f18bd4d8/ap.png', where 'storage.mydomain' is my bucket name.

Here is the node.js script that, I am using to generate a signed URL for a file in S3 with a custom domain:

const tempCreds = await assumeRole(roleArn, roleSessionName); 
const s3 = new S3Client({
        region: process.env.AWS_REGION,
        endpoint: 'https://storage.mydomain',
        s3BucketEndpoint: false,
        signatureVersion: 'v4',
        credentials: {
                accessKeyId: tempCreds.AccessKeyId,
                secretAccessKey: tempCreds.SecretAccessKey,
                sessionToken: tempCreds.SessionToken,
        } 
}); 
const bucketName = "storage.mydomain"; 
const expirationTime = 5 * 3600; // 5 hour in seconds 
const command = new GetObjectCommand({
        Bucket: bucketName,
        Key: key, 
}); 
const signedUrl = await getSignedUrl(s3, command, { expiresIn: expirationTime });

It's generating a URL something like this: https://storage.mydomain/storage.mydomain/6703b8f18bd4d8/ap.png?X-Amz-Algorithm=AWS4-HMAC-SHA....

On accessing this route, I am getting an error like this:

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>storage.mydomain/6703b8f18bd4d8/ap.png</Key>
<RequestId>Y3AZXK8CT2W1EA7S</RequestId>
<HostId>H8/cJYWdZRr9JAOquyiJyaF4fee5seG2kzsA4C+IqDYe3zwUlRHXHWlm93fP2SsKXwyUJgKC6yo=</HostId>
</Error>

My file is stored at key : 6703b8f18bd4d8/ap.png. But AWS is considering my key as 'storage.mydomain/6703b8f18bd4d8/ap.png', where 'storage.mydomain' is my bucket name.

Share Improve this question asked Mar 26 at 17:49 lazylazy 111 bronze badge 1
  • If you are using endpoint: "storage.mydomain", then storage.mydomain should not be the bucket name. Your domain might be a CloudFront or custom S3 endpoint. If it's CloudFront, S3 signed URLs won’t work. You need CloudFront-signed URLs instead. If storage.mydomain is an S3-compatible endpoint, set s3BucketEndpoint: true. Otherwise, remove it. – Piyush Jain Commented Mar 28 at 4:45
Add a comment  | 

1 Answer 1

Reset to default 0

OK, I think I understand your question now.

If you're saying that you already have a bucket named storage.mydomain, and your key inside that bucket is 6703b8f18bd4d8/ap.png, then my suggestion of using a bucket named that is not helpful.

Instead, changing from s3BucketEndpoint: false, to s3BucketEndpoint: true, tells S3 NOT to add the CNAME to the key.

const s3 = new S3Client({
  region: process.env.AWS_REGION,
  endpoint: 'https://storage.mydomain',
  s3BucketEndpoint: true,
  signatureVersion: 'v4',
  credentials: {
    accessKeyId: tempCreds.AccessKeyId,
    secretAccessKey: tempCreds.SecretAccessKey,
    sessionToken: tempCreds.SessionToken,
  },
});

I'm not sure, but you may also need to leave Bucket out of your config, so I'm not sure if you need this:

const command = new GetObjectCommand({
  Bucket: bucketName,
  Key: key,
});

or this:

const command = new GetObjectCommand({
  Key: key,
});

Original Answer

My file is stored at key : 6703b8f18bd4d8/ap.png. But AWS is considering my key as 'storage.mydomain/6703b8f18bd4d8/ap.png', where 'storage.mydomain' is my bucket name.

I don't know if this is the answer you want, but the results you're getting explain the solution. From the Amazon documentation:

Amazon S3 uses the hostname to determine the bucket name. So the CNAME and the bucket name must be the same.

Just put your file in a bucket with the same name as your CNAME.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744134520a4559996.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信