产品动态
产品公告
pod 'QCloudCOSXML'
pod 'QCloudCOSXML/Transfer'
pod 'QCloudCOSXML/Slim'


-ObjC


#import <QCloudCOSXML/QCloudCOSXML.h>
#import <QCloudCOSXML/QCloudCOSXMLTransfer.h>
#import <QCloudCOSXML/QCloudCOSXML.h>
import QCloudCOSXML
import QCloudCOSXML
import QCloudCOSXML
//AppDelegate.m@interface AppDelegate()@end@implementation AppDelegate- (BOOL)application:(UIApplication * )applicationdidFinishLaunchingWithOptions:(NSDictionary * )launchOptions {QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];// 存储桶所在地域简称可以在控制台查看:https://console.tencentcloud.com/cos5/bucket// 例如广州地区是 ap-guangzhou,COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = @"COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;configuration.endpoint = endpoint;// 初始化 COS 服务示例[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];return YES;}@end
//AppDelegate.swiftclass AppDelegate: UIResponder, UIApplicationDelegate {func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();// 存储桶所在地域简称可以在控制台查看:https://console.tencentcloud.com/cos5/bucket// 例如广州地区是 ap-guangzhou,COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = "COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;config.endpoint = endpoint;// 初始化 COS 服务示例QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);return true}}
import SwiftUIimport QCloudCOSXML@mainstruct SwiftUIDemoApp: App {init() {// 在应用初始化时调用 SDK 配置COSManager.shared.setupCOSSDK();}var body: some Scene {WindowGroup {ContentView()}}}class COSManager: NSObject {static let shared = COSManager()private override init() {}func setupCOSSDK() {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();// 存储桶所在地域简称可以在控制台查看:https://console.tencentcloud.com/cos5/bucket// 例如广州地区是 ap-guangzhou,COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = "COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;config.endpoint = endpoint;// 初始化 COS 服务示例QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);}}
// 以文件上传请求为例QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];QCloudCredential * credential = [QCloudCredential new];// 临时密钥 IDcredential.secretID = @"secretID";// 临时密钥 keycredential.secretKey = @"secretKey";// 临时密钥 tokencredential.token = @"token";// 设置临时密钥put.credential = credential;// 本地文件路径NSURL* url = [NSURL fileURLWithPath:@"文件的URL"];// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketput.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"put.object = @"exampleobject";//需要上传的对象内容。可以传入 NSData* 或者 NSURL* 类型的变量put.body = url;//监听上传进度[put setSendProcessBlock:^(int64_t bytesSent,int64_t totalBytesSent,int64_t totalBytesExpectedToSend) {// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)// totalBytesSent 已发送的字节数// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)}];//监听上传结果[put setFinishBlock:^(id outputObject, NSError *error) {//可以从 outputObject 中获取 response 中 etag 或者自定义头部等信息NSDictionary * result = (NSDictionary *)outputObject;}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
// 以文件上传请求为例let put:QCloudCOSXMLUploadObjectRequest = QCloudCOSXMLUploadObjectRequest<AnyObject>();let credential = QCloudCredential()// 临时密钥 IDcredential.secretID = "secretID"// 临时密钥 Keycredential.secretKey = "secretKey"// 临时密钥 tokencredential.token = "token"// 设置临时密钥put.credential = credential// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketput.bucket = "examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"put.object = "exampleobject";//需要上传的对象内容。可以传入 NSData* 或者 NSURL* 类型的变量put.body = NSURL.fileURL(withPath: "Local File Path") as AnyObject;//监听上传结果put.setFinish { (result, error) in// 获取上传结果if error != nil{print(error!);}else{print(result!);}}//监听上传进度put.sendProcessBlock = { (bytesSent, totalBytesSent,totalBytesExpectedToSend) in// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)// totalBytesSent 已发送的字节数// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)};//设置上传参数put.initMultipleUploadFinishBlock = {(multipleUploadInitResult, resumeData) in//在初始化分块上传完成以后会回调该 block,在这里可以获取 resumeData//并且可以通过 resumeData 生成一个分块上传的请求let resumeUploadRequest = QCloudCOSXMLUploadObjectRequest<AnyObject>.init(request: resumeData as Data?);}QCloudCOSTransferMangerService.defaultCOSTransferManager().uploadObject(put);
//AppDelegate.m//AppDelegate 需遵循 QCloudSignatureProvider@interface AppDelegate()<QCloudSignatureProvider>@end@implementation AppDelegate- (BOOL)application:(UIApplication * )applicationdidFinishLaunchingWithOptions:(NSDictionary * )launchOptions {QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];endpoint.regionName = @"COS_REGION";endpoint.useHTTPS = true;configuration.endpoint = endpoint;// 设置密钥回调代理configuration.signatureProvider = self;[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];return YES;}// 密钥回调的代理方法- (void)signatureWithFields:(QCloudSignatureFields*)filedsrequest:(QCloudBizHTTPRequest*)requesturlRequest:(NSMutableURLRequest*)urlRequstcompelete:(QCloudHTTPAuthentationContinueBlock)continueBlock{/*** 此处获取密钥,如果使用永久密钥则忽略* ...*//*** 此处既可以是临时密钥也可以是永久密钥* 注意:永久密钥不可用于生产环境,仅可作为调试使用*/QCloudCredential* credential = [QCloudCredential new];// 密钥 SecretIdcredential.secretID = @"SECRETID";// 密钥 SecretKeycredential.secretKey = @"SECRETKEY";// 密钥 Token(如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入)credential.token = @"TOKEN";// 强烈建议返回服务器时间作为签名的开始时间, 用来避免由于用户手机本地时间偏差过大导致的签名不正确(参数 startTime 和 expiredTime 单位为秒)credential.startDate = [NSDate dateWithTimeIntervalSince1970:startTime]; // 单位是秒credential.expirationDate = [NSDate dateWithTimeIntervalSince1970:expiredTime]];// 单位是秒QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc]initWithCredential:credential];// SDK 中默认下面所有 Header 都参与签名,无需设置。// 若需制定某个 Header 不参与签名,则将相应的 Header 从下面数组中删除,然后赋值给 shouldSignedList 即可。// creator.shouldSignedList = @[@"Cache-Control", @"Content-Disposition", @"Content-Encoding", @"Content-Length", @"Content-MD5", @"Content-Type", @"Expect", @"Expires", @"If-Match" , @"If-Modified-Since" , @"If-None-Match" , @"If-Unmodified-Since" , @"Origin" , @"Range" , @"transfer-encoding" ,@"Host",@"Pic-Operations",@"ci-process"];// 注意 这里不要对 urlRequst 进行 copy 以及 mutableCopy 操作QCloudSignature *signature = [creator signatureForData:urlRequst];continueBlock(signature, nil);}@end
//AppDelegate.swift//AppDelegate 需遵循 QCloudSignatureProviderclass AppDelegate: UIResponder, UIApplicationDelegate,QCloudSignatureProvider {func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();endpoint.regionName = "COS_REGION";endpoint.useHTTPS = true;config.endpoint = endpoint;// 设置密钥回调代理config.signatureProvider = self;QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);return true}// 密钥回调的代理方法func signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {/*** 此处获取密钥,如果使用永久密钥则忽略* ...*//*** 此处既可以是临时密钥也可以是永久密钥* 临时密钥获取见:https://www.tencentcloud.com/document/product/436/14048* 注意:永久密钥不可用于生产环境,仅可作为调试使用*/let credential = QCloudCredential.init();// 密钥 SecretIdcredential.secretID = "SECRETID";// 密钥 SecretKeycredential.secretKey = "SECRETKEY";// 密钥 Token(如果使用永久密钥不需要填入,如果使用临时密钥需要填入)credential.token = "TOKEN";// 强烈建议返回服务器时间作为签名的开始时间, 用来避免由于用户手机本地时间偏差过大导致的签名不正确(参数 startTime 和 expiredTime 单位为秒)credential.startDate = Date.init(timeIntervalSince1970: TimeInterval(startTime)!) DateFormatter().date(from: "startTime");credential.expirationDate = Date.init(timeIntervalSince1970: TimeInterval(expiredTime)!)let creator = QCloudAuthentationV5Creator.init(credential: credential);// 注意:这里不要对 urlRequst 进行 copy 以及 mutableCopy 操作let signature = creator?.signature(forData: urlRequst);continueBlock(signature,nil);}}
import SwiftUIimport QCloudCOSXML@mainstruct SwiftUIDemoApp: App {init() {// 在应用初始化时调用 SDK 配置COSManager.shared.setupCOSSDK();}var body: some Scene {WindowGroup {ContentView()}}}// COSManager需遵循QCloudSignatureProviderclass COSManager: NSObject, QCloudSignatureProvider {static let shared = COSManager()private override init() {}func setupCOSSDK() {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();endpoint.regionName = "COS_REGION";endpoint.useHTTPS = true;config.endpoint = endpoint;config.signatureProvider = self;QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);}// 密钥回调的代理方法func signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {/*** 此处获取密钥,如果使用永久密钥则忽略* ...*//*** 此处既可以是临时密钥也可以是永久密钥* 临时密钥获取见:https://www.tencentcloud.com/document/product/436/14048* 注意:永久密钥不可用于生产环境,仅可作为调试使用*/let credential = QCloudCredential.init();// 密钥 SecretIdcredential.secretID = "SECRETID";// 密钥 SecretKeycredential.secretKey = "SECRETKEY";// 密钥 Token(如果使用永久密钥不需要填入,如果使用临时密钥需要填入)credential.token = "TOKEN";// 强烈建议返回服务器时间作为签名的开始时间, 用来避免由于用户手机本地时间偏差过大导致的签名不正确(参数 startTime 和 expiredTime 单位为秒)credential.startDate = Date.init(timeIntervalSince1970: TimeInterval(startTime)!) DateFormatter().date(from: "startTime");credential.expirationDate = Date.init(timeIntervalSince1970: TimeInterval(expiredTime)!)let creator = QCloudAuthentationV5Creator.init(credential: credential);// 注意:这里不要对 urlRequst 进行 copy 以及 mutableCopy 操作let signature = creator?.signature(forData: urlRequst);continueBlock(signature,nil);}}
// AppDelegate需遵循QCloudCredentailFenceQueueDelegate@interface AppDelegate () <QCloudSignatureProvider, QCloudCredentailFenceQueueDelegate>// 控制栅栏机制的对象@property (nonatomic, strong) QCloudCredentailFenceQueue *credentialFenceQueue;@end@implementation AppDelegate- (BOOL)application:(UIApplication * )applicationdidFinishLaunchingWithOptions:(NSDictionary * )launchOptions {// ... SDK 初始化相关的流程此处省略self.credentialFenceQueue = [QCloudCredentailFenceQueue new];// 设置栅栏机制的代理对象self.credentialFenceQueue.delegate = self;return YES;}// 密钥回调代理中需要使用栅栏对象获取签名- (void)signatureWithFields:(QCloudSignatureFields *)filedsrequest:(QCloudBizHTTPRequest *)requesturlRequest:(NSMutableURLRequest *)urlRequstcompelete:(QCloudHTTPAuthentationContinueBlock)continueBlock {[self.credentialFenceQueue performAction:^(QCloudAuthentationCreator *creator, NSError *error) {if (error) {continueBlock(nil, error);} else {QCloudSignature *signature = [creator signatureForData:urlRequst];continueBlock(signature, nil);}}];}// 栅栏机制的回调代理中获取密钥- (void)fenceQueue:(QCloudCredentailFenceQueue *)queue requestCreatorWithContinue:(QCloudCredentailFenceQueueContinue)continueBlock {[[[NSURLSession sharedSession]dataTaskWithURL:[NSURL URLWithString:@"http://******"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {// 从业务服务接口请求临时密钥。NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingTopLevelDictionaryAssumed error:nil];NSDictionary * credentials = dic[@"credentials"];QCloudCredential *credential = [QCloudCredential new];credential.secretID = credentials[@"tmpSecretId"];credential.secretKey = credentials[@"tmpSecretKey"];;credential.token = credentials[@"sessionToken"];;//签名过期时间credential.expirationDate = nil;credential.startDate = nil;QCloudAuthentationV5Creator *creator = [[QCloudAuthentationV5Creator alloc] initWithCredential:credential];continueBlock(creator, nil);}] resume];}@end
import UIKitimport QCloudCOSXML@mainclass AppDelegate: UIResponder, UIApplicationDelegate, QCloudSignatureProvider, QCloudCredentailFenceQueueDelegate {var window: UIWindow?var credentialFenceQueue: QCloudCredentailFenceQueue!func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {// ... SDK 初始化相关的流程此处省略credentialFenceQueue = QCloudCredentailFenceQueue()credentialFenceQueue.delegate = selfreturn true}// MARK: - QCloudSignatureProviderfunc signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {credentialFenceQueue.performAction { (creator: QCloudAuthentationCreator?, error: Error?) inif let error = error {continueBlock(nil, error)} else if let creator = creator {let signature = creator.signature(for: urlRequst)continueBlock(signature, nil)}}}// MARK: - QCloudCredentailFenceQueueDelegatefunc fenceQueue(_ queue: QCloudCredentailFenceQueue!, requestCreatorWithContinue continueBlock: QCloudCredentailFenceQueueContinue!) {// 构建请求临时密钥的 URL (请替换为您实际的服务器地址)guard let url = URL(string: "http://******") else {// 处理 URL 构造失败let error = NSError(domain: "AppDelegate", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL for temporary key"])continueBlock(nil, error)return}let task = URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in// 处理网络错误if let error = error {continueBlock(nil, error)return}// 确保数据存在guard let data = data else {let error = NSError(domain: "AppDelegate", code: -2, userInfo: [NSLocalizedDescriptionKey: "No data received from server"])continueBlock(nil, error)return}do {// 解析 JSON 数据if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],let credentials = json["credentials"] as? [String: Any],let tmpSecretId = credentials["tmpSecretId"] as? String,let tmpSecretKey = credentials["tmpSecretKey"] as? String,let sessionToken = credentials["sessionToken"] as? String {// 创建 QCloudCredential 对象并设置临时密钥let credential = QCloudCredential()credential.secretID = tmpSecretIdcredential.secretKey = tmpSecretKeycredential.token = sessionToken// 如果服务器返回了 startTime 和 expiredTime,建议设置// credential.startDate = Date(timeIntervalSince1970: startTime)// credential.expirationDate = Date(timeIntervalSince1970: expiredTime)let creator = QCloudAuthentationV5Creator(credential: credential)continueBlock(creator, nil)} else {// JSON 解析失败或键值缺失let error = NSError(domain: "AppDelegate", code: -3, userInfo: [NSLocalizedDescriptionKey: "Failed to parse temporary key response"])continueBlock(nil, error)}} catch {// JSON 解析异常continueBlock(nil, error)}}task.resume() // 启动网络任务}}
import SwiftUIimport QCloudCOSXML@mainstruct SwiftUIDemoApp: App {init() {// 在应用初始化时调用 SDK 配置COSManager.shared.setupCOSSDK();}var body: some Scene {WindowGroup {ContentView()}}}class COSManager: NSObject, QCloudSignatureProvider, QCloudCredentailFenceQueueDelegate{static let shared = COSManager()private let credentialFenceQueue = QCloudCredentailFenceQueue()private override init() {}func setupCOSSDK() {// ... SDK 初始化相关的流程此处省略credentialFenceQueue.delegate = self}// MARK: - QCloudSignatureProviderfunc signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {credentialFenceQueue.performAction { (creator: QCloudAuthentationCreator?, error: Error?) inif let error = error {continueBlock(nil, error)} else if let creator = creator {let signature = creator.signature(for: urlRequst)continueBlock(signature, nil)}}}// MARK: - QCloudCredentailFenceQueueDelegatefunc fenceQueue(_ queue: QCloudCredentailFenceQueue!, requestCreatorWithContinue continueBlock: QCloudCredentailFenceQueueContinue!) {// 构建请求临时密钥的 URL (请替换为您实际的服务器地址)guard let url = URL(string: "http://******") else {// 处理 URL 构造失败let error = NSError(domain: "AppDelegate", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL for temporary key"])continueBlock(nil, error)return}let task = URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in// 处理网络错误if let error = error {continueBlock(nil, error)return}// 确保数据存在guard let data = data else {let error = NSError(domain: "AppDelegate", code: -2, userInfo: [NSLocalizedDescriptionKey: "No data received from server"])continueBlock(nil, error)return}do {// 解析 JSON 数据if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],let credentials = json["credentials"] as? [String: Any],let tmpSecretId = credentials["tmpSecretId"] as? String,let tmpSecretKey = credentials["tmpSecretKey"] as? String,let sessionToken = credentials["sessionToken"] as? String {// 创建 QCloudCredential 对象并设置临时密钥let credential = QCloudCredential()credential.secretID = tmpSecretIdcredential.secretKey = tmpSecretKeycredential.token = sessionToken// 如果服务器返回了 startTime 和 expiredTime,建议设置// credential.startDate = Date(timeIntervalSince1970: startTime)// credential.expirationDate = Date(timeIntervalSince1970: expiredTime)let creator = QCloudAuthentationV5Creator(credential: credential)continueBlock(creator, nil)} else {// JSON 解析失败或键值缺失let error = NSError(domain: "AppDelegate", code: -3, userInfo: [NSLocalizedDescriptionKey: "Failed to parse temporary key response"])continueBlock(nil, error)}} catch {// JSON 解析异常continueBlock(nil, error)}}task.resume() // 启动网络任务}}
//AppDelegate.m//AppDelegate 需遵循 QCloudSignatureProvider@interface AppDelegate()<QCloudSignatureProvider>@end@implementation AppDelegate- (BOOL)application:(UIApplication * )applicationdidFinishLaunchingWithOptions:(NSDictionary * )launchOptions {QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = @"COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;configuration.endpoint = endpoint;// 密钥提供者为自己configuration.signatureProvider = self;// 初始化 COS 服务示例[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];return YES;}- (void)signatureWithFields:(QCloudSignatureFields*)filedsrequest:(QCloudBizHTTPRequest*)requesturlRequest:(NSMutableURLRequest*)urlRequstcompelete:(QCloudHTTPAuthentationContinueBlock)continueBlock{// 签名过期时间NSDate *expiration = [[[NSDateFormatter alloc] init]dateFromString:@"expiredTime"];QCloudSignature *signature = [[QCloudSignature alloc] initWithSignature:@"后台计算好的签名" expiration:expiration];signature.token = @"临时密钥Token";// 永久密钥不需要 TokencontinueBlock(signature, nil);}@end
//AppDelegate.swift//AppDelegate 需遵循 QCloudSignatureProviderclass AppDelegate: UIResponder, UIApplicationDelegate,QCloudSignatureProvider {func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey: Any]?) -> Bool {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = "COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;config.endpoint = endpoint;// 密钥提供者为自己config.signatureProvider = self;// 初始化 COS 服务示例QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);return true}func signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {// 签名过期时间let expiration = DateFormatter().date(from: "expiredTime");let signature = QCloudSignature.init(signature: "后台计算好的签名",expiration: expiration);signature.token = "临时密钥Token";// 永久密钥不需要 TokencontinueBlock(signature,nil);}}
import SwiftUIimport QCloudCOSXML@mainstruct SwiftUIDemoApp: App {init() {// 在应用初始化时调用 SDK 配置COSManager.shared.setupCOSSDK();}var body: some Scene {WindowGroup {ContentView()}}}class COSManager: NSObject, QCloudSignatureProvider {static let shared = COSManager()private override init() {}func setupCOSSDK() {let config = QCloudServiceConfiguration.init();let endpoint = QCloudCOSXMLEndPoint.init();// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见 https://www.qcloud.com/document/product/436/6224endpoint.regionName = "COS_REGION";// 使用 HTTPSendpoint.useHTTPS = true;config.endpoint = endpoint;config.signatureProvider = self;// 初始化 COS 服务示例QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);}// 获取签名的方法入口,这里演示了获取临时密钥并计算签名的过程// 您也可以自定义计算签名的过程func signature(with fileds: QCloudSignatureFields!,request: QCloudBizHTTPRequest!,urlRequest urlRequst: NSMutableURLRequest!,compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {// 签名过期时间let expiration = DateFormatter().date(from: "expiredTime");let signature = QCloudSignature.init(signature: "后台计算好的签名",expiration: expiration);signature.token = "临时密钥Token";// 永久密钥不需要 TokencontinueBlock(signature,nil);}}
NSString *customDomain = @"exampledomain.com"; // 自定义源站域名或全球加速域名QCloudCOSXMLEndPoint *endpoint = [[QCloudCOSXMLEndPoint alloc] initWithLiteralURL:[NSURL URLWithString:customDomain]];QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见https://www.qcloud.com/document/product/436/6224// 使用 HTTPSendpoint.regionName = @"COS_REGION";endpoint.useHTTPS = true;configuration.endpoint = endpoint;// 初始化 COS 服务示例[QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];[QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
let endpoint = QCloudCOSXMLEndPoint.init(literalURL: NSURL.init(string: "exampledomain.com") as URL?);let config = QCloudServiceConfiguration.init();// 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.tencentcloud.com/cos5/bucket// COS 支持的所有 region 列表参见https://www.qcloud.com/document/product/436/6224// 使用 HTTPSendpoint.useHTTPS = true;endpoint.regionName = "COS_REGION";config.endpoint = endpoint;// 初始化 COS 服务示例QCloudCOSXMLService.registerDefaultCOSXML(with: config);QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(with: config);
// 设置并发数4[QCloudHTTPSessionManager shareClient].customConcurrentCount = 4;// 设置自动上限8[QCloudHTTPSessionManager shareClient].maxConcurrencyTask = 8;
// 设置并发数4QCloudHTTPSessionManager.shareClient.customConcurrentCount = 4;// 设置自动上限8QCloudHTTPSessionManager.shareClient.maxConcurrencyTask = 8;
QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];// 本地文件路径NSURL* url = [NSURL fileURLWithPath:@"文件的URL"];// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketput.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"put.object = @"exampleobject";//需要上传的对象内容。可以传入 NSData* 或者 NSURL* 类型的变量put.body = url;//监听上传进度[put setSendProcessBlock:^(int64_t bytesSent,int64_t totalBytesSent,int64_t totalBytesExpectedToSend) {// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)// totalBytesSent 已发送的字节数// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)}];//监听上传结果[put setFinishBlock:^(id outputObject, NSError *error) {//可以从 outputObject 中获取 response 中 etag 或者自定义头部等信息NSDictionary * result = (NSDictionary *)outputObject;}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
let put:QCloudCOSXMLUploadObjectRequest = QCloudCOSXMLUploadObjectRequest<AnyObject>();// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketput.bucket = "examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"put.object = "exampleobject";//需要上传的对象内容。可以传入 NSData* 或者 NSURL* 类型的变量put.body = NSURL.fileURL(withPath: "Local File Path") as AnyObject;//监听上传结果put.setFinish { (result, error) in// 获取上传结果if error != nil{print(error!);}else{print(result!);}}//监听上传进度put.sendProcessBlock = { (bytesSent, totalBytesSent,totalBytesExpectedToSend) in// bytesSent 本次要发送的字节数(一个大文件可能要分多次发送)// totalBytesSent 已发送的字节数// totalBytesExpectedToSend 本次上传要发送的总字节数(即一个文件大小)};//设置上传参数put.initMultipleUploadFinishBlock = {(multipleUploadInitResult, resumeData) in//在初始化分块上传完成以后会回调该 block,在这里可以获取 resumeData//并且可以通过 resumeData 生成一个分块上传的请求let resumeUploadRequest = QCloudCOSXMLUploadObjectRequest<AnyObject>.init(request: resumeData as Data?);}QCloudCOSTransferMangerService.defaultCOSTransferManager().uploadObject(put);
QCloudCOSXMLDownloadObjectRequest * request = [QCloudCOSXMLDownloadObjectRequest new];// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketrequest.bucket = @"examplebucket-1250000000";// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"request.object = @"exampleobject";//设置下载的路径 URL,如果设置了,文件将会被下载到指定路径中request.downloadingURL = [NSURL fileURLWithPath:@"Local File Path"];//监听下载结果[request setFinishBlock:^(id outputObject, NSError *error) {//outputObject 包含所有的响应 http 头部NSDictionary* info = (NSDictionary *) outputObject;}];//监听下载进度[request setDownProcessBlock:^(int64_t bytesDownload,int64_t totalBytesDownload,int64_t totalBytesExpectedToDownload) {// bytesDownload 本次要下载的字节数(一个大文件可能要分多次发送)// totalBytesDownload 已下载的字节数// totalBytesExpectedToDownload 本次要下载的总字节数(即一个文件大小)}];[[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request];
let request : QCloudCOSXMLDownloadObjectRequest = QCloudCOSXMLDownloadObjectRequest();// 存储桶名称,由 BucketName-Appid 组成,可以在 COS 控制台查看 https://console.tencentcloud.com/cos5/bucketrequest.bucket = "examplebucket-1250000000";// 对象键request.object = "exampleobject";//设置下载的路径 URL,如果设置了,文件将会被下载到指定路径中request.downloadingURL = NSURL.fileURL(withPath: "Local File Path") as URL?;//监听下载进度request.downProcessBlock = { (bytesDownload, totalBytesDownload,totalBytesExpectedToDownload) in// bytesDownload 本次要下载的字节数(一个大文件可能要分多次发送)// totalBytesDownload 已下载的字节数// totalBytesExpectedToDownload 本次要下载的总字节数(即一个文件大小)}//监听下载结果request.finishBlock = { (copyResult, error) inif error != nil{print(error!);}else{print(copyResult!);}}QCloudCOSTransferMangerService.defaultCOSTransferManager().downloadObject(request);
文档反馈