Serving varied content from Amazon S3
One question that I see a lot is "How do I get my file of type 'blah' to be served properly from S3?"
The answer, it turns out is fairly simple. Each object in S3 has metadata associated with it. When you upload the file, you can set that metadata, including the Content-Type header. For example, XML files use; Content-Type: text/xml
If you have something like an .swf file, you'd use;
Content-Type: application/x-shockwave-flash
or for a mp4 video file;
Content-Type: video/mp4
To do this using the Jets3t java client, you'd write some code that looks like this;
RestS3Service s3 = getS3Service();
S3Bucket bucket = new S3Bucket(bucketName);
S3Object obj = new S3Object(bucket, objectId);
obj.setContentType(mimeType);
obj.setContentLength(fileSize);
obj.setDataInputStream(iStr);
obj = s3.putObject(bucket, obj);
This code also sets the content length, so the file being uploaded is streamed to S3, not buffered in memory.
Jets3t also has a pretty handy class: org.jets3t.service.utils.Mimetypes, which helps you get the mime type for a given file or filename.
- dkavanagh's blog
- Login or register to post comments
rawsug.org