Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

537
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda