QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];// Bucket name in the format of BucketName-Appid, which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketgetPresignedURLRequest.bucket = @"examplebucket-1250000000";// HTTP method of the request using a pre-signed URL. Valid values (case-sensitive): @"GET", @"PUT", @"POST", @"DELETE"getPresignedURLRequest.HTTPMethod = @"PUT";// Get the pre-signed URL function. By default, it is signed to the `host` header. You can also choose not to include the `host` header in the signature, but the request may fail or vulnerabilities may occur.getPresignedURLRequest.signHost = YES;// HTTP request parameters, which should be the same as those passed to the actual request. This can prevent users from tampering with the HTTP request parameters.getPresignedURLRequest.requestParameters = @{@"param1":@"value1",@"param1":@"value1"};// HTTP request headers, which should be included in the actual request. This can prevent users from tampering with the HTTP request headers that are signed here.getPresignedURLRequest.requestHeaders = @{@"param1":@"value1",@"param1":@"value1"};// Object key, i.e., the full path of a COS object. If the object is in a directory, the path should be "video/xxx/movie.mp4"getPresignedURLRequest.object = @"exampleobject";[getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nonnull result,NSError * _Nonnull error) {// Pre-signed URLNSString* presignedURL = result.presienedURL;// Upload a file through the pre-signed URLNSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:result.presienedURL]];// Specify `HTTPMethod` as `PUT`request.HTTPMethod = @"PUT";// `fromData` is the file to be uploaded[[[NSURLSession sharedSession]uploadTaskWithRequest:request fromData:[@"testtest" dataUsingEncoding:NSUTF8StringEncoding] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {// View the result in the response}]resume];}];[[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];
let getPresign = QCloudGetPresignedURLRequest.init();// Bucket name in the format of BucketName-Appid, which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketgetPresign.bucket = "examplebucket-1250000000" ;// HTTP method of the request using a pre-signed URL. Valid values (case-sensitive):// @"GET", @"PUT", @"POST", @"DELETE"getPresign.httpMethod = "PUT";// Get the pre-signed URL function. By default, it is signed to the `host` header. You can also choose not to include the `host` header in the signature, but the request may fail or vulnerabilities may occur.getPresign.signHost = YES;// HTTP request parameters, which should be the same as those passed to the actual request. This can prevent users from tampering with the HTTP request parameters.getPresign.requestParameters = {"param1":"value1","param1":"value1"};// HTTP request headers, which should be included in the actual request. This can prevent users from tampering with the HTTP request headers that are signed here.getPresign.requestHeaders = {"param1":"value1","param1":"value1"};// Object key, i.e., the full path of a COS object. If the object is in a directory, the path should be "video/xxx/movie.mp4"getPresign.object = "exampleobject";getPresign.setFinish { (result, error) inif let result = result {let url = result.presienedURL// Upload a file through the pre-signed URLlet request = NSMutableURLRequest.init(url: NSURL.init(string:url) as! URL);// Specify `HTTPMethod` as `PUT`request.httpMethod = "PUT";// `fromData` is the file to be uploadedURLSession.shared.uploadTask(with: request, from: "testtest".data(using: String.Encoding.utf8)) { data, res, err in// View the result in the response}.resume()} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getPresignedURL(getPresign);
QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];// Bucket name in the format of BucketName-Appid, which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketgetPresignedURLRequest.bucket = @"examplebucket-1250000000";// HTTP method of the request using a pre-signed URL. Valid values (case-sensitive): @"GET", @"PUT", @"POST", @"DELETE"getPresignedURLRequest.HTTPMethod = @"GET";// Get the pre-signed URL function. By default, it is signed to the `host` header. You can also choose not to include the `host` header in the signature, but the request may fail or vulnerabilities may occur.getPresignedURLRequest.signHost = YES;// HTTP request parameters, which should be the same as those passed to the actual request. This can prevent users from tampering with the HTTP request parameters.getPresignedURLRequest.requestParameters = @{@"param1":@"value1",@"param1":@"value1"};// HTTP request headers, which should be included in the actual request. This can prevent users from tampering with the HTTP request headers that are signed here.getPresignedURLRequest.requestHeaders = @{@"param1":@"value1",@"param1":@"value1"};// Object key, i.e., the full path of a COS object. If the object is in a directory, the path should be "video/xxx/movie.mp4"getPresignedURLRequest.object = @"exampleobject";[getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nonnull result,NSError * _Nonnull error) {// Pre-signed URLNSString* presignedURL = result.presienedURL;// Download a file through the pre-signed URLNSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:presignedURL]];// Specify `HTTPMethod` as `GET`request.HTTPMethod = @"GET";[[[NSURLSession sharedSession]downloadTaskWithRequest:requestcompletionHandler:^(NSURL *_Nullable location, NSURLResponse *_Nullable response, NSError *_Nullable error) {// `location` is the local file path after successful download}] resume];}];[[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];
let getPresign = QCloudGetPresignedURLRequest.init();// Bucket name in the format of BucketName-Appid, which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketgetPresign.bucket = "examplebucket-1250000000" ;// HTTP method of the request using a pre-signed URL. Valid values (case-sensitive):// @"GET", @"PUT", @"POST", @"DELETE"getPresign.httpMethod = "GET";// Get the pre-signed URL function. By default, it is signed to the `host` header. You can also choose not to include the `host` header in the signature, but the request may fail or vulnerabilities may occur.getPresignedURLRequest.signHost = YES;// HTTP request parameters, which should be the same as those passed to the actual request. This can prevent users from tampering with the HTTP request parameters.getPresignedURLRequest.requestParameters = {"param1":"value1","param1":"value1"};// HTTP request headers, which should be included in the actual request. This can prevent users from tampering with the HTTP request headers that are signed here.getPresignedURLRequest.requestHeaders = {"param1":"value1","param1":"value1"};// Object key, i.e., the full path of a COS object. If the object is in a directory, the path should be "video/xxx/movie.mp4"getPresign.object = "exampleobject";getPresign.setFinish { (result, error) inif let result = result {let url = result.presienedURL// Download a file through the pre-signed URLlet request = NSMutableURLRequest.init(url: NSURL.init(string: url) as! URL);// Specify `HTTPMethod` as `GET`request.httpMethod = "GET";URLSession.shared.downloadTask(with: request) { location, response, error in// `location` is the local file path after successful download}.resume();} else {print(error!);}}QCloudCOSXMLService.defaultCOSXML().getPresignedURL(getPresign);
Was this page helpful?