Created
April 20, 2014 01:54
-
-
Save apprentice1988/11102877 to your computer and use it in GitHub Desktop.
Copy a Remote File to S3 with Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
undefined method `store' for AWS::S3::S3Object:Class