Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

angular - How access image in s3 bucket using pre-signed url

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to generate a presigned url for PutObject. Try "GetObject" instead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...