Skip to content

Instantly share code, notes, and snippets.

@agemooij
Created June 6, 2014 20:07
Show Gist options
  • Save agemooij/210b970c77618cdbf509 to your computer and use it in GitHub Desktop.
Save agemooij/210b970c77618cdbf509 to your computer and use it in GitHub Desktop.
Snippet from an S3 download/upload client based purely on spray-client
/**
* Amazon S3 uploads and downloads need to have an authorization header containing a signature based on a very specific formatting
* of several of the headers of the HTTP request. This method only implements the parts that are required for our use cases,
* thus very specific situations like headers spanning multiple lines are not supported.
* For the details of the string-to-sign that is constructed see http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html?r=1821
*/
def signAwsRequest(method: String, dateString: String, contentType: String, bucket: String, resource: String, amzHeaders: List[HttpHeader] = List.empty[HttpHeader], contentMd5: String = ""): String = {
val bucketResource = s"/$bucket$resource"
val signableAmzHeaders = amzHeaders.sortBy(_.name).map { header ⇒
s"""${header.name}:${header.value}"""
}
val signatureElements = method :: contentMd5 :: contentType :: dateString :: signableAmzHeaders ::: bucketResource :: Nil
val stringToSign = signatureElements.mkString("\n")
calculateHmacSha1(filesSettings.S3.SecretKey, stringToSign).asBase64String
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment