更换存储
This commit is contained in:
@@ -4,12 +4,89 @@
|
||||
//
|
||||
// Created by GreenLand on 2022/2/23.
|
||||
//
|
||||
|
||||
#import <QCloudCOSXML/QCloudCOSXML.h>
|
||||
#import "UploadFile.h"
|
||||
#import <Qiniu/QiniuSDK.h>
|
||||
#import <AFNetworking.h>
|
||||
#import "Api+Mine.h"
|
||||
#import "UploadFileModel.h"
|
||||
static UploadFile* manager;
|
||||
|
||||
@interface UploadFile()<QCloudSignatureProvider>
|
||||
@property(nonatomic,strong) UploadFileModel *fileModel;
|
||||
@end
|
||||
|
||||
@implementation UploadFile
|
||||
|
||||
+ (instancetype)share {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
manager = [[UploadFile alloc] init];
|
||||
});
|
||||
return manager;
|
||||
}
|
||||
|
||||
-(void)initQCloud{
|
||||
[Api getQCloudInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
|
||||
if (code == 200){
|
||||
UploadFileModel *fileModel = [UploadFileModel modelWithDictionary:data.data];
|
||||
self.fileModel = fileModel;
|
||||
QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
|
||||
QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
|
||||
|
||||
|
||||
|
||||
endpoint.regionName = fileModel.region;
|
||||
// 使用 HTTPS
|
||||
endpoint.useHTTPS = true;
|
||||
configuration.endpoint = endpoint;
|
||||
// 密钥提供者为自己
|
||||
configuration.signatureProvider = self;
|
||||
// 初始化 COS 服务示例
|
||||
[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
|
||||
[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:
|
||||
configuration];
|
||||
|
||||
}
|
||||
}];
|
||||
}
|
||||
#pragma mark- QCloudSignatureProvider
|
||||
- (void) signatureWithFields:(QCloudSignatureFields*)fileds
|
||||
request:(QCloudBizHTTPRequest*)request
|
||||
urlRequest:(NSMutableURLRequest*)urlRequst
|
||||
compelete:(QCloudHTTPAuthentationContinueBlock)continueBlock
|
||||
{
|
||||
|
||||
QCloudCredential* credential = [QCloudCredential new];
|
||||
|
||||
|
||||
// 临时密钥 SecretId
|
||||
// sercret_id替换为用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
|
||||
credential.secretID = self.fileModel.secretId;
|
||||
// 临时密钥 SecretKey
|
||||
// sercret_key替换为用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
|
||||
credential.secretKey = self.fileModel.secretKey;
|
||||
// 临时密钥 Token
|
||||
// 如果使用永久密钥不需要填入token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见https://cloud.tencent.com/document/product/436/14048
|
||||
credential.token = self.fileModel.sessionToken;
|
||||
/** 强烈建议返回服务器时间作为签名的开始时间, 用来避免由于用户手机本地时间偏差过大导致的签名不正确(参数startTime和expiredTime单位为秒)
|
||||
*/
|
||||
credential.startDate = [NSDate dateWithTimeIntervalSince1970:self.fileModel.startTime]; // 单位是秒
|
||||
credential.expirationDate = [NSDate dateWithTimeIntervalSince1970:self.fileModel.startTime];// 单位是秒
|
||||
|
||||
|
||||
QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc]
|
||||
initWithCredential:credential];
|
||||
// 注意 这里不要对urlRequst 进行copy以及mutableCopy操作
|
||||
QCloudSignature *signature = [creator signatureForData:urlRequst];
|
||||
continueBlock(signature, nil);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//zoneAs0
|
||||
/// 上传一个文件
|
||||
/// @param filePath 文件地址
|
||||
|
Reference in New Issue
Block a user