美文网首页iOS 知识点
阿里云多图上传

阿里云多图上传

作者: 最晴天 | 来源:发表于2016-11-10 16:52 被阅读303次

来说说阿里云的上传功能,最开始是想要自己封装一下OSSClient,类似于单例类的存在,折腾了两三天,也没有解决这个问题,原因在于 异步请求,block回调,等各种的不同步,因为自己太菜了,这一块后面再多看看资料 学习一下。。。

阿里云的官方文档上很清楚的,比较坑的是 我们的NSURLRequest直接请求,拿不到token的数据,因为需要网页先行登录才可以。

最后调整为对接口进行封装,似乎采用这种方式的比较多,使用获得的STS令牌 再去向阿里云上传图片。

多图上传

```

+ (void)upLoadImagesToAliyun:(NSArray *)imagesArr imageNames:(NSArray *)imageNames  completionBlock:(void(^)(NSMutableArray *errorIndexs,UploadImageState state))completeBlock{

[HHHomeData requestAliyunSTSInfoBlock:^(HHAliyunSTSModel *STSModel, NSString *errorStr) {

dispatch_queue_t uploadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_group_t uploadGroup = dispatch_group_create();

//异步上传所有图片

__block NSMutableArray *errorIndexs = [NSMutableArray array];

for (int i = 0; i < imagesArr.count; i++) {

dispatch_group_async(uploadGroup, uploadQueue, ^{

UIImage *image = imagesArr[i];

NSString *imageName = imageNames[i];

//上传图片的二进制数据

OSSPutObjectRequest *put = [OSSPutObjectRequest new];

put.bucketName = STSModel.bucket;

//                put.objectKey = [NSString stringWithFormat:@"%@%@",STSModel.objectKeyPrefix,imageName];

put.objectKey = imageName;

HHLog(@"bucket:%@====上传时的objectKey==%@",put.bucketName,put.objectKey);

put.uploadingData = [image getImageDataWithCompress];

put.contentType = @"image/jpeg";

put.contentMd5 = [OSSUtil base64Md5ForData:put.uploadingData];

//                HHLog(@"data===%lu",(unsigned long)put.uploadingData.length);

OSSClient *client = [self getClientWithAccessKeyId:STSModel.accessKeyId secrectKey:STSModel.accessKeySecret token:STSModel.securityToken];

//                HHLog(@"==id=%@,secrec>>%@",STSModel.accessKeyId,STSModel.accessKeySecret);

OSSTask *putTask = [client putObject:put];

//                putTask = [client presignPublicURLWithBucketName:STSModel.bucket withObjectKey:put.objectKey];

put.uploadProgress = ^(int64_t bytesSent,int64_t totalByteSent, int64_t totalBytesExpectedToSend) {

HHLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);

if (totalByteSent == [[image getImageDataWithCompress] length]) {

}

};

[putTask continueWithBlock:^id _Nullable(OSSTask * _Nonnull task) {

if (!task.error) {

HHLog(@"第%d张上传成功",i);

}else{

HHLog(@"第%d张上传失败,error==>%@",i,task.error.localizedDescription);

[errorIndexs addObject:@(i)];

}

return nil;

}];

[putTask waitUntilFinished];

});

}

dispatch_group_notify(uploadGroup, uploadQueue, ^{

HHLog(@"上传结束后的通知");

if (errorIndexs.count == 0) {

if (completeBlock) {

completeBlock(nil,UploadImageSuccess);

}

}else{

if (completeBlock) {

completeBlock(errorIndexs,UploadImageFailed);

}

}

});

}];

}

```

相关文章

  • 阿里云上传

    阿里云上传 1. 导入阿里云上传SDK 2. 实例化阿里云 3. 使用阿里云上传实例

  • 阿里云OSS上传图片封装

    项目中用到了阿里云对象存储,就封装了四种上传方式,按数量分为单图和多图上传,上传方式分为同步和异步上传。

  • PicPlus + Github 实现 markdown 图床

    前言 PicPlus 是一个图床上传 APP,帮你快速上传手机图片到各个图床,支持七牛云、阿里云、又拍云等主流图床...

  • PicPlus + Gitee (码云) 实现 markdown

    前言 PicPlus 是一个图床上传 APP,帮你快速上传手机图片到各个图床,支持七牛云、阿里云、又拍云等主流图床...

  • iOS 使用阿里云上传图片

    前言: 项目要把图床从七牛迁到阿里云。因此,就要使用阿里云的图床上传。我去看了阿里云开发文档给出的示例代码和git...

  • 封装直传阿里云存储文件上传控件

    本文目标 封装一个直传阿里云OSS云存储图片上传控件 控件效果图 上传之前 上传成功 预览图片 文件上传前后台及阿...

  • vue多图上传到阿里云

    结合这两篇文章小程序选择图片、预览,上传到阿里云和阿里云上传图片 引入js 做循环判断上传成功后再上传下一张图 循...

  • 阿里云多图上传

    来说说阿里云的上传功能,最开始是想要自己封装一下OSSClient,类似于单例类的存在,折腾了两三天,也没有解决这...

  • 移动端实现图片压缩上传

    上传图片有很多框架,或者是阿里云直传,关于阿里云直传可以看我之前的博客上传图片到阿里云,这次是通过后台进行操作上传...

  • 图片上传

    图片上传 获取阿里云接口 图片上传到阿里云 图片预览接口post http://113.108.139.178:1...

网友评论

    本文标题:阿里云多图上传

    本文链接:https://www.haomeiwen.com/subject/hkjxpttx.html