浏览器直接上传到S3与流星,jQuery和AWS的SDK流星、浏览器、SDK、jQuery

由网友(相关姐妹推荐>>)分享简介:我已经钻进的话题几乎所有的资源,但仍然需要你的帮助,使这项工作。我想直接文件从我的流星应用程序的浏览器上传到我的S3直接。对于这一点,我提供了一个签名的URL到客户端就像在该简化示例:I have got into almost every resource on the topic, but still need...

我已经钻进的话题几乎所有的资源,但仍然需要你的帮助,使这项工作。我想直接文件从我的流星应用程序的浏览器上传到我的S3直接。对于这一点,我提供了一个签名的URL到客户端就像在该简化示例:

I have got into almost every resource on the topic, but still need your help to make this work. I want to directly upload files to my S3 directly from the browser in my Meteor app. For this, I provide a signed url to the client just like in this simplified example:

Meteor.methods({
    requestUpload: function(filename) {
        var fut = new Future();
        new Fiber(function() {
            var params = {
                Bucket: MY_BUCKET,
                Key: new Date().getTime() + "_" + filename
            };
            var surl = s3.getSignedUrl('putObject', params, function(err, surl) {
                if (!err) {
                    console.log("signed url: " + surl);
                    fut.return(data);
                } else {
                    console.log("Error signing url " + err);
                    fut.return();
                }
            });
        }).run();
        return fut.wait();
    }
}

然后客户端调用此方法,得到签名的URL看起来像这样

The client then calls this method, obtains the signed url which looks like this

https://mybucket.s3-eu-west-1.amazonaws.com/1382890365957_myfile.png?AWSAccessKeyId=AKBLABLA&Expires=1382891265&Signature=BLABLA

和尝试上传的文件,在这个片段中使用jQuery像一个POST请求:

and tries to upload the file with a POST request using jQuery like in this snippet:

Template.form.events({
    'submit form': function(e, t) {
        e.preventDefault();
        var fileInput = t.find("input[type=file]");
        for (var i = 0; i < fileInput.files.length; i++) {
            var file = fileInput.files[i];
            Meteor.call("requestUpload", file.name, function(err, surl) {
                if (!err) {
                    console.log("signed url: " + surl);
                    var reader = new FileReader();
                    reader.onload = function(event) {

                    // Here I am trying to upload, it fails
                        $.post(surl, reader.result, function(data, status) {
                            console.log("status: " + status);
                            console.log("data: " + data);
                        });
                    };
                    reader.readAsDataURL(file);
                } else {
                    console.log(err);
                }
            });
        }
    }
});

我想使用jQuery,因为我的东西它可能覆盖了大量的浏览器和浏览器版本的好办法。我还检查我的CORS配置这个特殊的水桶,它看起来是这样的:

I want to use jQuery because I thing it's probably a good way to cover a lot of browsers and browser versions. I also checked my CORS configuration for this particular bucket, it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

的凭据,我配置我的SDK上都保证了服务器是好的,他们有管理员的权限,我用他们所有的时间从服务器将和获取对象的S3。另外,如果我签一个URL的GET调用上的私人文件,已签名的URL是否有效。我可能做错事的职位通话。

The credentials, that I configure my SDK with on the server are ensured to be alright, they have admin permissions and I am using them all the time for putting and getting objects in the S3 from the server. Also, if I sign a url for a GET call on a private file, the signed url is valid. I am probably doing something wrong with the post call.

任何帮助是非常AP preciated!

Any help is very appreciated!

推荐答案

要得到这个工作,我有一对夫妇的参数添加到 s3.getSignedUrl 电话:

To get this working I had to add a couple of parameters to the s3.getSignedUrl call:

ContentType: mimeType,
Body: '',
"ACL": 'public-read'

完整的方法:

Full method:

Meteor.methods({
    requestUpload: function(filename, mimeType) {
        var fut = new Future();
        new Fiber(function() {
            var params = {
                Bucket: MY_BUCKET,
                Key: new Date().getTime() + "_" + filename,
                ContentType: mimeType,
                Body: '',
                "ACL": 'public-read'
            };
            var surl = s3.getSignedUrl('putObject', params, function(err, surl) {
                if (!err) {
                    console.log("signed url: " + surl);
                    fut.return(surl);
                } else {
                    console.log("Error signing url " + err);
                    fut.return();
                }
            });
        }).run();
        return fut.wait();
    }
}
阅读全文

相关推荐

最新文章