如何使用预签名的URL访问s3存储桶中的图像 [英] How access image in s3 bucket using pre-signed url

查看:472
本文介绍了如何使用预签名的URL访问s3存储桶中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用预签名的URL访问存储在s3存储桶中的图像.该图像需要访问具有预签名的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>

这是我尝试生成presigned-url的代码

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}`
            })
        };
    });


}

在aws方面,我阻止了所有公共访问. 这是存储桶政策

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/*"
        }
    ]
}

推荐答案

您正在尝试为PutObject生成一个预签名的url.尝试使用"GetObject"

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

这篇关于如何使用预签名的URL访问s3存储桶中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆