AWS JS S3:ManagedUpload中“流”的类型是什么?

时间:2015-02-25 23:34:52

标签: javascript amazon-web-services amazon-s3

看着 http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#constructor-property

var upload = new AWS.S3.ManagedUpload({
  params: {Bucket: 'bucket', Key: 'key', Body: stream}
});

Body假设是stream。如果我想在浏览器中使用此stream,如何从本地文件创建ManagedUpload

streamstreamObject

中的uploadPart不同
Body: new Buffer('...') || 'STRING_VALUE' || streamObject,

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#uploadPart-property

1 个答案:

答案 0 :(得分:0)

呀。他们的文件很可怕。他们没有很好的例子。您可以尝试使用FileReader对象和readAsArrayBuffer方法。类似的东西:

    var reader = new FileReader();

    // Closure to capture the file information.
    reader.onload = (function(file) {
    return function(e) {
        var upload;
        if (e.eventPhase == 2) {
            upload = new AWS.S3.ManagedUpload({
                params: {Bucket: 'annuals-original', Key: 'name', Body: e.currentTarget.result}
            }).send(function(err, data) {
                console.log(err, data);
            });
         };
    })(f);

    // Read in the image file 
    reader.readAsArrayBuffer(f);

您可以将eventHandler附加到输入标记。

<input type="file" id="input" multiple onchange="handleFile(this.files)">

然后将文件传递给处理程序。

inputElement.addEventListener("change", handleFile, false);