问题
I'm trying to upload image to Amazon S3 with this Ruby code:
require 'net/http/post/multipart'
url = URI.parse('http://public.domain.com/')
File.open("/tmp/uup_1114.jpg") do |jpg|
req = Net::HTTP::Post::Multipart.new url.path,
'key' => s3_key,
'acl' => s3_acl,
'content_type' => s3_content_type,
'AWSAccessKeyId' => s3_AWSAccessKeyId,
'policy' => s3_policy,
'signature' => s3_signature,
"file" => UploadIO.new(jpg, "image/png", "image.jpg")
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
And I'm getting error from Amazon:
InvalidArgument: Bucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.
Looks like 'file' field goes first in query and that causes an error above. I can't figure out how to post file field at the end of query.
回答1:
I have successfully used the AWS-SDK in ruby to create post forms. But in my case I was getting users to upload from a browser into an AWS account. Still this may help:
the aws-sdk has a call on a bucket called presigned_post(options) that creates a pre signed post that works fine.
See also https://forums.aws.amazon.com/thread.jspa?messageID=296867񈞣
回答2:
It's better to use AWS::S3 (http://amazon.rubyforge.org/)
and S3Object
If you're experiencing some problems try to check if your local computer time is valid (it's really important) and try setting
AWS::S3.const_set('DEFAULT_HOST', "s3-eu-west-1.amazonaws.com")
if you're working with bucket(s) located in Europe.
来源:https://stackoverflow.com/questions/9311128/post-upload-request-to-amazon-fails