checked Geen minimaal aantal vereist
checked Wereldwijd printen en verzenden
checked Integreer Peecho met je website
checked Peecho is een gratis service

18-11-2010

Java S3 upload using Spring RestTemplate

At Peecho, we use many of the AmazonAWS services. For example, we use EC2for our virtual machines and S3for all of our storage. Because of the scalable nature of S3,theoretically, we could serve an infinite amount of users uploadingfiles to our platform without stressing our machines or infrastructureat all. The only drawback is that connected apps will have to uploadtheir files directly to S3 – which can be challenging at times. That’swhy I’m writing this blog.First of all, the Spring RestTemplate classis awesome. It is a really neat and easy way to create requests torestful web services or even not so restful services. The cool thing isthat you can configure marshallers on the template, which willautomatically convert outgoing and incoming objects into XML, Json andmore. For example, you can configure an XStreamMarshaller tomarshall all outgoing objects into XML and all incoming XML into objectsthis way.Uploading to S3 can be really easy if you use one ofthe many libraries that Amazon provides for the different platforms likeJava, .Net, PHP etcetera. These libraries have easy-to-use methods toupload files to buckets, creating objects and setting policies. To makeuse of all this, you need an Amazon public and secret key, which is fineif you are uploading to your own S3 account. We need our customers to beable to upload to our S3 account and naturally we can’t giveour customers the secret key to our amazon account because they could doall kinds of nasty evil stuff with it.Luckily, Amazonprovides us with a way to upload files to S3 using a pre-signed url.This url contains a base64 encoded policy file, some paths to the dataand a signed hash of the entire url – using your secret key. The policyfiles specify exactly what and where you can upload your data. Forexample, it specifies you can only upload *.jpg files to the/user-data/username/* path in S3. This file is generated on our server,using our secret key. This way customers of our API can only upload indirectories that we specify and tampering with other customer’s files isimpossible. Doing browser based uploads using a pre-signed urlis explained in thisS3 article.Now, we have a signed url to post to and a validpolicy file – but we still need to actually upload the data. This iswhere the rest template comes in. S3 expects a multi-part formpost, instead of a normal file upload. Luckily there is a MessageConverterin Spring to create multi-part form posts! Configure it in yourapplication context like this:<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"><property name="messageConverters"><list><bean class="org.springframework.http.converter.StringHttpMessageConverter" /><bean class="org.springframework.http.converter.FormHttpMessageConverter" /></list></property></bean>The FormHttpMessageConverter makesit possible to create a multi-part form post. In your Java code you cannow create the request:MultiValueMap<String, Object> form= new LinkedMultiValueMap<String, Object>(); form.add("key", objectKey);form.add("acl","private");form.add(“Content-Type”,”image/jpeg”);form.add(“AWSAccessKeyId”, awsAccessKeyId);form.add(“Policy”,serverGeneratedPolicy);form.add(“Signature”,serverGeneratedSignature);form.add(“Filename”, “”);form.add(“success_action_status”,”201″);form.add(“file”, new FileSystemResource(file));restTemplate.postForLocation(signedPutUrl,form); When just providing a map with only strings, the converter will convertit into a normal form post. However, when adding a file to the map, theconverter automatically makes it a multi-part form post. The Filenameparameter of the form is set to an empty string, which means amazon S3will use the filename of the uploaded file as the filename of the objectin S3.Well that is pretty much it – it can’t get much easier,right? 🙂

Jolien

Latest blogs

Meet Lachlan, our new Growth Hacker

Read blog

New: Personalize the order confirmation emails your customers receive

Read blog

Create a personal present with Chatella

Read blog