Created
June 6, 2014 20:07
-
-
Save agemooij/210b970c77618cdbf509 to your computer and use it in GitHub Desktop.
Snippet from an S3 download/upload client based purely on spray-client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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