使用 jquery.fileupload 直接上传到亚马逊 S3
Direct upload to amazon S3 using jquery.fileupload
在我的应用程序中,我希望能够将文件直接从浏览器上传到我的 AWS S3 存储桶。我的后端是 rails,但我想避免额外跳到我的服务器,并避免使用 gems,如回形针、载波、carrierwave_direct 等,以保持简单。我大致遵循 heroku 中的这个 tutorial。
我正在使用 aws-sdk gem 和 jquery.fileupload.js 库。
问题是当我尝试上传时,我从 AWS 返回了一个 400 错误请求。
我认为这不是 CORS 问题。我在我的存储桶上配置了 CORS,我可以看到一个成功的 OPTIONS 请求,然后是 POST 文件上传请求,returns 400 错误请求。
以下是复制问题的简化演示。
这是控制器动作。它生成一个 AWS::S3::PresignedPost 对象,因此视图可以使用它 post 文件直接到 S3。
def new
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('[FILTERED]', '[FILTERED]'),
})
s3 = Aws::S3::Resource.new
bucket = s3.bucket('mybucket')
@presigned_post = bucket.presigned_post(key: "attachments/#{SecureRandom.uuid}/${filename}")
@thing = Thing.new
end
这是视图,new.html.erb
,由上面的呈现,带有上传表单和 javascript 来处理上传。
<div class='container'>
<%= form_for(@thing, html: { class: 'direct_upload' }) do |f| %>
<%= f.label 'Thing' %>
<%= f.file_field :attachment_url %>
<%= f.submit %>
<% end %>
</div>
<script type="text/javascript">
$(function() {
var $form = $('form.direct_upload'),
upload_url = '<%= escape_javascript(@presigned_post.url.to_s) %>',
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>';
console.log('URL: ', upload_url);
console.log('Form data: ', upload_form_data);
if ($form.length) {
$form.find('input[type=file]').each(function(index, input) {
var $file_field = $(input);
$file_field.fileupload({
fileInput: $file_field,
url: upload_url,
type: 'POST',
autoUpload: false,
formData: upload_form_data,
paramName: 'file',
dataType: 'XML',
add: function(e, data) {
console.log('add callback fired.');
$form.submit(function(e) {
e.preventDefault();
console.log('form submitted.');
data.submit();
});
},
start: function(e) {
console.log('start callback fired');
},
done: function(e, data) {
console.log('done callback fired');
},
fail: function(e, data) {
console.log('fail callback fired');
console.log(e);
console.log(data);
}
});
});
}
});
</script>
这是从 S3 返回的响应:
<Error>
<Code>InvalidArgument</Code>
<Message>Bucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.</Message>
<ArgumentName>key</ArgumentName>
<ArgumentValue></ArgumentValue>
<RequestId>[filtered]</RequestId>
<HostId>[filtered]</HostId>
</Error>
页面加载后,您可以在 javascript 控制台中看到预期的输出:
URL: https://mybucket.s3.amazonaws.com/
Form data: {"key":"attachments/d6313635-9735-4b84-9985-f9f62a036de8/${filename}","policy":"[FILTERED]","x-amz-credential":"[FILTERED]/us-east-1/s3/aws4_request","x-amz-algorithm":"AWS4-HMAC-SHA256","x-amz-date":"20150809T134239Z","x-amz-signature":"[FILTERED]"}
如您所见,有一个关键字段。
当您将文件添加到文件输入字段时,add
回调将按预期触发并绑定表单的提交操作。提交表单后,请求转到 S3,但随后会触发 fail
回调,因为返回了 400。
这个question可能描述了问题所在,但我无法根据提供的信息解决问题。
以下是 request/response 信息,复制自 Chrome 开发工具。
Remote Address:54.231.17.17:443
Request URL:https://mybucket.s3.amazonaws.com/
Request Method:POST
Status Code:400 Bad Request
Response Headers
Access-Control-Allow-Methods:GET, POST, PUT
Access-Control-Allow-Origin:*
Connection:close
Content-Type:application/xml
Date:Sun, 09 Aug 2015 12:29:57 GMT
Server:AmazonS3
Transfer-Encoding:chunked
Vary:Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2:ymrt0MUlhf3bKqVWj+O5jhaUPXNEXy9HQh9PABmqzDkkb4Ods3Hy1LA++8G/Svri3LcOktpnGeE=
x-amz-request-id:545E755033D285F2
Request Headers
Accept:application/xml, text/xml, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:331
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary9vtTme67oAg1OMyL
Host:braidio.s3.amazonaws.com
Origin:http://localhost:3000
Referer:http://localhost:3000/things/new
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
Request Payload
------WebKitFormBoundary9vtTme67oAg1OMyL
Content-Disposition: form-data; name="file"; filename="my_text.txt"
Content-Type: text/plain
------WebKitFormBoundary9vtTme67oAg1OMyL--
如您所见,请求负载仅包含文件,而不包含密钥。可能是文件需要在 post 请求中的所有其他字段之后出现,这就是为什么 S3 没有看到关键字段,如 answer.
中所建议的
一些相关的gem:
* jquery-rails (4.0.4)
* rails (4.2.3)
* aws-sdk (2.1.13)
* aws-sdk-core (2.1.13)
* aws-sdk-resources (2.1.13)
同时使用 jquery.fileupload.js 5.42.3
我不确定如何让它工作。
提前致谢!
我找到了解决方案。
我在前端捕获的在后端生成的表单数据:
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>'
必须从 JSON 字符串转换为 JavaScript 对象:
upload_form_data_obj = JSON.parse(upload_form_data);
显然,$.fileupload
函数需要一个 formData 对象,而不是字符串。
完成该更改后,所需的表单数据将包含在 POST 到 S3 中,并且成功。
这是有效的 javascript 代码:
$(function() {
var $form = $('form.direct_upload'),
upload_url,
upload_form_data,
upload_form_data_obj;
if ($form.length) {
upload_url = '<%= escape_javascript(@presigned_post.url) %>'
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>';
upload_form_data_obj = JSON.parse(upload_form_data);
console.log('URL: ', upload_url);
console.log('Form data: ', upload_form_data_obj);
$form.find('input[type=file]').each(function(index, input) {
var $file_field = $(input);
$file_field.fileupload({
fileInput: $file_field,
url: upload_url,
type: 'POST',
autoUpload: false,
formData: upload_form_data_obj, // needed to be an object, not a string
paramName: 'file',
dataType: 'JSON',
add: function(e, data) {
console.log('add callback fired.');
$form.submit(function(e) {
e.preventDefault();
console.log('form submitted.');
console.log(data);
data.submit();
});
},
start: function(e) {
console.log('start callback fired');
},
done: function(e, data) {
console.log('done callback fired');
},
fail: function(e, data) {
console.log('fail callback fired');
console.log(e);
console.log(data);
}
});
});
}
});
在我的应用程序中,我希望能够将文件直接从浏览器上传到我的 AWS S3 存储桶。我的后端是 rails,但我想避免额外跳到我的服务器,并避免使用 gems,如回形针、载波、carrierwave_direct 等,以保持简单。我大致遵循 heroku 中的这个 tutorial。
我正在使用 aws-sdk gem 和 jquery.fileupload.js 库。
问题是当我尝试上传时,我从 AWS 返回了一个 400 错误请求。
我认为这不是 CORS 问题。我在我的存储桶上配置了 CORS,我可以看到一个成功的 OPTIONS 请求,然后是 POST 文件上传请求,returns 400 错误请求。
以下是复制问题的简化演示。
这是控制器动作。它生成一个 AWS::S3::PresignedPost 对象,因此视图可以使用它 post 文件直接到 S3。
def new
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new('[FILTERED]', '[FILTERED]'),
})
s3 = Aws::S3::Resource.new
bucket = s3.bucket('mybucket')
@presigned_post = bucket.presigned_post(key: "attachments/#{SecureRandom.uuid}/${filename}")
@thing = Thing.new
end
这是视图,new.html.erb
,由上面的呈现,带有上传表单和 javascript 来处理上传。
<div class='container'>
<%= form_for(@thing, html: { class: 'direct_upload' }) do |f| %>
<%= f.label 'Thing' %>
<%= f.file_field :attachment_url %>
<%= f.submit %>
<% end %>
</div>
<script type="text/javascript">
$(function() {
var $form = $('form.direct_upload'),
upload_url = '<%= escape_javascript(@presigned_post.url.to_s) %>',
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>';
console.log('URL: ', upload_url);
console.log('Form data: ', upload_form_data);
if ($form.length) {
$form.find('input[type=file]').each(function(index, input) {
var $file_field = $(input);
$file_field.fileupload({
fileInput: $file_field,
url: upload_url,
type: 'POST',
autoUpload: false,
formData: upload_form_data,
paramName: 'file',
dataType: 'XML',
add: function(e, data) {
console.log('add callback fired.');
$form.submit(function(e) {
e.preventDefault();
console.log('form submitted.');
data.submit();
});
},
start: function(e) {
console.log('start callback fired');
},
done: function(e, data) {
console.log('done callback fired');
},
fail: function(e, data) {
console.log('fail callback fired');
console.log(e);
console.log(data);
}
});
});
}
});
</script>
这是从 S3 返回的响应:
<Error>
<Code>InvalidArgument</Code>
<Message>Bucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.</Message>
<ArgumentName>key</ArgumentName>
<ArgumentValue></ArgumentValue>
<RequestId>[filtered]</RequestId>
<HostId>[filtered]</HostId>
</Error>
页面加载后,您可以在 javascript 控制台中看到预期的输出:
URL: https://mybucket.s3.amazonaws.com/
Form data: {"key":"attachments/d6313635-9735-4b84-9985-f9f62a036de8/${filename}","policy":"[FILTERED]","x-amz-credential":"[FILTERED]/us-east-1/s3/aws4_request","x-amz-algorithm":"AWS4-HMAC-SHA256","x-amz-date":"20150809T134239Z","x-amz-signature":"[FILTERED]"}
如您所见,有一个关键字段。
当您将文件添加到文件输入字段时,add
回调将按预期触发并绑定表单的提交操作。提交表单后,请求转到 S3,但随后会触发 fail
回调,因为返回了 400。
这个question可能描述了问题所在,但我无法根据提供的信息解决问题。
以下是 request/response 信息,复制自 Chrome 开发工具。
Remote Address:54.231.17.17:443
Request URL:https://mybucket.s3.amazonaws.com/
Request Method:POST
Status Code:400 Bad Request
Response Headers
Access-Control-Allow-Methods:GET, POST, PUT
Access-Control-Allow-Origin:*
Connection:close
Content-Type:application/xml
Date:Sun, 09 Aug 2015 12:29:57 GMT
Server:AmazonS3
Transfer-Encoding:chunked
Vary:Origin, Access-Control-Request-Headers, Access-Control-Request-Method
x-amz-id-2:ymrt0MUlhf3bKqVWj+O5jhaUPXNEXy9HQh9PABmqzDkkb4Ods3Hy1LA++8G/Svri3LcOktpnGeE=
x-amz-request-id:545E755033D285F2
Request Headers
Accept:application/xml, text/xml, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:331
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary9vtTme67oAg1OMyL
Host:braidio.s3.amazonaws.com
Origin:http://localhost:3000
Referer:http://localhost:3000/things/new
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36
Request Payload
------WebKitFormBoundary9vtTme67oAg1OMyL
Content-Disposition: form-data; name="file"; filename="my_text.txt"
Content-Type: text/plain
------WebKitFormBoundary9vtTme67oAg1OMyL--
如您所见,请求负载仅包含文件,而不包含密钥。可能是文件需要在 post 请求中的所有其他字段之后出现,这就是为什么 S3 没有看到关键字段,如 answer.
中所建议的一些相关的gem:
* jquery-rails (4.0.4)
* rails (4.2.3)
* aws-sdk (2.1.13)
* aws-sdk-core (2.1.13)
* aws-sdk-resources (2.1.13)
同时使用 jquery.fileupload.js 5.42.3
我不确定如何让它工作。
提前致谢!
我找到了解决方案。
我在前端捕获的在后端生成的表单数据:
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>'
必须从 JSON 字符串转换为 JavaScript 对象:
upload_form_data_obj = JSON.parse(upload_form_data);
显然,$.fileupload
函数需要一个 formData 对象,而不是字符串。
完成该更改后,所需的表单数据将包含在 POST 到 S3 中,并且成功。
这是有效的 javascript 代码:
$(function() {
var $form = $('form.direct_upload'),
upload_url,
upload_form_data,
upload_form_data_obj;
if ($form.length) {
upload_url = '<%= escape_javascript(@presigned_post.url) %>'
upload_form_data = '<%= escape_javascript(@presigned_post.fields.to_json.html_safe) %>';
upload_form_data_obj = JSON.parse(upload_form_data);
console.log('URL: ', upload_url);
console.log('Form data: ', upload_form_data_obj);
$form.find('input[type=file]').each(function(index, input) {
var $file_field = $(input);
$file_field.fileupload({
fileInput: $file_field,
url: upload_url,
type: 'POST',
autoUpload: false,
formData: upload_form_data_obj, // needed to be an object, not a string
paramName: 'file',
dataType: 'JSON',
add: function(e, data) {
console.log('add callback fired.');
$form.submit(function(e) {
e.preventDefault();
console.log('form submitted.');
console.log(data);
data.submit();
});
},
start: function(e) {
console.log('start callback fired');
},
done: function(e, data) {
console.log('done callback fired');
},
fail: function(e, data) {
console.log('fail callback fired');
console.log(e);
console.log(data);
}
});
});
}
});