cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/OSS_Tool/OSSTool.m
2023-07-27 09:20:00 +08:00

140 lines
4.4 KiB
Objective-C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// OSSTool.m
// TreeHole
//
// Created by iOS on 2023/1/31.
//
#import "OSSTool.h"
#import <AliyunOSSiOS/OSSService.h>
@interface OSSTool()
@property (nonatomic, strong)OSSClient *client;
@end
@implementation OSSTool
///初始化
- (void)setupSDK {
}
/// 上传文件
/// - Parameters:
/// - file: 单文件传NSData/NSUrl/NSString;
/// 多文件NSArray<NSData/NSUrl/NSString>
/// - param: NSDictionary<key=@"bucketName"/@"objectKey">
/// - progress: 进度
/// - complete: 完成回调
- (void)uploadFile:(id)file param:(NSDictionary *)param uploadProgress:(OSSNetworkingProgressBlock)progress complete:(OSSCompleteBlock)complete {
}
/// 下载指定文件, return NSData
/// - Parameters:
/// - filePath: 文件路径,url
/// - param: -
/// - progress: -
/// - complete: -
- (void)downloadFile:(NSString *)filePath param:(NSDictionary *)param downloadProgress:(OSSNetworkingProgressBlock)progress complete:(OSSCompleteBlock)complete {
}
#pragma mark - 阿里云
- (void)setupAliOSS {
NSString *OSS_STS_URL = @"";
NSString *OSS_ENDPOINT = @"";
// 初始化具有自动刷新的provider
OSSAuthCredentialProvider *credentialProvider = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:OSS_STS_URL];
// client端的配置,如超时时间开启dns解析等等
OSSClientConfiguration *cfg = [[OSSClientConfiguration alloc] init];
_client = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credentialProvider clientConfiguration:cfg];
}
- (void)uploadToAliOSSFile:(id)file param:(NSDictionary *)param uploadProgress:(OSSNetworkingProgressBlock)progress complete:(OSSCompleteBlock)complete {
NSString *bucketName = @"";//存储目录
NSString *objectKey = @"";//文件名
if ([param.allKeys containsObject:@"bucketName"]) {
bucketName = [param objectForKey:@"bucketName"];
}
if ([param.allKeys containsObject:@"objectKey"]) {
objectKey = [param objectForKey:@"objectKey"];
}
///上传多个文件
NSMutableArray *fileArrayM = [NSMutableArray array];
if ([file isKindOfClass:NSArray.class]) {
for (id fileItem in file) {
OSSPutObjectRequest *put = [OSSPutObjectRequest new];
put.bucketName = bucketName;
put.objectKey = objectKey;
if ([fileItem isKindOfClass:NSData.class]) {
//直接上传NSData
put.uploadingData = fileItem;
} else if ([fileItem isKindOfClass:NSURL.class]) {
//local file path
put.uploadingFileURL = fileItem;
} else if ([fileItem isKindOfClass:NSString.class]) {
//local file path
put.uploadingFileURL = [NSURL URLWithString:fileItem];
} else {
NSAssert(YES, @"fileItem必须是NSData/NSURL/NSString类型");
}
put.uploadProgress = progress;
OSSTask *putTask = [self.client putObject:put];
[fileArrayM addObject:putTask];
}
[OSSTask taskForCompletionOfAllTasks:fileArrayM];
return;
}
///上传单个文件
OSSPutObjectRequest * put = [OSSPutObjectRequest new];
put.bucketName = bucketName;
put.objectKey = objectKey;
if ([file isKindOfClass:NSData.class]) {
//直接上传NSData
put.uploadingData = file;
} else if ([file isKindOfClass:NSURL.class]) {
//local file path
put.uploadingFileURL = file;
} else if ([file isKindOfClass:NSString.class]) {
//local file path
put.uploadingFileURL = [NSURL URLWithString:file];
} else {
NSAssert(YES, @"fileItem必须是NSData/NSURL/NSString类型");
}
put.uploadProgress = progress;
// ^(int64_t bytesSent, int64_t totalByteSent, int64_t totalBytesExpectedToSend) {
// NSLog(@"%lld, %lld, %lld", bytesSent, totalByteSent, totalBytesExpectedToSend);
// };
OSSTask *putTask = [self.client putObject:put];
[putTask continueWithBlock:^id(OSSTask *task) {
// if (!task.error) {
// NSLog(@"upload object success!");
// } else {
// NSLog(@"upload object failed, error: %@" , task.error);
// }
// return nil;
return complete(task.result, task.error);
}];
// 可以等待任务完成
[putTask waitUntilFinished];
}
@end