+ (void)ImageAyyay:(NSArray *)dataSoure success:(void (^)(id res))success{
NSMutableArray * array = [NSMutableArray array];
NSString *endpoint = @"oss-cn.aliyuncs.com";//后臺給的
id<OSSCredentialProvider> credential = [[OSSStsTokenCredentialProvider alloc] initWithAccessKeyId:@"AccessKeyId " secretKeyId:@"AccessKeySecre" securityToken:@"SecurityToken"];//以上三個參數(shù)都是后臺給的
OSSClient * client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
// 明文設(shè)置secret的方式建議只在測試時使用,更多鑒權(quán)模式參考后面鏈接給出的官網(wǎng)完整文檔的`訪問控制`章節(jié)
// id credential = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:@"AccessKey"
// secretKey:@"secretKey"];//后臺給的
// OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
for (int i = 0; i<dataSoure.count; i++) {
put.bucketName = @"cl1000";//后臺給的
put.objectKey = [NSString stringWithFormat:@"cIos%@.jpg",[self currentTimeStr]];//這個方法為時間戳加userid命名方式
put.uploadingData = dataSoure[i]; // 直接上傳NSData字節(jié)
put.uploadProgress = ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
};
OSSTask * putTask = [client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
[array addObject:put.objectKey];//多張圖片時這里面存放的圖片名字的數(shù)組在把這些名字弄成json字符串 給服務(wù)器
if (dataSoure.count > 1) {
if (i == dataSoure.count-1) {
success(array);
}
}else{
success(array);
}
} else {
NSLog(@"upload object failed, error: %@" , task.error);
}
return nil;
}];
[putTask waitUntilFinished];//這個時sdk給的用于多張圖片上傳時 加上它時只有第一個走了成功或者失敗第二個才會走 。相當(dāng)于等待串行。
}
原文鏈接:https://blog.csdn.net/qq_33560608/article/details/88761764