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
1 Answer
Reset to default 0OK, 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条)