Groovy version of code to get an Amazon S3 Download URL

This post is more than 2 years old.

Recently I had need of some Groovy code to integrate with Amazon S3. I searched around, but it seemed as if they only code out there was set up for Grails. I couldn't find a simple class that I could drop into our code at work and run with that. Now maybe I didn't search long enough, and maybe the Grails-related stuff would have worked, but it just didn't feel right.

I then ran across this UDF written by ColdFusion developer Barney Boisvert: Amazon S3 URL Builder for ColdFusion

This was perfect. While I had been looking for an S3 "library", all I really needed was a way to generate the URL. I took his CFML and converted it into the following Groovy code. Groovy people - feel free to laugh/comment on how I could improve this:

private String getS3URL(key,secret,bucket,objectkey,expires=900) { def algo = 'HmacSHA1' def expireValue = ((new Date().getTime())/1000+expires).intValue() def stringToSign = 'GET\n\n\n'+expireValue+'\n/'+bucket+'/'+objectkey def signingKey = new javax.crypto.spec.SecretKeySpec(secret.getBytes(),algo) def mymac = Mac.getInstance(algo) mymac.init(signingKey)
def rawSig = mymac.doFinal(stringToSign.getBytes())
def sig = new sun.misc.BASE64Encoder().encode(rawSig);

sig =
def destURL = "$bucket/$objectkey?AWSAccessKeyId="+key+"&Signature=$sig&Expires=$expireValue"
return destURL


I think it's interesting to compare both versions. My version got rid of the requestType parameter since we didn't need to worry about that for our code.

You know it's funny - I never used to understand why people didn't use semicolons at the end of their code when it was allowed - but now that I'm getting used to it, it really bugs me when I have to use them.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA