[SalesForce] Uploading files to Amazon S3 from a trigger

I get files from an external webservice and have to upload them to Amazon S3 via apex. I found the unmanaged package Force.com for Amazon Web Services a.k.a Amazon toolkit, but it looks rather outdated (SOAP instead of REST, incomplete code). I also don't want to use a paid app from the app exchange.

My use case is just uploading single files that I have in memory to S3. I don't want to create buckets, delete files etc. There is no visualforce involved.

Are there other code examples or packages available that I could use?

More context if necessary: In an external applciation, users can upload files. These files are passed on to Salesforce using a custom rest service. To store these files, we use the app S-Drive, which allows uploading and downloading files to Amazon S3 on managed Visualforce pages. To upload files using apex, you have to

  1. Initialize the upload using the S-Drive API.
  2. Actually upload the file to S3 directly, using parameters you get from step 1.
  3. Finalize the upload using the S-Drive API.

I need help with step 2.

Best Answer

I found the example code of Jason Flammang at https://developer.salesforce.com/forums/?id=906F0000000BMDFIA4 (the posting from October 19, 2015, NOT the accepted answer.) I could adapt it to my purpose and it worked fine. I use it to do a http PUT to upload files to S3.

Changes I made to the example code:

  • Instead of the local timezone, I use GMT.

  • The filename has to be urlencoded, otherwise the signature will not be correct if it contains special characters (like ß), and the upload will be rejected (http 403).

Related Topic