Search code examples
c#asp.netasp.net-mvcamazon-s3uploadify

Uploadify send directly to S3 and save filename to database


I need to use uploadify to allow users to upload files which will be stored in Amazon S3.

I believe it is possible to upload directly to S3 but I also need to do some processing on my server.

What I need to do is generate a unique filename for the video before upload and then save the filename to my database after a successful upload.

Can anyone provide any insight on how to accomplish this?


Solution

  • We also do this in our application.

    What I need to do is generate a unique filename for the video before upload and then save the filename to my database after a successful upload.

    I think you've got that the wrong way around.

    We have a generic Post table where we store all types of Posts, of which Photo is one.

    So when a user uploads a photo, we first save the photo so it get's a unique identifier (e.g the IDENTITY field in the DB - PK), then we use that as the filename.

    That way we can use convention to render the S3 links - e.g /s3.amazonaws.com/yourbucket/photoid.jpg.

    So your controller would look something like this:

    // might need to change to HttpPostedFileBase[] for multiple file uploads
    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase, string photoName) 
    {
       var stream = httpPostedFileBase.InputStream;
       var photo = new Photo { Name = photoName };
       repository.Save(photo);
       s3service.Upload(stream, photo.Id);
    }
    

    Are you allowing the user to enter a name/tags/other input of some sort? If so, you could create unique slugs on the server based on this name, then use that as the filename (good for SEO).