AWS requests with Signature Version 4 in Swift 5/AWS file downloading without pool ID

AKANSHA DIXIT
2 min readJan 7, 2021

--

To sign AWS requests with Signature Version 4 in Swift 5, we know that we have to follow a few steps like:

  1. Create a canonical request
  2. Create a string to sign
  3. Calculate the signature
  4. Add the signature to the HTTP request.

But the question is how do we do all this in Swift? Well, the task before me was downloading files from AWS without having pool ID with me. While doing this I faced a lot of challenges as I did not get anything relevant that could help. Here is the article which helped me doing this but I faced issues,so, I resolved those bugs and shared a step by step solution to download files from AWS.

Here, we get the url string, we create the url request by adding http method and values.Using this signed request, secret key and access ID of AWS, we create a signed request.

Firstly, date and short date is extracted, then host of url and date are added in url request’s values.Then signed headers are created by sorting header values and joined by separator “;”. Hash of canonical requests is created which consists of method, headers, signedHeaders etc.

Now, stringToSign is created using algorithm, credential, Hash of canonical request, date and is separated by “/n”.

Now use this stringToSign to create an HMAC. The hex-string representation of this HMAC will be the signature for the request. This process can be seen above in the function called ‘hmacStringToSign’. We need to have cryptoSwift using cocoapods in our project to do this process.

The final step is to add the signature to the Authorization header along with the credential scope and signed headers.

Here are the constants used like, Access Key, Secret Key, Region Name etc.

So, applying all these steps we can create a signature for an AWS request and download any file without having poolID in Swift 5.

Please find the complete Project Here.

Hope this will help you folks. If you have any questions or comments, feel free to reach out to me. You can also refer to Amazon’s documentation.

--

--