Skip to content

Instantly share code, notes, and snippets.

@db42
Created February 9, 2018 12:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save db42/c94756ee3b45b7a2472bc4ded1f376e1 to your computer and use it in GitHub Desktop.
Save db42/c94756ee3b45b7a2472bc4ded1f376e1 to your computer and use it in GitHub Desktop.
Ruby script to upload a file to amazon s3
require 'aws-sdk-s3' # v2: require 'aws-sdk'
ACCESS_KEY_ID = ""
SECRET_ACCESS_KEY = ""
REGION_ID = "us-east-1"
BUCKET_NAME = "bucket-name"
def uploadS3
credentials = Aws::Credentials.new(
ACCESS_KEY_ID,
SECRET_ACCESS_KEY
)
s3 = Aws::S3::Client.new(
region: REGION_ID,
credentials: credentials
)
#Upload export/file1.zip, export/file2.zip
for file_name in ['file1.zip', 'file2.zip']
name = File.join('export', file_name)
# Upload
File.open(name, 'rb') do |file|
puts "start uploading #{file_name} to s3"
resp = s3.put_object(bucket: BUCKET_NAME, acl: "public-read", key: file_name, body: file)
puts resp
end
puts "File should be available at https://#{BUCKET_NAME}.s3.amazonaws.com/#{file_name}"
end
end
@ahtasham067
Copy link

Thanks it help me!

@Cristopheer96
Copy link

i tried to do it for an xlsx file and it doesn't work.
I tried with a .txt file and it works.
any suggestions?

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