我在谷歌搜索过去两天关于在我的可可项目中使用amazon multipart上传api for mac。我已经下载了适用于ios的AWS sdk。但没有找到如何在可可项目中使用该sdk。任何人都可以提供一些示例代码来实现使用亚马逊S3分段上传的多部分上传???
修改
由于适用于IOS的AWS SDK与Cocoa应用程序不兼容,我使用Rest api使用libcurl上传文件。我正在使用以下代码(通过引用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];
}
但它的回应是
“&lt; HTTP / 1.0 403禁止
SignatureDoesNotMatch
我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。“
答案 0 :(得分:0)
您可以通过SDK本身随附的AWS服务(如S3,SDB等)获得基本示例: http://docs.amazonwebservices.com/mobile/sdkforios/gsg/Welcome.html?r=1498
答案 1 :(得分:0)
你可以从http://dextercoder.blogspot.in/2012/02/multipart-upload-to-amazon-s3-in-three.html引用java代码。讲述如何在没有AWS的情况下在多部件中上传文件。