I want access an image stored in s3 bucket using pre-signed url.This image needs to access who has the pre-signed url.Please help me to solve this issue.
I have no idea what signature is it getting.If it's getting my signature, then how it's works for other users.Please help me
When I doing this.It will show this error
<Error>
<Code>SignatureDoesNotMatch</Code>
</Message>
<AWSAccessKeyId>XX</AWSAccessKeyId>
<StringToSign>GET 1900675866 /bucketname/161305.jpg</StringToSign>
<SignatureProvided>xxxx</SignatureProvided>
<StringToSignBytes>
47 45 54 0a 0a 0a 31 39 30 30 36 37 35 38 36 36 0a 2f 6b 6c 70 2d 6d 65 64 69 61 2f 31 36 31 33 30 35 2e 6a 70 67
</StringToSignBytes>
<RequestId>xxxxx</RequestId>
<HostId>
xxxx
</HostId>
</Error>
Here is the code I tried to generate presigned-url
exports.handler = async function (event, context) {
console.log('strarting to generate pre-signed image url');
let s3BucketName = process.env.S3_BUCKET_NAME;
const request = JSON.parse(event.body);
const objectKey = request.key;
const studentId = request.studentId;
console.log(' generate pre-signed image url for:' + objectKey);
console.log('Started getting pre signed url for:' + objectKey);
let params = {
Bucket: s3BucketName,
Key: objectKey,
Expires: 60 * 60 * 24 * 365 * 10,
ContentType: 'image/jpg'
};
return await s3.getSignedUrlPromise('putObject', params).then(async url => {
console.log('Successfully generated pre signed image url', url);
const payload = {
message: 'success',
imageUrl: url
};
const response = {
statusCode: 201,
headers: responseHeaders,
body: JSON.stringify(payload),
};
return response;
}).catch(error => {
console.error(`Failed to generate presigned url: ${error}`);
return {
statusCode: error.statusCode || 501,
headers: responseHeaders,
body: JSON.stringify({
message: `Failed to generate presigned url. Request Id: ${context.awsRequestId}`
})
};
});
}
In aws side, I blocked all public access.
Here is the bucket policy
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::media/*"
}
]
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…