我用
[font=PingFangSC, &]iOS SDK开发包(2016-12-15) 版本号 2.6.0,官方的断点续传示例,使用自签名模式认证,但是始终返回 操作未完成 server error -403的错误,同样的方式在安卓下面能成功,通过跟踪签名请求如下:
[font=PingFangSC, &]
===========下面是ios版本的日志=============
发现加签请求把partNumber和uploadId分成两个请求了,不像安卓版本的一样。怎么解决?
POST
image/jpeg
Mon, 22 May 2017 15:07:39 GMT
/wiz-test/abc.jpg?uploads
======OSS LTAI35TTxg9XV0KK:brScSlpByeIOJn32vFvD3T2VGWk=
GET
Mon, 22 May 2017 15:07:41 GMT
/wiz-test/abc.jpg?uploadId=4C784C746132401793182CAE35B62013
======OSS LTAI35TTxg9XV0KK:zWpO0elEaW0rQ4+25Pt3uk6Nybs=
PUT
zKsBFRoquWrlkhfHHQev0g==
image/jpeg
Mon, 22 May 2017 15:07:42 GMT
/wiz-test/abc.jpg?partNumber=1
======OSS LTAI35TTxg9XV0KK:JokCb1GGSExWCQCOwDsTJtQW3+0=
=====================下面是android版本的日志,======================================
POST
image/jpeg
Mon, 22 May 2017 15:08:33 GMT
/wiz-test/abc.jpg?uploads
======OSS LTAI35TTxg9XV0KK:m5SxoFIZj8j6fdR4IAgHH6w/ntM=
PUT
p5rBrspBOGhb0YcFOsEwLA==
image/jpeg
Mon, 22 May 2017 15:08:34 GMT
/wiz-test/abc.jpg?partNumber=1&uploadId=B0E72129E4D9494CBF384C340BE3F52F
======OSS LTAI35TTxg9XV0KK:cnSlbZUQUi9/2xtMciqYN5jxl2w=
PUT
kKU528nOw83C4IqqyVTFEg==
image/jpeg
Mon, 22 May 2017 15:08:36 GMT
/wiz-test/abc.jpg?partNumber=2&uploadId=B0E72129E4D9494CBF384C340BE3F52F
======OSS LTAI35TTxg9XV0KK:oUXxpvaBhyHMSef63XAh6/BoHj8=
POST
image/jpeg
Mon, 22 May 2017 15:08:36 GMT
/wiz-test/abc.jpg?uploadId=B0E72129E4D9494CBF384C340BE3F52F
======OSS LTAI35TTxg9XV0KK:Dd1qT/MtLo+HLLeU3CoBbXa1b5A=
[font=PingFangSC, &]
// 自签名请求
id<OSSCredentialProvider> credential = [[OSSCustomSignerCredentialProvider alloc] initWithImplementedSigner:^NSString *(NSString *contentToSign, NSError *__autoreleasing *error) {
NSURL * url = [NSURL URLWithString:signUrl];
// NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[@"content=" stringByAppendingString:contentToSign] dataUsingEncoding:NSUTF8StringEncoding]];
OSSTaskCompletionSource * tcs = [OSSTaskCompletionSource taskCompletionSource];
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionTask * sessionTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
[tcs setError:error];
return;
}
[tcs setResult:data];
}];
[sessionTask resume];
[tcs.task waitUntilFinished];
if (tcs.task.error) {
return nil;
} else {
return [[NSString alloc]initWithData:tcs.task.result encoding:NSUTF8StringEncoding];
}
}];
client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];[[[[[[OSSTask taskWithResult:nil] continueWithBlock:^id (OSSTask *task) {
// 为该文件构造一个唯一的记录键
NSURL * fileURL = [NSURL fileURLWithPath:filePath];
NSDate * lastModified;
NSError * error;
[fileURL getResourceValue:&lastModified forKey:NSURLContentModificationDateKey error:&error];
if (error) {
return [OSSTask taskWithError:error];
}
recordKey = [NSString stringWithFormat:@"%@-%@-%@-%@", bucketName, objectKey, [OSSUtil getRelativePath:filePath], lastModified];
// 通过记录键查看本地是否保存有未完成的UploadId
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
return [OSSTask taskWithResult:[userDefault objectForKey:recordKey]];
}] continueWithSuccessBlock:^id (OSSTask *task) {
if (!task.result) {
// 如果本地尚无记录,调用初始化UploadId接口获取
OSSInitMultipartUploadRequest * initMultipart = [OSSInitMultipartUploadRequest new];
initMultipart.bucketName = bucketName;
initMultipart.objectKey = objectKey;
return [client multipartUploadInit:initMultipart];
}
//OSSLogVerbose(@"An resumable task for uploadid: %@", task.result);
return task;
}] continueWithSuccessBlock:^id (OSSTask *task) {
NSString * uploadId = nil;
if (task.error) {
return task;
}
if ([task.result isKindOfClass:[OSSInitMultipartUploadResult class]]) {
uploadId = ((OSSInitMultipartUploadResult *)task.result).uploadId;
} else {
uploadId = task.result;
}
if (!uploadId) {
return [OSSTask taskWithError:[NSError errorWithDomain:OSSClientErrorDomain
code:OSSClientErrorCodeNilUploadid
userInfo:@{OSSErrorMessageTOKEN: @"Can't get an upload id"}]];
}
// 将“记录键:UploadId”持久化到本地存储
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:uploadId forKey:recordKey];
[userDefault synchronize];
return [OSSTask taskWithResult:uploadId];
}] continueWithSuccessBlock:^id (OSSTask *task) {
// 持有UploadId上传文件
OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
resumableUpload.bucketName = bucketName;
resumableUpload.objectKey = objectKey;
resumableUpload.uploadId = task.result;
resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:filePath];
resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSString *progress=[NSString stringWithFormat:@"%.2f",(float) totalBytesSent/totalBytesExpectedToSend*100];
NSDictionary *ret = @{@"oper":@"progress", @"progress":[progress stringByAppendingString:@"%"]};
[self sendResultEventWithCallbackId:_cbId dataDict:ret errDict:nil doDelete:NO];
};
return [client resumableUpload:resumableUpload];
}] continueWithBlock:^id (OSSTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:OSSClientErrorDomain] && task.error.code == OSSClientErrorCodeCannotResumeUpload) {
// 如果续传失败且无法恢复,需要删除本地记录的UploadId,然后重启任务
[[NSUserDefaults standardUserDefaults] removeObjectForKey:recordKey];
}
NSDictionary *err = @{@"code":@500,@"msg":task.error.localizedDescription};
[self sendResultEventWithCallbackId:_cbId dataDict:nil errDict:err doDelete:YES];
} else {
// 上传成功,删除本地保存的UploadId
[[NSUserDefaults standardUserDefaults] removeObjectForKey:recordKey];
OSSResumableUploadResult *result=task.result;
NSDictionary *ret = @{@"oper":@"complete",@"requestId":result.requestId};
[self sendResultEventWithCallbackId:_cbId dataDict:ret errDict:nil doDelete:YES];
}
return nil;
}];
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。