Skip to content

Instantly share code, notes, and snippets.

@luaysuarna
Created June 17, 2021 09:22
Show Gist options
  • Save luaysuarna/e0914dfa6818aba64013fe3a61e07541 to your computer and use it in GitHub Desktop.
Save luaysuarna/e0914dfa6818aba64013fe3a61e07541 to your computer and use it in GitHub Desktop.
Ruby aws-sdk upload public image
config = {
region: Settings.aws.upload.region,
bucket: Settings.aws.upload.bucket_assets_name,
key: Settings.aws.access_key_id,
secret: Settings.aws.secret_access_key
}
Aws.config.update({
region: config[:region],
credentials: Aws::Credentials.new(config[:key], config[:secret])
})
s3 = Aws::S3::Client.new(
access_key_id: config[:key],
secret_access_key: config[:secret]
)
file_name = File.basename(file_path)
bucket = Aws::S3::Resource.new.bucket(config[:bucket])
key = "#test/key/#{file_name }"
# Upload
resp = nil
File.open(file_path, 'rb') do |file|
puts "start uploading #{file_name} to s3"
resp = s3.put_object(bucket: config[:bucket], acl: "public-read", key: key, body: file)
puts resp
end
object = bucket.objects.to_a.select { |a| a.key.include?('public') }.first
object.public_url # will return public url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment