Using Amazon S3 with Google App Engine for Java

Posted on : 2011-02-15 | By : Sam Edwards | In : App Engine, Tips

Tags: , , , , , ,

0

Update: (2011.03.16) – Github Link Updated

Amazon S3 is a fantastic way to store static files, but when you try to use their Java SDK on Google’s App EngineFAIL!

Soo, after a day of hitting my head against the wall, I finally had some success by combining multiple projects. The result: s3-simple-appengine on GitHub

The code is a mix of these libraries:

jets3t and Amazon’s SDK are great but they use the Apache Commons http client which doesn’t work 100% on Google App Engine. This is due to restrictions… namely, no threads, no sockets, and… other restrictions…

The library I’ve thrown together allows me to do “PUT” requests and upload blobs from the datastore, as well as create signed URLs which are the two things I needed currently.

//Create an S3Store Object and Set the Current Bucket
S3Store s3 = new S3Store(Constants.S3_DEFAULT_HOSTNAME, ACCESS_KEY, SECRET_KEY);
s3.setBucket("my-bucket");

//Sign an S3 URL
String signedUrl = s3.createSignedGETUrl(objectKey, validForInSeconds, isHttps);

//Upload Binary Data to S3 (i.e. From the AppEngine Blobstore, etc)
final Map<String, List<String>>() headers = new HashMap<String, List<String>>();
headers.put("Content-Type", contentType);
s3.storeItem(key, bytes, Constants.ACL_PUBLIC_READ, headers);

Write a comment