Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

536
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda