232 lines
11 KiB
Objective-C
232 lines
11 KiB
Objective-C
//
|
|
// QXCOSUploadManager.m
|
|
// QXLive
|
|
//
|
|
// Created by 启星 on 2025/10/23.
|
|
//
|
|
|
|
#import "QXCOSUploadManager.h"
|
|
#import <QCloudCOSXML/QCloudCOSXMLTransfer.h>
|
|
@implementation QXCOSUploadManager
|
|
+(instancetype)shareManager{
|
|
static QXCOSUploadManager *manager = nil;
|
|
static dispatch_once_t predicate;
|
|
dispatch_once(&predicate, ^{
|
|
manager = [[QXCOSUploadManager alloc] init];
|
|
});
|
|
return manager;
|
|
}
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
// 设置并发数4
|
|
[QCloudHTTPSessionManager shareClient].customConcurrentCount = 1;
|
|
// 设置自动上限8
|
|
[QCloudHTTPSessionManager shareClient].maxConcurrencyTask = 9;
|
|
}
|
|
return self;
|
|
}
|
|
-(void)uploadFile:(NSArray *)files withObjectKey:(NSArray *)objectKeys isAsync:(BOOL)isAsync complete:(void (^)(NSArray<NSString *> * _Nullable, QXCOSUploadImageState))complete{
|
|
[[QXRequset shareInstance] getWithUrl:[NSString stringWithFormat:@"%@%@",ServerUrl,QXGetCosTempKeys] parameters:@{} needCache:NO success:^(id responseObject) {
|
|
QXCOSUploadModel *model = [QXCOSUploadModel yy_modelWithJSON:responseObject[@"data"]];
|
|
if (![model.region isExist] || ![model.bucket isExist] || ![model.credentials.sessionToken isExist] || ![model.credentials.tmpSecretId isExist] || ![model.credentials.tmpSecretKey isExist]) {
|
|
QXLOG(@"服务端配置错误");
|
|
complete(@[],QXCOSUploadImageFailed);
|
|
return;
|
|
}
|
|
NSString *region = model.region;
|
|
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
|
|
QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
|
|
endpoint.regionName = region;
|
|
// 使用 HTTPS
|
|
endpoint.useHTTPS = true;
|
|
configuration.endpoint = endpoint;
|
|
// 初始化 COS 服务示例
|
|
[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
|
|
[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
|
|
|
|
NSString *bucket = model.bucket;
|
|
|
|
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
|
|
queue.maxConcurrentOperationCount = files.count;
|
|
NSMutableArray *callBackNames = [NSMutableArray array];
|
|
for (int i = 0 ; i < files.count ;i++) {
|
|
id file = files[i];
|
|
if (file) {
|
|
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
|
|
QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];
|
|
QCloudCredential * credential = [QCloudCredential new];
|
|
credential.secretID = model.credentials.tmpSecretId;
|
|
credential.secretKey = model.credentials.tmpSecretKey;
|
|
credential.token = model.credentials.sessionToken;
|
|
// 设置临时密钥
|
|
put.credential = credential;
|
|
put.bucket = bucket;
|
|
put.object = [NSString stringWithFormat:@"%@%@",IMG_FILE_BASE_PATH,objectKeys[i]];
|
|
if ([file isKindOfClass:[UIImage class]]) {
|
|
NSData *data = UIImageJPEGRepresentation(file, 0.3);
|
|
put.body = data;
|
|
}else if ([file isKindOfClass:[NSData class]]){
|
|
put.body = file;
|
|
}else if ([file isKindOfClass:[NSURL class]]){
|
|
put.body = (NSURL *)file;
|
|
}
|
|
// 监听上传进度
|
|
[put setSendProcessBlock:^(int64_t bytesSent,
|
|
int64_t totalBytesSent,
|
|
int64_t totalBytesExpectedToSend) {
|
|
// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)
|
|
// totalBytesSent 已发送的字节数
|
|
// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)
|
|
}];
|
|
|
|
// 监听上传结果
|
|
[put setFinishBlock:^(QCloudUploadObjectResult *result, NSError *error) {
|
|
if (!error) {
|
|
NSLog(@"upload object success!");
|
|
} else {
|
|
NSLog(@"upload object failed, error: %@" , error);
|
|
}
|
|
NSString * fileUrl = result.location;
|
|
[callBackNames addObject:fileUrl];
|
|
if (isAsync) {
|
|
if (file == files.lastObject) {
|
|
NSLog(@"upload object finished!");
|
|
if (complete) {
|
|
complete([NSArray arrayWithArray:callBackNames] ,QXCOSUploadImageSuccess);
|
|
}
|
|
}
|
|
}
|
|
}];
|
|
[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
|
|
}];
|
|
if (queue.operations.count != 0) {
|
|
[operation addDependency:queue.operations.lastObject];
|
|
}
|
|
[queue addOperation:operation];
|
|
}
|
|
if (!isAsync) {
|
|
[queue waitUntilAllOperationsAreFinished];
|
|
NSLog(@"haha");
|
|
if (complete) {
|
|
if (complete) {
|
|
complete([NSArray arrayWithArray:callBackNames], QXCOSUploadImageSuccess);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} fail:^(NSError *error, NSString *msg, NSURLSessionDataTask *task) {
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)activityUploadFile:(NSArray *)files withObjectKey:(NSArray *)objectKeys isAsync:(BOOL)isAsync complete:(void (^)(NSArray<NSString *> * _Nonnull, QXCOSUploadImageState))complete{
|
|
[[QXRequset shareInstance] getWithUrl:[NSString stringWithFormat:@"%@%@",ServerUrl,QXGetCosTempKeys] parameters:@{} needCache:NO success:^(id responseObject) {
|
|
QXCOSUploadModel *model = [QXCOSUploadModel yy_modelWithJSON:responseObject[@"data"]];
|
|
if (![model.region isExist] || ![model.bucket isExist] || ![model.credentials.sessionToken isExist] || ![model.credentials.tmpSecretId isExist] || ![model.credentials.tmpSecretKey isExist]) {
|
|
QXLOG(@"服务端配置错误");
|
|
complete(@[],QXCOSUploadImageFailed);
|
|
return;
|
|
}
|
|
NSString *region = model.region;
|
|
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
|
|
QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
|
|
endpoint.regionName = region;
|
|
// 使用 HTTPS
|
|
endpoint.useHTTPS = true;
|
|
configuration.endpoint = endpoint;
|
|
// 初始化 COS 服务示例
|
|
[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
|
|
[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
|
|
|
|
NSString *bucket = model.bucket;
|
|
|
|
NSMutableArray *callBackNames = [NSMutableArray array];
|
|
for (int i = 0 ; i < files.count ;i++) {
|
|
id file = files[i];
|
|
if (file) {
|
|
QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];
|
|
QCloudCredential * credential = [QCloudCredential new];
|
|
credential.secretID = model.credentials.tmpSecretId;
|
|
credential.secretKey = model.credentials.tmpSecretKey;
|
|
credential.token = model.credentials.sessionToken;
|
|
// 设置临时密钥
|
|
put.credential = credential;
|
|
put.bucket = bucket;
|
|
put.object = [NSString stringWithFormat:@"%@%@",IMG_FILE_BASE_PATH,objectKeys[i]];
|
|
if ([file isKindOfClass:[UIImage class]]) {
|
|
NSData *data = UIImageJPEGRepresentation(file, 0.3);
|
|
put.body = data;
|
|
}else if ([file isKindOfClass:[NSData class]]){
|
|
put.body = file;
|
|
}else if ([file isKindOfClass:[NSURL class]]){
|
|
put.body = (NSURL *)file;
|
|
}
|
|
// 监听上传进度
|
|
[put setSendProcessBlock:^(int64_t bytesSent,
|
|
int64_t totalBytesSent,
|
|
int64_t totalBytesExpectedToSend) {
|
|
// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)
|
|
// totalBytesSent 已发送的字节数
|
|
// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)
|
|
}];
|
|
|
|
// 监听上传结果
|
|
[put setFinishBlock:^(QCloudUploadObjectResult *result, NSError *error) {
|
|
if (error) {
|
|
NSLog(@"upload object failed, error: %@" , error);
|
|
if (complete) {
|
|
complete([NSArray arrayWithArray:callBackNames] ,QXCOSUploadImageFailed);
|
|
}
|
|
return;
|
|
} else {
|
|
NSLog(@"upload object success!--i=%d",i);
|
|
}
|
|
NSString * fileUrl = result.location;
|
|
[callBackNames addObject:fileUrl];
|
|
NSLog(@"添加第%d个图片",i);
|
|
if (isAsync) {
|
|
if (callBackNames.count == files.count) {
|
|
NSLog(@"upload object finished!");
|
|
if (complete) {
|
|
complete([NSArray arrayWithArray:callBackNames] ,QXCOSUploadImageSuccess);
|
|
}
|
|
}
|
|
}
|
|
}];
|
|
[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
|
|
}
|
|
|
|
}
|
|
|
|
} fail:^(NSError *error, NSString *msg, NSURLSessionDataTask *task) {
|
|
|
|
}];
|
|
}
|
|
- (NSString *)currentDate {
|
|
NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
|
|
long long timestampInMilliseconds = interval * 1000;
|
|
return [NSString stringWithFormat:@"%lld",timestampInMilliseconds];
|
|
}
|
|
@end
|
|
|
|
|
|
@implementation QXCOSUploadModel
|
|
|
|
+(NSDictionary<NSString *,id> *)modelContainerPropertyGenericClass{
|
|
return @{
|
|
@"credentials" : @"QXCOSUploadCredentialsModel",
|
|
};
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation QXCOSUploadCredentialsModel
|
|
|
|
|
|
|
|
@end
|