#import "AliyunOSSiOS.h"
NSString *endpoint = @"https://oss-cn-hangzhou.aliyuncs.com";
// 明文设置secret的方式建议只在测试时使用,更多鉴权模式请参考后面的`访问控制`章节
id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:self.accessKeyId secretKeyId:self.accessKeySecret securityToken:self.securityToken];
//设置网络请求的一些参数
OSSClientConfiguration * conf=[OSSClientConfiguration new];
//最大重试次数
conf.maxRetryCount=3;
conf.timeoutIntervalForRequest=30;// 网络请求的超时时间
OSSClient* client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
// 必填字段
put.bucketName = @"v-kd";
put.objectKey = [NSString stringWithFormat:@"user/%@/certificate/%@tupian.png",userId,_dateStr];
put.uploadingData=data;
//可以自定义添加头信息,方便后面区分的使用
put.objectMeta = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"sure", @"x-oss-meta-name1d", nil];
// 可选字段,可不设置
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// 当前上传段长度、当前已经上传总长度、一共需要上传的总长度
};
OSSTask * putTask = [client putObject:put];
//[putTask waitUntilFinished]; // 阻塞直到上传完成
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
//上传完成,可以进行的操作
} else {
NSLog(@"upload object failed, error: 0000000000000000%@" , task.error);
}
return nil;
}];