简体   繁体   中英

Best way to transload a file in rails (from HTTP URL to S3)

In Rails, what is the best (ie. efficient, elegant) way to download a file from a public HTTP url, upload to Amazon S3 and delete the file in the server.

I am using Heroku so I have the additional restriction of a ephemeral file system.

The answer is to read your image into memory instead of storing it on the filesystem. Here's an example.

s3 = Aws::S3::Resource.new
obj = s3.bucket(your_bucket_name).object(your_object_key)

s3_put_url = URI.parse(obj.presigned_url(:put))

image_url = 'http://www.google.com/google.jpg'   
image_file = open(image_url).read

Net::HTTP.start(s3_put_url.host) { |http| http.send_request('PUT', s3_put_url.request_uri, image_file); }

# Let's get the URL
s3.bucket(your_bucket_name).object(your_object_key).presigned_url(:get)

If reading into memory isn't an option. You can use the /tmp directory. As long as this is in the same process using that shouldn't be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM