【问题标题】:Uploading a file to a S3 Presigned URL将文件上传到 S3 预签名 URL
【发布时间】:2014-05-03 11:43:51
【问题描述】:

我正在尝试将本地文件上传到 S3 预签名 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'

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby amazon-web-services amazon-s3 rest-client


    【解决方案1】:

    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
    

    【讨论】:

      【解决方案2】:

      您不能简单地 PUT S3 上的文件。使用write API 来做到这一点:

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

      【讨论】:

      猜你喜欢
      • 2020-09-30
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 2022-08-20
      • 2019-01-19
      • 2019-08-05
      • 1970-01-01
      • 2016-10-12
      相关资源
      最近更新 更多