如何通过CloudFront实施分段上传?

时间:2018-07-25 05:55:09

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

这是我目前的代码,已成功通过CloudFront将文件上传到S3:

let fileObject = thisUploader.getFile(id); //returns File API
let reader = new FileReader(); //using the FileReader API to read files
reader.onload = function () {                                      
	$.ajax({
		url: 'http://sampleId.cloudfront.net/video.mp4?Policy=examplePolicy&Signature=exampleSignature&Key-Pair-Id=exampleKey',
		type: 'PUT',
		contentType: fileObject.type,
		data: reader.result,
		processData: false,
		crossDomain: true
	});
}
reader.readAsArrayBuffer(fileObject);

现在对于分块上传(或AWS术语中的多部分),我不知道如何通过CloudFront(只有S3的文档)来做到这一点。

所以我的问题是,通过CloudFront实施分段上传需要采取什么步骤?

我尝试过的操作:我已在CloudFront控制台中启用了Forward Query Strings选项,并将参数uploads添加到了白名单。我为S3尝试了guides,只是测试了这段代码以启动多部分上传(请注意添加的uploads参数):

$.ajax({
    url: 'http://sampleId.cloudfront.net/video.mp4'+'?uploads'+'&Policy=examplePolicy&Signature=exampleSignature&Key-Pair-Id=exampleKey',
    type: 'POST',
    contentType: 'application/json'
});

它发送了预检请求(OPTIONS),并返回带有以下响应头的200 OK

Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET, PUT, HEAD, POST
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3000
Connection: keep-alive
Content-Length: 0
Date: Thu, 02 Aug 2018 07:03:01 GMT
Server: AmazonS3
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Via: 1.1 7111a943ba8327e4a6723f271fc9f7c4.cloudfront.net (CloudFront)
X-Amz-Cf-Id: oeE4D7FM8TyxE7NtmVidbyADGLqvwOnc49XdI1ps-tHIUKXrdm2PGg==
X-Cache: Miss from cloudfront

但是真正的POST请求返回带有以下响应标头的403 Forbidden

Accept-Ranges: none
Access-Control-Allow-Methods: GET, PUT, HEAD, POST
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3000
Age: 13976
Connection: keep-alive
Content-Length: 445
Content-Type: text/html
Date: Thu, 02 Aug 2018 03:10:05 GMT
ETag: "07468170dde4c34c5990eee2d1c2ae1e"
Last-Modified: Wed, 01 Aug 2018 08:34:47 GMT
Server: AmazonS3
Via: 1.1 7111a943ba8327e4a6723f271fc9f7c4.cloudfront.net (CloudFront)
X-Amz-Cf-Id: EWPPWw0l9BTmhFFe39HWdUuCp2Ab2L3yE5bj9wEqFvl3mVbNmt28Pg==
x-amz-replication-status: FAILED
x-amz-version-id: yLhhgPevn2eeofp7kEmcvixP3iW.vj7S
X-Cache: Error from cloudfront

2 个答案:

答案 0 :(得分:1)

不幸的是,Cloudfront不能上传,您需要通过S3路径或前缀URL来上传文件。

答案 1 :(得分:0)

最后,我必须关闭Cloudfront的“限制查看器访问”选项。分段上传似乎不能很好地发挥作用,或者打开它时查询字符串参数正向转发不能正常工作。