简体   繁体   English

适用于Ruby v2的aws-sdk:在我将对象放入S3存储桶中后检查成功状态

[英]aws-sdk for Ruby v2: check success status after I PUT object in S3 bucket

I'm using the aws-sdk v2 for Ruby and I find the methods available on objects really limiting. 我将aws-sdk v2用于Ruby,发现对对象可用的方法确实存在限制。 I created a bucket like so: 我创建了一个像这样的存储桶:

client = Aws::S3::Client.new(region: 'us-west-2')
s3 = Aws::S3::Resource.new(client: client)
S3_BUCKET = s3.bucket(ENV['AWS_BUCKET'])

I've found that the only methods available to write an object to my bucket is put. 我发现put.了唯一可用于将对象写入存储桶的方法put. However, I don't see a 'success_action_status' available with this method. 但是,我看不到此方法可用的“ success_action_status”。 I've deployed my app to Elastic Beanstalk. 我已将我的应用程序部署到Elastic Beanstalk。 Locally, I can write to this bucket, but when I try and write to my eb app, it's not working and I am working blind trying to figure out what's happening. 在本地,我可以写这个存储桶,但是当我尝试写到eb应用程序时,它不起作用,并且我正在盲目地试图弄清正在发生的事情。 Any info to help determine where my PUT request is going wrong would be helpful. 任何有助于确定我的PUT请求出错的信息都将有所帮助。

Here's what my method looks like now: 这是我的方法现在的样子:

  def create
    username = params[:user][:user_alias]
    key = "uploads/#{username}"
    obj = S3_BUCKET.object(key)

    obj.put({
      acl: 'public-read',
      body: params[:user][:image_uri],
    })

    @user = User.new(user_params)
    if @user.save
      render json: @user, status: :created, location: @user
    else
      render json: @user.errors, status: :unprocessable_entity
    end
  end

Here's the documentation I'm referring to for PUT methods: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#put-instance_method 这是我要用于PUT方法的文档: http : //docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#put-instance_method

It doesn't seem like you're put ing the image file, only the uri. 它似乎并不像你put荷兰国际集团的图像文件,只有URI。 The docs say this about the put method's body option: 文档说了关于put方法的body选项:

body: source_file, # file/IO object, or string data

You'll have to read or open the image file in order to actually upload the image: 您必须阅读或打开图像文件才能实际上传图像:

obj.put({
  acl: 'public-read',
  body: File.open(params[:user][:image_uri], ?r),
})

Then you can check the success status with exists? 然后您可以检查成功状态是否存在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM