如何使用ruby检索对AWS :: S3 :: MultipartUpload的引用

时间:2014-09-11 02:29:41

标签: ruby-on-rails ruby amazon-web-services amazon-s3 multipartform-data

我有一个rails应用程序,在控制器操作中,我可以像这样创建一个分段上传:

  def create
    s3 = AWS::S3.new
    bucket = s3.buckets["my_bucket"]
    key = "some_new_file_name.ext"
    obj = bucket.objects[key]
    mpu = obj.multipart_upload
    render json: {
      :id => mpu.id
    }
  end

所以现在客户端具有分段上传ID,她可以使用她的浏览器将部件上传到aws。我希望创建另一个动作,一旦完成上传,就会组装这些动作。类似的东西:

  def assemble
    s3 = AWS::S3.new
    bucket = s3.buckets["my_bucket"]
    key = params['key']
    bucket.objects[key].multipart_upload.complete
    render json: { :status => "all good" }
  end

这不行。如何检索对multipartUpload对象的引用或使用key或id创建一个新对象,以便我可以在其上调用“完整”方法?任何见解都表示赞赏

1 个答案:

答案 0 :(得分:0)

我在Client类的文档中找到了这个方法,并使其工作如下:

client = AWS::S3::Client.new

# reassemble partsList
partsList = []
params[:partsList].each do |key, pair|
  partsList.push pair
end

options = {
  :bucket_name => 'my_bucket',
  :key => params[:key],
  :upload_id => params[:upload_id],
  :parts => partsList
}
client.complete_multipart_upload(options)