Rails proxy redirection for downloading file from s3






















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. How to proxy files from S3 through rails application to avoid leeching? Ask Question. Asked 11 years ago.

Active 5 years, 7 months ago. Viewed 4k times. Update: If you have any other ideas which can reduce S3 bill and prevent hot-linking leeching in anyway please share, even if they are not directly related to Rails. Improve this question. Are you currently having a problem with leeching? I would be reluctant to do something that will dramatically slow my application, just to solve a problem I might have in the future.

It's not only a matter of solving a potential problem. I just don't want to wake up one day with overwhelming S3 bill, that I cannot afford to pay Add a comment. Active Oldest Votes. Use a private bucket private files and use signed URLs to the files stored on S3. Improve this answer. Peter Brown Justin Justin 51 1 1 silver badge 1 1 bronze badge.

Brian Low Brian Low This is smart, redirecting so that if you put the links in emails or somewhere, they will alway get a new link with new expiry once redirected.

I'd probably avoid to do this -- at least until I'd have no other choice. 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. We can allow the browser to link to our file on S3 inline and try to display it in the browser, or we can force it to download to our filesystem.

Depending on what you are building, either might be appropriate, so let's take a look at both of them. If you want to go this route, you can remove the following code:. As we won't be serving the file download, this means that we can get rid of the code that does. You don't want dead code lying around your application. It's confusing when you try to come back to your app. Just remove it and bring it back with source control if you need to.

If you want to make sure that the file downloads to the user's computer rather than downloads in the browser the change we want to make is a little different. This time we will be keeping the route and controller action. In this, we are displaying our thumbnail from S3, and then we are linking to the download action on our images controller. We already have the route hooked up. We just need to change how we serve the file.

This will initiate a download with their browser, and they will get their file. There are tradeoffs in using this method of downloading though. The file will get downloaded to your server and then passed off to the user. If these are big files, then that could be a problem as they will experience a delay, and it will block an instance of your application while it is downloading.

If they are small files, then this should be fine. As ever, different things work well in different situations. Active Oldest Votes. Improve this answer. This is a possibility but I would prefer to find some sort of streaming if possible.

Also, even if the file is stored on S3, it is on a public URL so I do not need to use the S3 library to get the file, not sure if there is reason to choose to get the file from the S3 lib or directly by the URL?

Do you have some idea with that? Thanks for your help. See apidock. As for dynamically determining the content type -- either store the content type in a model on the app server, in the filename or path of the s3 object, or in the metadata for the s3 object. 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. Podcast what if you could invest in your favorite developer?

Who owns this outage? Building intelligent escalation chains for modern SRE.



0コメント

  • 1000 / 1000