Setting a Callback for a Resumable Upload
Function
You can specify the callback parameters in a resumable upload (uploadFile) request. After the object is successfully uploaded, OBS calls back the upload result to a specific server and returns the callback result to you.
Method
1 2 |
- (OBSBFTask*)uploadFile:(OBSUploadFileRequest *)request completionHandler:(void (^)(OBSUploadFileResponse * response, NSError * error))completionHandler; |
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
bucketName |
NSString * |
Yes |
Definition: Bucket name. Restrictions:
Default value: None |
objectKey |
NSString * |
Yes |
Definition: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name. For example, if the access path is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value can contain 1 to 1,024 characters. Default value: None |
uploadFilePath |
NSString * |
Yes |
Definition: File to be uploaded. Default value: None |
enableCheckpoint |
BOOL |
Yes |
Definition: Whether to enable the resumable mode. Value range:
Default value: NO |
checkpointFilePath |
NSString * |
No |
Definition: Path of a file generated for recording the progress of a resumable download. The file contains the information about parts and the progress. Restrictions: This parameter is valid only when the resumable mode is enabled. |
partSize |
NSNumber * |
Yes |
Definition: Part size. Value range: The value ranges from 100 KB to 5 GB, in bytes. Default value: 5MB |
callBack |
No |
Definition: Upload callback parameters. Value range: For details, see Table 2. Default value: None |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
callbackUrl |
NSString * |
Yes |
Definition: After an object is uploaded successfully, OBS sends a callback request to the URL using the POST method. Restrictions:
|
callbackHost |
NSString * |
No |
Definition: Value of the Host header in the callback request. Restrictions: If callbackHost is not specified, the value of host parsed from the callbackUrl parameter is used. |
callbackBody |
NSString * |
Yes |
Definition: Body of the callback request. Restrictions: The body format must comply with the media type specified in the callbackBodyType field. Default value: The callback body supports system variables and custom variables. Custom variables are those starting with x:. For example, in key=$(key)&hash=$(etag)&fileid=$(x:fileid), variables key and etag are system variables, and x:fileid is a custom variable. If the object to be uploaded is an image, you can use imageInfo.width and imageInfo.height in the parameter to indicate the width and height of the image. Example: key=$(key)&hash=$(etag)&w=$(imageInfo.width)&h=$(imageInfo.height) |
callbackBodyType |
NSString * |
No |
Definition: Value of the Content-Type header in the callback request. Value range:
Default value: If this parameter is not set, the default value application/json is used. |
Responses
Parameter |
Type |
Description |
---|---|---|
statusCode |
NSString * |
Definition: HTTP status code. Value range: A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For more information, see Status Code. Default value: None |
headers |
NSDictionary * |
Definition: Response header list, composed of tuples. Default value: None |
etag |
NSString * |
Definition: ETag of an object, which is a Base64-encoded 128-bit MD5 digest. ETag is the unique identifier of the object content. It can be used to determine whether the object content is changed. For example, if the ETag is A when an object is uploaded and is B when the object is downloaded, the object content is changed. The ETag reflects changes only to the contents of the object, not its metadata. An object created by an upload or copy operation has a unique ETag. Restrictions: If an object is encrypted using server-side encryption, the ETag is not the MD5 value of the object. Value range: The value must contain 32 characters. Default value: None |
responseRawData |
NSMutableData * |
Definition: Raw data of the response body. Default value: None |
Code Examples
void testPutObjectWithCallback(){ // NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. NSString* endPoint = @"https://obs.ap-southeast-1.myhuaweicloud.com"; char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); char* securityToken_env = getenv("SecurityToken"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; NSString* securityToken = [NSString stringWithUTF8String:securityToken_env]; NSString* exampleBucketName = @"example-bucket-name"; NSString* exampleObjectKey = @"objectname.pic"; NSString* exampleFile = [[NSBundle mainBundle]pathForResource:@"pic" ofType:@"jpeg"];//@""; NSString* exampleCallbackUrl = @"www.example-url.com"; NSString* exampleCallbackBody = @"exampleCallbackBody"; NSString* exampleCallbackHost = @"www.example-host.com"; NSString* exampleCallbackBodyType = @"example-content-type"; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you must specify a security token. // credentialProvider.securityToken = securityToken; // Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize the client. OBSClient *client = [[OBSClient alloc] initWithConfiguration:conf]; // Initialize the object upload request putObjectRequest. OBSUploadFileRequest* uploadFileRequest = [[OBSUploadFileRequest alloc] initWithBucketName:exampleBucketName objectKey:exampleObjectKey uploadFilePath:exampleFile]; uploadFileRequest.partSize = [NSNumber numberWithInteger: 100 * 1024]; uploadFileRequest.callback = [[OBSCallback alloc] initWithUrl:exampleCallbackUrl withBody:exampleCallbackBody withBodyType:exampleCallbackBodyType withHost:exampleCallbackHost]; // Initialize the asynchronous upload task. OBSBFTask* uploadFileTask = [client uploadFile:uploadFileRequest completionHandler:^(OBSUploadFileResponse *response, NSError *error) { if(error){ // Upload failed. NSLog(@"UploadFile failed:%@", error); } if(response){ NSLog(@"UploadFile response:%@", response); NSString* callbackResponseString = [[NSString alloc] initWithData:response.responseRawData encoding:NSUTF8StringEncoding]; NSLog(@"UploadFile callbackResponseString:%@", callbackResponseString); } }]; // Wait until the asynchronous upload task is complete. [uploadFileTask waitUntilFinished]; }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot