Pre-signed URL

Last updated: 2019-08-19 10:24:11

PDF

This document describes how to perform operations such as upload and download with a pre-signed URL which can be generated by either the SDK or the server.

  • It is assumed that you have downloaded, installed, and initialized the SDK as instructed in Getting Started.
  • You are recommended to use Command+F to search for the API you want to query, check the provided descriptions of the API, and then copy the sample code to your project for execution.

    If you want to learn more about the function of the API or the meanings of its parameters, you are recommended to directly view the comments in the code. In Xcode, you can use three-finger tap or Force Touch or hover over a variable and press Control+Command+D to see its interpretation.

Directions

  1. Generate a QCloudGetPresignedURLRequest instance.
  2. Enter the required information such as the bucket, object, and HTTPMethod of the request.
  3. If you add an additional HTTP header or parameter to the request, you should call the corresponding method in QCloudGetPresignedURLRequest to add them to the pre-signed URL when it is generated.
  4. Call getPresignedURL in QCloudCOSXMLService to make the request and get the pre-signed URL in the result.

QCloudGetPresignedURLRequest Parameter Descriptions

Parameter Name Description Type Required
bucket Bucket name in the pre-signed request in the format of <BucketName-APPID>, such as examplebucket-1250000000, which can be viewed in the COS V5 Console NSString* Yes
object Object in the pre-signed request. An object key (Key) is a unique ID of an object in the bucket. For example, in the object's access domain name bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/text.txt, the object key is doc1/text.txt. For more information, see Object Overview NSString* Yes
HTTPMethod HTTP method in the request using a pre-signed URL. Value range (case sensitive): @"GET", @"PUT", @"POST", @"DELETE" NSString* Yes
contentType Specifies the HTTP body encoding type for the request and response NSString* No
contentMD5 MD5 value of the file NSString* No

If needed, you can set the header or URL parameter in the request generated by the pre-signed URL in the following way:

/**
 Add a header to the pre-signed request

 @param value   HTTP header value
 @param requestHeader   HTTP header key
 */
- (void)setValue:(NSString * _Nullable)value forRequestHeader:(NSString * _Nullable)requestHeader;

/**
 Add a URL parameter to the pre-signed request

 @param value   Parameter value
 @param requestParameter   Parameter key
 */
- (void)setValue:(NSString * _Nullable)value forRequestParameter:(NSString *_Nullable)requestParameter;

Example of Getting a Pre-signed URL

QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];
getPresignedURLRequest.bucket = @“examplebucket-1250000000”;
getPresignedURLRequest.HTTPMethod = @"GET";
getPresignedURLRequest.object = @"text.txt";
[getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nonnull result, NSError * _Nonnull error) {
if (nil == error) {
 NSString* presignedURL = result.presienedURL;
}
}
[[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];

Example of Using a Pre-signed URL

Below is an example of download with a pre-signed URL.

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"pre-signed URL"]];
request.HTTPMethod = @"GET";
request.HTTPBody = [@"file content" dataUsingEncoding:NSUTF8StringEncoding];
[[[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSInteger statusCode = [(NSHTTPURLResponse*)response statusCode];
}] resume];