Serving static websites from S3 through Rails - ruby-on-rails

I am trying to create a rails app that will run on my main domain, which will have a bunch of subdomains that will all be separate static websites.
I want the files for these static sites to each be in their own folder on an S3 bucket I created. So, going to mysite.example.com you would see the index.html file in the mysite folder on S3. The user's browser would also have to be able to pull any of the CSS and images referenced in index.html
Is there any way to do this? If possible I would like to do this off of Heroku, but that might not be feasible.

The right_aws gem might be exactly what you're looking for; it claims to offer S3 and EC2 support, among other services.

Related

How to use File.new() in Rails app to create a file?

I would like to create some_javascript_file.js after a user submits a form and save this file in the public directory of my app.
When testing this localy I can simply use File.new("./public/some_javascript_file.js", "w") to accomplish this but this solution doesn't work when the app is deployed to Heroku.
Please would you have any suggestions? Thank you
This is because of how Heroku works. Basically, as stated in the docs, the filesystem is ephemeral and you can't rely on it. If you want persistence, you should upload this JS file to some external service, such as AWS S3. Or you might want to deploy your app to a different environment, such as self-managed VPS, where filesystem is real and your file won't disappear.

Best AWS Storage Option for Exporting Directories as .zip Files?

I'm brand new to AWS products, ruby on rails, web development, and coding of any type. For my first project after a quick (and dirty) bootcamp, I'm trying to build a ruby-on-rails website that stores images and allows the user to download them as a zip file. I used the RubyZip gem to accomplish this in my EC2 dev environment, but I have deployed to Elastic Beanstalk with S3 file storage, and the RubyZip gem seems unable to handle this structure without traditional directory targets for zipping.
My question is what's the best setup to achieve this functionality in EB? Disregarding the ruby constraint, zipping an S3 directory seems tricky. Should I move to EFS or another storage system? I plan to erase the folders regularly, and limit them to ~100 photos, so long term and large size storage are not a concern. Thanks very much!
Edit: I am attached to Ruby (only language I know), but not RubyZip, AWS, or much of anything else if they are not the best approach for this task.
I think you're on the right path as far as using S3 as a solution. The problem that your facing is that when you're interacting with S3 it's not like a folder on your local system, instead you're hitting the S3 API to interact with the files. (upload, edit, delete ect). This will be a problem you'll encounter with every AWS based storage solution.
I think the solution, in your case, is to fetch all of the photos and download them to a temporary folder on your local system. Then, you could zip them using Ruby, locally. After it's zipped, upload it back to S3.
Edit: By locally I mean onto the server where the Ruby application is running (not client-side)

Upload .mp3 file to public URL within Rails 4

I am using Rails 4.0.0 and Ruby 2.0.0, and I have an app deployed to Heroku.
I want to give my users the ability to upload a mp3 file. After it is completed uploading, I need them to get access to the public URL of that mp3 file. Right now, I could upload the recordings myself in my public directory, and then I could access them at a public URL.
I need to replicate that ability for my users. Any thoughts on the best way to do that?
Heroku generally doesn't want you to let people upload files onto the Heroku filesystem via your website. You need to use a third party file storage system. Most people use Amazon S3, and there are loads of detailed tutorials on how to use this with heroku (including on the heroku site). Google for "heroku amazon s3" and you'll see loads of helpful stuff, eg
https://devcenter.heroku.com/articles/s3

How do I setup a route as an alias for Amazon S3?

I'm hosting my rails app with heroku and managing my DNS with ZerigoDNS (the free heroku addon). I use Amazon S3 to store all of my assets.
I have a public folder in my S3 bucket called newsletter.
I'd like http://mydomain.com/newsletter to simply act as an alias to https://my-bucket.s3.amazonaws.com/newsletter.
How would I go about doing that?
Thanks!
Edit: preferably, I'd like the URL to not redirect but instead just read from the S3 folder. So I'd basically just set up an alias for https://my-bucket.s3.amazonaws.com/newsletter. Is there a way to do that using routes.rb or some gem?

Paperclip: migrating from file system storage to Amazon S3

I have a RoR website, where users can upload photos. I use paperclip gem to upload the photos and store them on the server as files. I am planning to move to Amazon S3 for storing the photos. I need to move all my existing photos from server to Amazon S3. Can someone tell me the best way for moving the photos. Thanks !
You'll want to log into your AWS Console and create a bucket structure to facilitate your images. Neither S3 nor Paperclip have any tools in the way of bulk migrations from file system -> s3, you'll need to use the tool s3cmd for that. In particular, you're interested in the s3cmd sync command, something along the lines of:
s3cmd sync ./public/system/images/ s3://imagesbucket
If you have any image urls hard-coded into your database (a la markdown/template code) this might be a little tricky. One option would be to manually update your urls to point to the new bucket. Alternatively, you can rack-rewrite.
You can easily do this by creating a bucket on Amazon S3 that has the same folder structure as your public directory on your Rails app.
So say for instance, you create a new bucket on Amazon S3 called MyBucket and it has a folder in it called images. You'd just move all of your images within your Rails app's images folder over to that new bucket's images folder.
Then you can set up your app to use an asset host like this answer describes: is it good to use S3 for Rails "public/images" and there an easy way to do it?
If you are using image_tag or other tag helpers (javascripts, stylesheets, etc), then it will use that asset_host for production environments and properly generate the URL to your S3 bucket.
I found this script which takes care of moving the images to Amazon S3 bucket using rake task.
https://gist.github.com/924617

Resources