How do I client-side upload a viewable file to Amazon S3?

前端 未结 7 1343
别那么骄傲
别那么骄傲 2020-12-13 15:20

Let me start of by saying that I am normally very reluctant to post this questions as I always feel that there\'s an answer to everything SOMEWHERE on the internet. After sp

相关标签:
7条回答
  • 2020-12-13 16:27

    You can in fact use getSignedURL as you specified above. Here's an example on how to both get a URL to read from S3, and also use getSignedURL for posting to S3. The files get uploaded with the same permissions as the IAM user that was used to generate the URLs. The problems you are noticing may be a function of how you are testing with curl? I uploaded from my iOS app using AFNetworking (AFHTTPSessionManager uploadTaskWithRequest). Here's an example on how to post using the signed URL: http://pulkitgoyal.in/uploading-objects-amazon-s3-pre-signed-urls/

    var s3 = new AWS.S3();  // Assumes you have your credentials and region loaded correctly.
    

    This is for reading from S3. URL will work for 60 seconds.

    var params = {Bucket: 'mys3bucket', Key: 'file for temp access.jpg', Expires: 60};
    var url = s3.getSignedUrl('getObject', params, function (err, url) {
              if (url) console.log("The URL is", url);
           });
    

    This is for writing to S3. URL will work for 60 seconds.

            var key = "file to give temp permission to write.jpg";
            var params = {
                Bucket: 'yours3bucket',
                Key: key,
                ContentType: mime.lookup(key),      // This uses the Node mime library
                Body: '',
                ACL: 'private',
                Expires: 60
            };
            var surl = s3.getSignedUrl('putObject', params, function(err, surl) {
                if (!err) {
                    console.log("signed url: " + surl);
                } else {
                    console.log("Error signing url " + err);
                }
            });
    
    0 讨论(0)
提交回复
热议问题