Setting a Callback for a Regular Upload
Function
You can specify the callback parameters in an object upload (PutObject) 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*)putObject:(OBSPutObjectWithFileRequest *)request completionHandler:(void (^)(OBSPutObjectResponse *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 |
callBack |
No |
Definition: Upload callback parameters. 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 |
versionID |
NSString * |
Definition: Object version ID. If versioning is enabled for the bucket, the object version ID will be returned. Value range: The value must contain 32 characters. 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
This example sets a callback for a regular object upload (PutObject) request.
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. OBSPutObjectWithFileRequest * putObjectRequest = [[OBSPutObjectWithFileRequest alloc] initWithBucketName:exampleBucketName objectKey:exampleObjectKey uploadFilePath:exampleFile]; putObjectRequest.callback = [[OBSCallback alloc] initWithUrl:exampleCallbackUrl withBody:exampleCallbackBody withBodyType:exampleCallbackBodyType withHost:exampleCallbackHost]; // Initialize the asynchronous upload task. OBSBFTask* putObjectTask = [client putObject:putObjectRequest completionHandler:^(OBSPutObjectResponse *response, NSError *error) { if(error){ // Upload failed. NSLog(@"PutObject failed:%@", error); } if(response){ NSLog(@"PutObject response:%@", response); NSString* callbackResponseString = [[NSString alloc] initWithData:response.responseRawData encoding:NSUTF8StringEncoding]; NSLog(@"PutObject callbackResponseString:%@", callbackResponseString); } }]; // Wait until the asynchronous upload task is complete. [putObjectTask 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