Rails download file from s3
I prefer setting default settings that are loaded when your application loads:. As you can probably tell, the section that we've added here to the default application. If you have more than one Paperclip class in your application, you might not want to go with this approach, or you can just override any settings in another class if you need.
Most of the stuff in here is pretty self-explanatory. It's all our settings that we have set up in our. Now that we have our configuration to connect Paperclip with S3, we need to tell the Image class what it needs to know to use it. Let's have a look at the changes we need:.
Let's go through the changes that we have here. These let our app know where to put our image when we upload it, and how what URL to use after it is uploaded.
In 10 seconds that URL will no longer work, and anyone using it will get an Access denied message from S3. Is the same, except we are giving someone a little longer to sort out their download, and it is explicitly asking for the original. We could write a method that would allow us to both displays an Image and use it for download, but by having two it is very clear what is for what and should avoid the possibility of serving an original Image instead of a thumbnail in the case that only one style is for purchase and the others are for marketing.
So we can remove:. So let's use it, we need to change the pages that show our image thumbnails to use them:. All that we have left now is to have our User be able to download their purchases. We have two options for this. Once you logged in AWS, you should create a policy and attach it to your account, and then create access key.
I'm not going to talk about all the detail of how to setup for AWS, so if you want to know how to do this, please refer to the AWS Official Documentation. Also, please make sure that you installed aws-sdk in your project. To install aws-sdk, you can simply use npm package manager to do below. And here is the simple code to generate the download URL. First, you need to create S3 bucket object.
The events that you may be interested in during the download are close when the stream is finished, meaning download is done , data receiving data chunk - downloading process , and error when failed to download.
There must be something I'm not understanding or including. You look to be downloading the file from s3 just fine into file. The call to File. Alternatively you can generate a signed link and redirect the user to that.
This avoids having to download the file to your server at all. With v2 of the sdk I believe you need to use the slightly higher level resource interface for this:.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 6 years, 1 month ago. Users upload multiple files direct to Amazon S3 im using carrierwave.
I'd like Users to have the abililty to download a Projects datafiles as a single zip file. Im trying to figure out the best strategy to implement this feature. Here are the ideas I've come up with so far:. Strategy 1: Rails creates a zip file and streams the zip to the user. Strategy 2: A background job later re-downloads the files to my server, creates a zip and reuploads to S3. Users will then be able to download the zip directly from s3 if it exists. Strategy 4: Limit users to a single file upload per project, forcing the end users to zip their files.
I'd love to hear some input on how you'd go about this, or any resources for someone that has done something similar. This is a good challenge Justin! I've thought about this in the past but never needed to implement it. Dropbox takes the background process approach, while Slack takes the approach of no zipping files and just doing one at a time.
It's hard read: impossible to come up with a single solution to this without knowing more details like the average file sizes, types and percentages of requests, and things. If people are uploading small documents that's one thing. If they're uploading videos, that's another. If you don't know the average usage before you get into this, then I would strongly encourage you to take the simplest approach first rubyzip and implement that.
You could run this all on the webserver at first and then move it to a dedicated server for processing if it starts taxing your webserver. You can measure the free disk space before a job and what you'll need for the zip before executing it.
That could tell you to either run it locally or on a dedicated EC2 instance for a few minutes. Once you get a user that's breaking the capabilities of one solution, then you can implement a more complex one and scale that out as you gather more usage measurements. This will also come down to keeping an idea of what percentage of requests are uploads vs downloads.
0コメント