Skip to content

Instantly share code, notes, and snippets.

@travisofthenorth
Created June 15, 2017 15:58
Show Gist options
  • Save travisofthenorth/5a814f6e37cab08c3583cf88573177f5 to your computer and use it in GitHub Desktop.
Save travisofthenorth/5a814f6e37cab08c3583cf88573177f5 to your computer and use it in GitHub Desktop.
Upload/download files to/from S3
# Upload local file at filepath to <fog_directory>/directory/filename
def upload_file(filepath, directory, filename: nil, acl: 'public-read')
filename ||= File.basename(filepath)
s3 = Aws::S3::Resource.new(region: region)
obj = s3.bucket(bucket).object("#{directory}/#{filename}")
obj.upload_file(filepath, acl: acl)
end
# Download file from <fog_directory>/remote_filepath to local filepath
def download_file(remote_filepath, filepath)
filepath ||= File.basename(remote_filepath)
s3 = Aws::S3::Resource.new(region: region)
obj = s3.bucket(bucket).object(remote_filepath)
# Create local directories since the Aws object doesn't
dirname = File.dirname(filepath)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
obj.get(response_target: filepath)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment