且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用预签名网址将对象放入亚马逊s3?

更新时间:2023-11-27 18:15:10

我成功地使用您的代码上传了一个文件。

I managed to succesfully upload a file by using your code.

以下是我遵循的步骤:


  1. 创建一个新的存储桶和新的IAM用户

  1. Created a new bucket and a new IAM user

设置IAM用户的政策如下:

Set IAM user's policy as below:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1418647210000",
            "Effect": "Allow",
            "Action": [
                "s3:Put*"
            ],
            "Resource": [
                "arn:aws:s3:::myBucket/*"
            ]
        }
    ]
}


  • 未创建存储桶策略

  • Did NOT create a bucket policy

    使用您的代码生成预先签名的URL:

    Used your code to generate the pre-signed URL:

    var aws = require('aws-sdk');
    aws.config = {
        accessKeyId: myAccessKeyId,
        secretAccessKey: mySecretAccessKey
    };
    
    var s3 = new aws.s3();
    s3.getSignedUrl('putObject', {
        Bucket: 'myBucket',
        Expires: 60*60,
        Key: 'myKey'
    }, function (err, url) {
        console.log(url);
    });
    


  • 在屏幕上复制URL并使用curl测试上传,如下所示:

  • Copied the URL on the screen and used curl to test the upload as below:

    curl.exe -k -X PUT -T "someFile" "https://myBucket.s3.amazonaws.com/myKey?AWSAccessKeyId=ACCESS_KEY_ID&Expires=1457632663&Signature=Dhgp40j84yfjBS5v5qSNE4Q6l6U%3D"
    


  • 在我的情况下,政策更改通常需要5-10秒才能生效,所以如果第一次失败,请务必继续发送一段时间。

    In my case it generally took 5-10 seconds for the policy changes to take effect so if it fails the first time make sure to keep sending it for a while.

    希望这会有所帮助。