简体   繁体   中英

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. 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. However, I don't see a 'success_action_status' available with this method. I've deployed my app to 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. Any info to help determine where my PUT request is going wrong would be helpful.

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

It doesn't seem like you're put ing the image file, only the uri. The docs say this about the put method's body option:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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