Skip to content

Instantly share code, notes, and snippets.

@hkraji
Created February 5, 2018 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkraji/8e63d3b25d131cf03fe9806af5dca319 to your computer and use it in GitHub Desktop.
Save hkraji/8e63d3b25d131cf03fe9806af5dca319 to your computer and use it in GitHub Desktop.
Connect to S3 and Create files
require 'rake'
require 'ap'
namespace :gazzda_s3 do
desc 'Upload images from directory to specified s3 bucket'
task :upload, [:bucket] do |t, args|
path_to_images = '/Users/haris/rails/scm_gazzda/image-files'
bucket = args[:bucket]
connection = Fog::Storage.new({ :provider => "AWS", :aws_access_key_id => ENV['AWS_KEY_ID'], :aws_secret_access_key => ENV['AWS_KEY_SECRET'], :region => 'eu-central-1' })
directory = connection.directories.get(bucket)
Dir.glob("#{path_to_images}/**/*").each do |entry|
if File.file?(entry)
short_path = entry.gsub(path_to_images, '')
print "Creating ... #{short_path} ..."
file = directory.files.create(
:key => short_path,
:body => File.read(entry)
)
puts 'Finished!'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment