Search code examples
ruby-on-railsfile-uploadcloud-hosting

Why would you upload assets directly to S3?


I have seen quite a few code samples/plugins that promote uploading assets directly to S3. For example, if you have a user object with an avatar, the file upload field would load directly to S3.

The only way I see this being possible is if the user object is already created in the database and your S3 bucket + path is something like

user_avatars.domain.com/some/id/partition/medium.jpg

But then if you had an image tag that tried to access that URL when an avatar was not uploaded, it would yield a bad result. How would you handle checking for existence?

Also, it seems like this would not work well for most has many associations. For example, if a user had many songs/mp3s, where would you store those and how would you access them.

Also, your validations will be shot.

I am having trouble thinking of situations where direct upload to S3 (or any cloud) is a good idea and was hoping people could clarify either proper use cases, or tell me why my logic is incorrect.


Solution

  • Why pay for storage/bandwidth/backups/etc. when you can have somebody in the cloud handle it for you?

    S3 (and other Cloud-based storage options) handle all the headaches for you. You get all the storage you need, a good distribution network (almost definitely better than you'd have on your own unless you're paying for a premium CDN), and backups.

    Allowing users to upload directly to S3 takes even more of the bandwidth load off of you. I can see the tracking concerns, but S3 makes it pretty easy to handle that situation. If you look at the direct upload methods, you'll see that you can force a redirect on a successful upload.

    Amazon will then pass the following to the redirect handler: bucket, key, etag

    That should give you what you need to track the uploaded asset after success. Direct uploads give you the best of both worlds. You get your tracking information and it unloads your bandwidth.

    Check this link for details: Amazon S3: Browser-Based Uploads using POST