Home > Mobile >  Why is AWS uploading literal file paths, instead of uploading images?
Why is AWS uploading literal file paths, instead of uploading images?

Time:09-16

TL;DR

How do you input file paths into the AWS S3 API Ruby client, and have them interpreted as images, not string literal file paths?

More Details

I'm using the Ruby AWS S3 client to upload images programmatically. I have taken this code from their example startup code and barely modified it myself. See enter image description here

file path shown when you click on attachment link

As far as I can see from the Client documentation, this should work. enter image description here Client docs

Also, manually uploading this file through the frontend does work just fine, so it has to be an issue in my code.

How are you supposed to let AWS know that it should interpret that file path as a file path, and not just as a string literal?

CodePudding user response:

Their docs seem a bit weird and not straigtforward, but it seems that you might need to pass in a file/io object, instead of the path.

The ruby docs here have an example like this:

s3_client.put_object(
  :bucket_name => 'mybucket',
  :key => 'some/key'
  :content_length => File.size('myfile.txt')
) do |buffer|
  File.open('myfile.txt') do |io|
    buffer.write(io.read(length)) until io.eof?
  end
end

or another option in the aws ruby sdk docs, under "Streaming a file from disk":

File.open('/source/file/path', 'rb') do |file|
  s3.put_object(bucket: 'bucket-name', key: 'object-key', body: file)
end

CodePudding user response:

You have two issues:

  1. You have commas at the end of your variable assignments in object_uploaded? that are impacting the way that your variables are being stored. Remove these.
  2. You need to reference the file as a File object type, not as a file path. Like this:
image = File.open("#{Rails.root}/tmp/cosn_img.jpeg")

See full code below:

def object_uploaded?(image, s3_client, bucket_name, object_key)
  response = s3_client.put_object(
    body:   image,
    acl:    "public-read",
    bucket: bucket_name,
    key:    object_key
  )
  puts response
  if response.etag
    return true
  else
    return false
  end
rescue StandardError => e
  puts "Error uploading object: #{e.message}"
  return false
end

Full example call:

def run_me
  image = File.open("#{Rails.root}/tmp/cosn_img.jpeg")
  bucket_name = 'cosn-images'
  object_key =  "#{order_number}-trello-pic_#{list_config[:ac_campaign_id]}.jpeg"
  region = 'us-west-2'
  s3_client = Aws::S3::Client.new(region: region)

  if object_uploaded?(image, s3_client, bucket_name, object_key)
    puts "Object '#{object_key}' uploaded to bucket '#{bucket_name}'."
  else
    puts "Object '#{object_key}' not uploaded to bucket '#{bucket_name}'."
  end
end
  • Related