2014-05-03 170 views
0

我想上傳本地文件到S3 presigned URL。它應該是非常直接的,但看起來像我失去了一些東西。上傳文件到S3 Presigned URL

http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html

實施例:

ENV['RESTCLIENT_LOG'] = "stdout" 
require 'aws-sdk' 
require 'rest_client' 

s3_object_key = "folder-name/file.zip" 

AWS.config(access_key_id: 'xxx', secret_access_key: 'xx') 
s3 = AWS::S3.new 
bucket = s3.buckets['my-bucket-name'] 
s3_object = bucket.objects[s3_object_key] 

upload_url = s3_object.url_for(:put, expires: 100000).to_s 

RestClient.put(upload_url, file: File.new("local-file.zip")) 

日誌:

RestClient.put "https://s3.amazonaws.com/my-bucket-name/folder-name/file.zip?AWSAccessKeyId=xxx&Expires=xxx&Signature=xxx", 246572 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"246572", "Content-Type"=>"multipart/form-data; boundary=183013" 

響應:

/Users/dev/.rbenv/versions/2.1.1/lib/ruby/2.1.0/openssl/buffering.rb:326:in `syswrite': Broken pipe (Errno::EPIPE) 
    from /Users/dev/.rbenv/versions/2.1.1/lib/ruby/2.1.0/openssl/buffering.rb:326:in `do_write' 
    from /Users/dev/.rbenv/versions/2.1.1/lib/ruby/2.1.0/openssl/buffering.rb:344:in `write' 

任何幫助將非常感激。

回答

2

什麼這裏使用PresignedPost

form = bucket.presigned_post(:key => "photos/${filename}") 
form.url.to_s  # => "https://mybucket.s3.amazonaws.com/" 
form.fields   # => { "AWSAccessKeyId" => "...", ... } 
form.url    # your signed url 
+0

感謝。這種方法奏效。 –

0

你不能簡單地PUT S3上的文件。使用write API來做到這一點:

s3_object.write(:file => "local-file.zip") 
+1

用例是,對於一個PUT請求預先簽署的網址是給另一個系統。該系統不知道AWS的信用。是否有該系統將文件上傳到PUT URL的方式 - http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.html –