I'm searching on google from last two days regarding using amazon multipart upload api in my cocoa project for mac. I have downloaded AWS sdk for ios. But didn't find how to use that sdk in cocoa project. Can anyone provide me some example code to achieve multipart uploading using amazon S3 multipart upload???
Edit:
As AWS SDK for IOS is not compatible with Cocoa applications, I'm using Rest api to upload file using libcurl. I'm using following code (by taking reference from http://dextercoder.blogspot.in/2012/02/multipart-upload-to-amazon-s3-in-three.html):
- (void)initUpload
{
NSDate* now = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss "];
NSString *strDate = [NSString stringWithFormat:@"%@GMT", [formatter stringFromDate:now]];
NSString *strDateString = [NSString stringWithFormat:@"Date: %@", strDate];
const char *date = [strDateString UTF8String];
NSString *stringToSign = [NSString stringWithFormat:@"POST\n\n\n%@\n/MY_BUCKET/test.pdf?uploads",strDate];
NSString *signature = [self base64forData:[self HMACSHA1withKey:MY_SECRET forString:stringToSign]];
signature = [signature stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
signature = [signature stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];
signature = [signature stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
NSString *strAuthorization = [NSString stringWithFormat:@"Authorization: AWS MY_ACCESSID:%@", signature];
const char *sig = [strAuthorization UTF8String];
curl_global_init(CURL_GLOBAL_ALL);
CURL *curlHandle = curl_easy_init();
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, date);
headers = curl_slist_append(headers, sig);
CURLcode res;
if (curlHandle) {
curl_easy_setopt(curlHandle, CURLOPT_URL, "http://MY_BUCKET.s3.amazonaws.com/test.pdf?uploads");
curl_easy_setopt(curlHandle, CURLOPT_POST, 1);
curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, "");
curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curlHandle, CURLOPT_VERBOSE,1L);
res = curl_easy_perform(curlHandle);
curl_easy_cleanup(curlHandle);
}
}
- (NSData *)HMACSHA1withKey:(NSString *)key forString:(NSString *)string
{
NSData *clearTextData = [string dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CCHmacContext hmacContext;
CCHmacInit(&hmacContext, kCCHmacAlgSHA1, keyData.bytes, keyData.length);
CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
CCHmacFinal(&hmacContext, digest);
return [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
}
- (NSString *)base64forData:(NSData *)data
{
static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if ([data length] == 0)
return @"";
char *characters = malloc((([data length] + 2) / 3) * 4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length])
{
char buffer[3] = {0,0,0};
short bufferLength = 0;
while (bufferLength < 3 && i < [data length])
buffer[bufferLength++] = ((char *)[data bytes])[i++];
// Encode the bytes in the buffer to four characters, including padding "=" characters if necessary.
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}
But its giving response
"< HTTP/1.0 403 ForbiddenSignatureDoesNotMatchThe request signature we calculated does not match the signature you provided. Check your key and signing method."
Any Idea what I'm doing wrong???