objective-camazon-web-servicesafnetworkingnsurlconnection

Downloading from amazon with NSURLConnection


I'm trying to download files from amazon server with my own library based on NSURLConnection. And amazon reply with this error:

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<StringToSignBytes>47 45 54 ... 6e 67</StringToSignBytes>
<RequestId>...</RequestId><HostId>...</HostId> 
<SignatureProvided>45ZlY98%2FyrYcHcA%2BKDmArUic5%2BI%3D</SignatureProvided>
<StringToSign>...</StringToSign>
<AWSAccessKeyId>...</AWSAccessKeyId>
</Error>

I replaced with "..." helpless information, download link was like this one (there is no reason to post original, because it has very short lifetime)

https://test111.s3-eu-west-1.amazonaws.com/uploads/post/image/152/preview_image.png?AWSAccessKeyId=AKIAJCZPX74XBX6LU7FA&Signature=45ZlY98%2FyrYcHcA%2BKDmArUic5%2BI%3D&Expires=1348578742

The same download link (the same as original) works with AFNetworking framework and in Safari, but does not in Chrome (!). Before I replace my own library with AFNetworking, may be you tell me what I'm doing wrong. Thanks!


Solution

  • I "love" these types of errors which have two or more reasons to fire. The first one was this:

    NSURL* url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    

    Because of aws give you already encoded string you should avoid to encoding it twice.

    The second reason was harder to understand. You should not call setHTTPBody if you have nothing to put in it. I did...