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.
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!
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...
Any help would be much appreciated.
Bucket: MyBucket
-Folder: Myfolder
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.