QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketgetPresignedURLRequest.bucket = @"examplebucket-1250000000";// 使用预签名 URL 请求的 HTTP 方法。有效值(大小写敏感)为:@"GET"、@"PUT"、@"POST"、@"DELETE"getPresignedURLRequest.HTTPMethod = @"PUT";// 获取预签名函数,默认签入Header Host;您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞getPresignedURLRequest.signHost = YES;// http 请求参数,传入的请求参数需与实际请求相同,能够防止用户篡改此HTTP请求的参数getPresignedURLRequest.requestParameters = @{@"param1":@"value1",@"param1":@"value1"};// http 请求头部,传入的请求头部需包含在实际请求中,能够防止用户篡改签入此处的HTTP请求头部getPresignedURLRequest.requestHeaders = @{@"param1":@"value1",@"param1":@"value1"};// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"getPresignedURLRequest.object = @"exampleobject";[getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nonnull result,NSError * _Nonnull error) {// 预签名 URLNSString* presignedURL = result.presienedURL;// 使用预签名链接进行上传文件NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:result.presienedURL]];// 指定HTTPMethod 为PUTrequest.HTTPMethod = @"PUT";// fromData 为需要上传的文件[[[NSURLSession sharedSession]uploadTaskWithRequest:request fromData:[@"testtest" dataUsingEncoding:NSUTF8StringEncoding] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {// response中查看上传结果}]resume];}];[[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];
let getPresign = QCloudGetPresignedURLRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketgetPresign.bucket = "examplebucket-1250000000" ;// 使用预签名 URL 请求的 HTTP 方法。有效值(大小写敏感)为:// @"GET"、@"PUT"、@"POST"、@"DELETE"getPresign.httpMethod = "PUT";// 获取预签名函数,默认签入Header Host;您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞getPresign.signHost = YES;// http 请求参数,传入的请求参数需与实际请求相同,能够防止用户篡改此HTTP请求的参数getPresign.requestParameters = {"param1":"value1","param1":"value1"};// http 请求头部,传入的请求头部需包含在实际请求中,能够防止用户篡改签入此处的HTTP请求头部getPresign.requestHeaders = {"param1":"value1","param1":"value1"};// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"getPresign.object = "exampleobject";getPresign.setFinish { (result, error) inif let result = result {let url = result.presienedURL// 使用预签名链接进行上传文件let request = NSMutableURLRequest.init(url: NSURL.init(string:url) as! URL);// 指定HTTPMethod 为PUTrequest.httpMethod = "PUT";// fromData 为需要上传的文件URLSession.shared.uploadTask(with: request, from: "testtest".data(using: String.Encoding.utf8)) { data, res, err in// response中查看上传结果}.resume()} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getPresignedURL(getPresign);
QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketgetPresignedURLRequest.bucket = @"examplebucket-1250000000";// 使用预签名 URL 请求的 HTTP 方法。有效值(大小写敏感)为:@"GET"、@"PUT"、@"POST"、@"DELETE"getPresignedURLRequest.HTTPMethod = @"GET";// 获取预签名函数,默认签入Header Host;您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞getPresignedURLRequest.signHost = YES;// http 请求参数,传入的请求参数需与实际请求相同,能够防止用户篡改此HTTP请求的参数getPresignedURLRequest.requestParameters = @{@"param1":@"value1",@"param1":@"value1"};// http 请求头部,传入的请求头部需包含在实际请求中,能够防止用户篡改签入此处的HTTP请求头部getPresignedURLRequest.requestHeaders = @{@"param1":@"value1",@"param1":@"value1"};// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"getPresignedURLRequest.object = @"exampleobject";[getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nonnull result,NSError * _Nonnull error) {// 预签名 URLNSString* presignedURL = result.presienedURL;// 使用预签名链接进行下载文件NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:presignedURL]];// 指定HTTPMethod为GETrequest.HTTPMethod = @"GET";[[[NSURLSession sharedSession]downloadTaskWithRequest:requestcompletionHandler:^(NSURL *_Nullable location, NSURLResponse *_Nullable response, NSError *_Nullable error) {// location 下载成功后的本地文件路径}] resume];}];[[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];
let getPresign = QCloudGetPresignedURLRequest.init();// 存储桶名称,由BucketName-Appid 组成,可以在COS控制台查看 https://console.tencentcloud.com/cos5/bucketgetPresign.bucket = "examplebucket-1250000000" ;// 使用预签名 URL 请求的 HTTP 方法。有效值(大小写敏感)为:// @"GET"、@"PUT"、@"POST"、@"DELETE"getPresign.httpMethod = "GET";// 获取预签名函数,默认签入Header Host;您也可以选择不签入Header Host,但可能导致请求失败或安全漏洞getPresignedURLRequest.signHost = YES;// http 请求参数,传入的请求参数需与实际请求相同,能够防止用户篡改此HTTP请求的参数getPresignedURLRequest.requestParameters = {"param1":"value1","param1":"value1"};// http 请求头部,传入的请求头部需包含在实际请求中,能够防止用户篡改签入此处的HTTP请求头部getPresignedURLRequest.requestHeaders = {"param1":"value1","param1":"value1"};// 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "video/xxx/movie.mp4"getPresign.object = "exampleobject";getPresign.setFinish { (result, error) inif let result = result {let url = result.presienedURL// 使用预签名链接进行下载文件let request = NSMutableURLRequest.init(url: NSURL.init(string: url) as! URL);// 指定HTTPMethod为GETrequest.httpMethod = "GET";URLSession.shared.downloadTask(with: request) { location, response, error in// location 下载成功后的本地文件路径}.resume();} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getPresignedURL(getPresign);
本页内容是否解决了您的问题?