Skip to content

Instantly share code, notes, and snippets.

@moreta
Created November 15, 2018 06:37
Show Gist options
  • Save moreta/8ec24b772b5b99acfaa0af1705d35148 to your computer and use it in GitHub Desktop.
Save moreta/8ec24b772b5b99acfaa0af1705d35148 to your computer and use it in GitHub Desktop.
ruby image base64 string upload to s3
class CustomFileStringIO < StringIO
def initialize(*args)
super(*args[1..-1])
@file_name = args[0]
end
def original_filename
@file_name
end
end
image_base64 = '"data:image/png;base64,xxxxx'.split(',')
file_io = CustomFileStringIO.new(image_file_name, Base64.decode64(image_base64[1]))
obj = S3_BUCKET.object(file_io.original_filename)
obj.put({ body: file_io })
@oamado
Copy link

oamado commented Apr 23, 2021

require 'aws-sdk-s3'

def upload_file
s3 = Aws::S3::Client.new(
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: ENV['AWS_REGION']
)
service = Aws::S3::Resource.new(client: s3)

object_key = 'filename'
object = service.bucket(ENV['AWS_S3_BUCKET']).object(object_key)
object.put(body: 'file content')

s3.put_object_acl(bucket: ENV['AWS_S3_BUCKET'], key: object_key, acl: 'public-read')

object.public_url.to_s

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment