23. Februar 2010

Amazon S3 is a great way to store files that need to be exposed on the web. Every uploaded file can be accessible to the public or accessible to authorized users only via a clever system of expiring signatures.

Joe Danziger wrote an excellent CFC to do all kinds of operations via the S3 REST interface.

The only missing part is the new way Amazon is offering to upload files without the need to first buffer them on your own server.

The old way:

  • You send a user (let's say a user of your CMS) to a page where he can upload a file
  • The user clicks on 'Browse' to find the file on his harddisk and submits the form.
  • The file is sent to your server.
  • You send the file to S3 via a REST put.
  • On success: you save a record in your database so you can remember what you sent to S3

The better way:

  • You send a user to a page where he can upload a file
  • The user clicks on 'Browse' to find the file on his harddisk and submits the form.
  • The file is sent directly to a certain bucket of your Amazon S3 storage
  • Amazon tells you about a successful upload and you update your database.

This is our example:

Save this code as tests3.cfm (or any other name you like) together with the s3.cfc from Joe Danzigers S3 REST Wrapper in a directory.

Set your accessKeyId, secretAccessKey and BucketName in the file and run it.

Feel free to use this example in your own projects. Enjoy :)

In case you want to read a little bit more about the specs check out this article:
Browser Uploads to S3 using HTML POST Forms

UPDATE: Fixed a missing variable in the above code based or Marty McGee's comment.

8 comments for Coldfusion: Amazon S3 upload via Form POST example
  1. Your code samples are broken above.
    23.02.2010 18:02
  2. Patrick
    Did you set your AccessKey, SecretKey and Bucket?
    23.02.2010 18:02
  3. Gary Boyle
    Thats saved me so much pain. Thank you very much.
    23.03.2010 15:03
  4. I'm cannot speak for others, but the exact above code worked out great for me, after a couple tweaks...
    All I needed to do is update the HTML form for the hidden param "acl" and set the value to "#variables.auth#", so that the form submission parameter matches the S3 Policy conditions.
    For uploading different Types of files, make sure to change the "Content-Type" to the correct MIME type prefix -- see MIMEtypes here: http://www.w3schools.com/media/media_mimeref.asp

    Great Post!
    17.04.2010 04:04
  5. Dave Anderson
    Thanks for this post! It was a big help.

    And to anybody having 'access denied' problems -- be sure to read Marty's comment.

    Now if only I could show a progress meter while uploading big files to S3...
    28.05.2010 21:05
  6. Graham Russ
    I have your code working above, which will upload my object into my bucket. My question is how do you upload objects to folders that are located inside of your bucket.

    Any help would be much appreciated.
    14.07.2010 15:07
  7. Graham Russ
    How do you call a folder that is inside of your bucket so you can upload your file into that folder.

    Bucket: MyBucket
    -Folder: Myfolder
    14.07.2010 19:07
  8. Patrick
    Graham,
    there is no concept of folders in S3. There is just objects which you access via a key (the name) inside a very big bucket.

    If you want to put files into a /images folder, you would name the key for the object:

    images/filename1.png

    A file in images/products/cars would have the key:

    images/products/cars/bmw.jpg

    When you request the file, you will not request the file bmw.jpg. You will request the object with the key images/products/cars/bmw.jpg

    So everything works as you expect. Only it's no folders but just names.

    That being said you wonder: But how can i create folders with other tools in S3?
    You can't. They are just simulating folders. Everything is still in the same bucket. They use the above described method.
    15.07.2010 16:07
Comments: Write a comment