Upload Callback

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
You can specify the callback parameters in an object upload request. After the object is successfully uploaded, OBS calls back the upload result to a specific server and returns the callback result to you.
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
CallbackUrl |
string |
Yes |
Definition: After an object is uploaded successfully, OBS sends a callback request to the URL using the POST method. Restrictions:
Default value: None |
CallbackHost |
string |
No |
Definition: Value of the Host header in the callback request. Default value: If CallbackHost is not specified, the value of host parsed from the CallbackUrl parameter is used. |
CallbackBody |
string |
Yes |
Definition: Body of the callback request. Restrictions:
Default value: None |
CallbackBodyType |
string |
No |
Definition: Value of the Content-Type header in the callback request. Value range:
Default value: application/json |
Variable |
Type |
Description |
---|---|---|
fname |
string |
Definition: Name of the original file to be uploaded. |
fsize |
string |
Definition: Size of the file, in bytes. The name is compatible with size. |
etag |
string |
Definition: Object ETag. |
bucket |
string |
Definition: Name of the bucket to which the object is uploaded. |
key |
string |
Definition: The name of the uploaded object. 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.cn-north-4.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 |
ext |
string |
Definition: Extension of the file to be uploaded. It is automatically obtained from parameter mimeType or $(fname). The name is compatible with mimeType. |
override |
string |
Definition: Whether the uploaded object overwrites an existing one. Value range: The value can be true or false. If the name of the object to be uploaded already exists in the bucket, the existing object may be overwritten. true indicates that the existing object will be overwritten. false means the existing object will not be overwritten. When versioning is enabled for a bucket, override is always false. |
imageInfo |
string |
Definition: Obtained basic information about the image to be uploaded. This parameter can be used only in the image uploading scenarios. This variable contains the $(imageInfo.width) and $(imageInfo.height) fields, which indicate the width and height of an image.
NOTE:
If the size of an uploaded image exceeds 25 MB, the basic image information cannot be obtained using the magic variables related to imageInfo. As a result, the callback fails. |
// Create an instance of ObsClient. var obsClient = new ObsClient({ // 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. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); obsClient.putObject({ Bucket: 'bucketname', // An object name is a complete path that does not contain the bucket name. Key: 'objectname', // File to be uploaded. SourceFile: document.getElementById('input-file').files[0], Callback: { CallbackUrl: "http://example.com:80", CallbackBody: "filename=${object}&size=${size}&mimeType=${mimeType}", // CallbackHost: "example.com", // CallbackBodyType: "application/x-www-form-urlencoded", } }, function (err, result) { if(err){ console.error('Error-->' + err); }else{ console.log('Status-->' + result.CommonMsg.Status); console.log('CallbackResponse-->' + result.InterfaceResult.CallbackResponse); } });
// Create an instance of ObsClient. var obsClient = new ObsClient({ // 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. // The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable. // 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. access_key_id: process.env.AccessKeyID, secret_access_key: process.env.SecretAccessKey, // Replace the example endpoint with the actual one in your case. server: 'https://obs.ap-southeast-1.myhuaweicloud.com' }); var cp; var hook; obsClient.uploadFile({ Bucket : 'bucketname', Key : 'objectname', SourceFile : document.getElementById('input-file').files[0], PartSize : 9 * 1024 * 1024, ProgressCallback : function(transferredAmount, totalAmount, totalSeconds){ console.log(transferredAmount * 1.0 / totalSeconds / 1024); console.log(transferredAmount * 100.0 / totalAmount); if(hook && (transferredAmount / totalAmount) > 0.5){ // Pause the resumable upload. // hook.cancel(); } }, EventCallback : function(eventType, eventParam, eventResult){ // Handle event response. }, ResumeCallback : function(resumeHook, uploadCheckpoint){ // Obtain the control parameter for aborting the resumable upload. hook = resumeHook; // Record a breakpoint. cp = uploadCheckpoint; }, Callback: { CallbackUrl: "http://example.com:80", CallbackBody: "filename=${object}&size=${size}&mimeType=${mimeType}", // CallbackHost: "example.com", // CallbackBodyType: "application/x-www-form-urlencoded", } }, function(err, result){ console.error('Error-->' + err); // If an error occurs, call the resumable upload API again to continue the upload task. if(err){ obsClient.uploadFile({ UploadCheckpoint : cp, ProgressCallback : function(transferredAmount, totalAmount, totalSeconds){ console.log(transferredAmount * 1.0 / totalSeconds / 1024); console.log(transferredAmount * 100.0 / totalAmount); }, EventCallback : function(eventType, eventParam, eventResult){ // Handle event response. }, }, function(err, result){ if(err){ console.error('Error-->' + err); }else{ if(result.CommonMsg.Status < 300){ console.log('RequestId-->' + result.InterfaceResult.RequestId); console.log('CallbackResponse-->' + result.InterfaceResult.CallbackResponse); }else{ console.log('Code-->' + result.CommonMsg.Code); console.log('Message-->' + result.CommonMsg.Message); } } }); }else { console.log('Status-->' + result.CommonMsg.Status); if (result.CommonMsg.Status < 300 && result.InterfaceResult) { console.log('RequestId-->' + result.InterfaceResult.RequestId); } } });
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