阿里云OSS上传多张图片


+ (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"];//以上三个参数都是后台给的
    
    OSSClient  * client = [[OSSClient alloc] initWithEndpoint:endpoint credentialProvider:credential];
    
    // 明文设置secret的方式建议只在测试时使用,更多鉴权模式参考后面链接给出的官网完整文档的`访问控制`章节
    
    //    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字节
        
        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];//多张图片时这里面存放的图片名字的数组在把这些名字弄成json字符串 给服务器
                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给的用于多张图片上传时 加上它时只有第一个走了成功或者失败第二个才会走 。相当于等待串行。
    }
    

原文链接:https://blog.csdn.net/qq_33560608/article/details/88761764

推荐阅读更多精彩内容