Ruby download file from s3






















Improve this answer. Wow, didn't know that imagemagick could download and save files this way, only with the URL, nice! I've got that code in a custom background job that downloads images from S3.

It could be anywhere though. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown. The Overflow Blog. Plus, it doesn't require another action in the controller. Actually I am getting error undefined method creative — Aniket Shivam Tiwari. You have to replace "creative" with the name of your attachment — guilleva.

Community Bot 1 1 1 silver badge. You are awesome! Took this idea and altered it a bit to work for my application: — RubyNewbie. Jonas W 3, 1 1 gold badge 28 28 silver badges 43 43 bronze badges. Jimmy Huang Jimmy Huang 4, 2 2 gold badges 21 21 silver badges 22 22 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Kernel open is a method that you can use to open files, streams, or processes to read to or write from. For example, you can open a file and read its contents with the following code:. We can use this to download an image and then save it as a file. To do so, we first require open-uri then use the open method to access an image URL.

We can then open up a file and write the contents of the image to the file. Open up IRB and try the following:. This is a success, but this was a straightforward example. In practice you would want to handle potential errors, such as a error for a missing image. Plus, there's a bunch of other potential issues with using open-uri.

The thing is, using open-uri like this is not ideal. First up, the above code is not very memory efficient, it loads the entire image into memory and then writes to disk. It also turns out that open-uri has some other quirks.

Notably, open-uri :. To solve all of this, Janko created the Down gem. Serge, I tried your method. It works, however the URL looks like this: s3. What I would like to see ideally is no mention of amazon, but something like " mydomain.

Is this possible? I've updated answer to reflect that. You should set the content-disposition as an attachment in order for it to force the browser to download it instead of trying to open it up in the window.

See this thread for a discussion, their solution is something like this: environment. Community Bot 1 1 1 silver badge. This turned out to be simple, thank you. Serge's one is also good, with the benefit of releasing the Rails app from handling the download, it just needs a bit more work to get it working. Just others know, there are two issues with this I found: 1 It downloads to the server and then to the user, which can take a while; and 2 The experience for the user isn't what they expect.

Instead of getting a download in their browser it looks, instead, like it is loading something. Check out stackoverflow. Sign up or log in Sign up using Google.



0コメント

  • 1000 / 1000