Skip to content

Instantly share code, notes, and snippets.

@apprentice1988
Created April 20, 2014 01:54
Show Gist options
  • Save apprentice1988/11102877 to your computer and use it in GitHub Desktop.
Save apprentice1988/11102877 to your computer and use it in GitHub Desktop.
Copy a Remote File to S3 with Ruby
require 'net/http'
AWS::S3::Base.establish_connection!({
access_key_id: 'your-access-key-id',
secret_access_key: 'your-secret-access-key'
})
# Use the Google Logo as an example
#
url = URI("https://www.google.com/images/srpr/logo3w.png")
Net::HTTP.start(url.host) do |http|
resp = http.get(url.path)
AWS::S3::S3Object.store(File.basename(url.path), resp.body, 'yourbucketname', access: :public_read)
end
@eliduke
Copy link

eliduke commented Mar 30, 2015

undefined method `store' for AWS::S3::S3Object:Class

@aaronmtate
Copy link

This is with the old SDK. Version 2 would look more like this:

s3 = Aws::S3::Resource.new(region: AWS_REGION)
bucket = s3.bucket('yourbucketname')

# Use the Google Logo as an example
#
url = URI("https://www.google.com/images/srpr/logo3w.png")

object_name = File.basename(url.path)

Net::HTTP.start(url.host) do |http|
  resp = http.get(url.path)
  bucket.object(object_name).put({ acl: "public-read", body: resp.body })
end

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