Eri*_*rik 12 ajax xmlhttprequest amazon-s3
我通过ajax请求将文件输出到S3,大约50%的时间我收到ERR_CONNECTION_RESET错误.
我知道请求是正确签名的 - 任何想法可能导致这个?同样,这是我从多个位置和机器看到的间歇性问题.
这是我用来将我的文件输出到S3的相关coffeescript代码.它来自Micah Roberson和Rok Krulec在http://micahroberson.com/upload-files-directly-to-s3-w-backbone-on-heroku/和http://codeartists.com/post/36892733572/的工作.如何直接上传文件到亚马逊-s3-from-your.
createCORSRequest: (method, url) ->
xhr = new XMLHttpRequest()
if xhr.withCredentials?
xhr.open method, url, true
else if typeof XDomainRequest != "undefined"
xhr = new XDomainRequest()
xhr.open method, url
else
xhr = null
xhr
uploadToS3: (file, signature) ->
this_s3upload = this
this_s3upload.signature = signature
url = signature.signed_request
xhr = @createCORSRequest 'PUT', decodeURIComponent(signature.signed_request)
if !xhr
@onError 'CORS not supported'
else
xhr.onload = () ->
if xhr.status == 200
this_s3upload.onProgress 100, 'Upload completed.'
this_s3upload.onFinishS3Put file, this_s3upload.signature
else
this_s3upload.onError file, 'Upload error: ' + xhr.status
xhr.onerror = () ->
this_s3upload.onError file, 'XHR error.', this_s3upload.signature
xhr.upload.onprogress = (e) ->
if e.lengthComputable
percentLoaded = Math.round (e.loaded / e.total) * 100
if percentLoaded == 100
message = "Finalizing"
else
message = "Uploading"
this_s3upload.onProgress xhr, file, percentLoaded, message, this_s3upload.signature
xhr.onabort = ->
this_s3upload.onAbort file, "XHR cancelled by user.", this_s3upload.signature
xhr.setRequestHeader 'Content-Type', file.type
xhr.setRequestHeader 'x-amz-acl', 'public-read'
xhr.send file
Run Code Online (Sandbox Code Playgroud)
更新
在这个问题上,我一直得到亚马逊的非常关注.根据他们的建议,我创建了一个EC2 Windows实例,在其上加载了Chrome浏览器,并尝试使用我的代码上传5个文件10次.我没有看到错误一次.我偶尔会看到一些SignatureDoesNotMatch错误,但没有一个ERR_CONNECTION_RESET错误.我仍然在我使用的每个非EC2客户端/网络位置看到ERR_CONNECTION_RESET错误.
更新仍然没有解决方案.我已经从使用自动滚动签名算法转移到boto提供的算法.但是对ERR_CONNECTION_RESET问题没有影响.
小智 5
我在使用预签名 URL 上传更大的文件(长请求)时遇到了这个问题,遵循Heroku 的示例(node aws sdk):
app.get('/sign-s3', (req, res) => {
const s3 = new aws.S3();
const fileName = req.query['file-name'];
const fileType = req.query['file-type'];
const s3Params = {
Bucket: S3_BUCKET,
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if(err){
console.log(err);
return res.end();
}
const returnData = {
signedRequest: data,
url: `https://${S3_BUCKET}.s3.amazonaws.com/${fileName}`
};
res.write(JSON.stringify(returnData));
res.end();
});
});
Run Code Online (Sandbox Code Playgroud)
“Expires”参数使签名 URL 的有效期为 60 秒。
我认为当签名的 URL 在上传过程中过期时(即使在上传开始时它是有效的),请求会崩溃。
但它不会恰好在 60 秒后崩溃,而是在 60 到 120 秒之间随机崩溃。大多数情况下,客户端会记录 ERR_CONNECTION_RESET,有时会记录 403 FORBIDDEN。
当把它调到3600之后,我就不再有任何问题了。
我怀疑 EC2 上没有出现此问题,因为它们的上传速度非常快。
| 归档时间: |
|
| 查看次数: |
5244 次 |
| 最近记录: |