viewDidLoad的时候调用server接口获取的objectKey,bucketName,AccessKeyId,AccessKeySecret,SecurityToken,Expiration等信息存放在dicResult字典中;设置STS鉴权模式。
//STS鉴权模式
-(void)STSTokenCredentialProvider {
NSString *endpoint = [dicResult objectForKey:@"endpoint"];
id<OSSCredentialProvider> credential2 = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * {
//server接口获取的信息存放在dicResult字典中
[self authorizeAPISuccess:nil Faild:nil];
OSSFederationToken * token = [OSSFederationToken new];
token.tAccessKey = [dicResult objectForKey:@"AccessKeyId"];
token.tSecretKey = [dicResult objectForKey:@"AccessKeySecret"];
token.tToken = [dicResult objectForKey:@"SecurityToken"];
NSString *string = [dicResult objectForKey:@"Expiration"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[formatter dateFromString:string];
token.expirationTimeInMilliSecond = [date timeIntervalSince1970];
NSLog(@"get token: %@", token);
return token;
}];
client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential2];
}
然后点击上传按钮调用下面方法
//断点续传视频
-(void)multipartUploadVideo {
__block NSString * uploadId = nil;
OSSInitMultipartUploadRequest * init = [OSSInitMultipartUploadRequest new];
init.bucketName = dicResult[@"bucketName"];
init.objectKey = dicResult[@"objectKey"];
// 先获取到用来标识整个上传事件的UploadId
OSSTask * task = [client multipartUploadInit:init];
[[task continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSInitMultipartUploadResult * result = task.result;
uploadId = result.uploadId;
} else {
NSLog(@"init uploadid failed, error: %@", task.error);
}
return nil;
}] waitUntilFinished];
// 获得UploadId进行上传,如果任务失败并且可以续传,利用同一个UploadId可以上传同一文件到同一个OSS上的存储对象
OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
resumableUpload.bucketName = dicResult[@"bucketName"];
resumableUpload.objectKey = dicResult[@"objectKey"];
resumableUpload.uploadId = uploadId;
resumableUpload.partSize = 1024 * 1024;
resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"VID_20160226_134406.mp4"]];
OSSTask * resumeTask = [client resumableUpload:resumableUpload];
[resumeTask continueWithBlock:^id(OSSTask *task) {
if (task.error) {
NSLog(@"error: %@", task.error);
if ([task.error.domain isEqualToString:OSSClientErrorDomain] && task.error.code == OSSClientErrorCodeCannotResumeUpload) {
//
NSLog(@"该任务无法续传,需要获取新的uploadId重新上传");
}
} else {
NSLog(@"Upload file success");
}
return nil;
}];
// [resumeTask waitUntilFinished];
// [resumableUpload cancel];
}
然而在第一个block里就报错了
Error Domain=com.aliyun.oss.serverError Code=-403 "(null)" UserInfo={__name=Error, HostId=em-video-upload-private.oss-cn-hangzhou.aliyuncs.com, Message=Access denied by authorizer's policy., Code=AccessDenied, RequestId=56D3B522F9EEA2C908C866B8}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。