Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

528
Views
Why is AWS uploading literal file paths, instead of uploading images?

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 https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/s3-example-upload-bucket-item.html

def object_uploaded?(s3_client, bucket_name, object_key)
    response = s3_client.put_object(
      body:   "tmp/cosn_img.jpeg", # is always interpreted literally
      acl:    "public-read",
      bucket: bucket_name,
      key:    object_key
    )
    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
    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?(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

This works and is able to upload to AWS, but it is uploading just the file path from the body, not the actual file itself.

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. https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#put_object-instance_method

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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

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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!