Help Center/ Object Storage Service/ Best Practices/ Uploading Data to OBS/ Uploading Data from a Web Client to OBS and Setting an Upload Callback
Updated on 2025-08-21 GMT+08:00

Uploading Data from a Web Client to OBS and Setting an Upload Callback

Scenarios

As shown in Figure 1, files are commonly uploaded through a browser to the app server and then uploaded to OBS. Data transfer on the app server results in a low efficiency. Moreover, the app server will be heavily loaded if multiple tasks are concurrently uploaded.

Figure 1 Common method for uploading a file from a web client

This section describes how to use the PostObject API to directly upload a file from a web client to OBS and set an upload callback, which is called form-based upload. After the upload is successful, the specified address will receive a callback. Solution Architecture shows the solution architecture of form-based upload. In this solution, a file is directly uploaded from a web client to OBS without being transferred through the app server. This makes the transfer faster and does not impose pressure on the server. Additionally, using a signature returned by the server makes direct transfer more secure.

Solution Architecture

Figure 2 Directly uploading data using the PostObject API and setting an upload callback

The following describes the process of uploading a file from a web client to OBS and setting an upload callback:

  1. A user sends a request to the server through the web client to obtain an upload security policy.
  2. The server calculates the signature using the security credentials and upload security policy, and returns the upload security policy and signature to the web client.
  3. The user uses the HTML form constructed on the web client to upload a file to OBS and set an upload callback.
  4. OBS sends a callback request to the server.
  5. The server returns a response to OBS.
  6. OBS returns the upload result to the web client.

A security policy defines which content can be uploaded using a form. For example, you can specify in a policy that only objects whose name starts with prefix01 can be uploaded using a form.

Resource Planning

Table 1 Resource planning

Region

Resource

Resource Name

Description

Quantity

Billing

CN North-Beijing4

OBS bucket

post-callback-demo

The bucket used to store the uploaded file

You need to create a bucket on OBS Console.

1

You will be billed for the storage space occupied by the file. For details, see Storage Space.

-

File

demo-object.txt

The file to be uploaded

You need to prepare the file to be uploaded in advance.

1

-

-

AK/SK pair

AK/SK

The credential for accessing OBS. The server uses the AK/SK pair to generate a signature for user authentication.

For details about AKs and SKs, see Access Keys.

1

-

Operation Process

To upload a file from a web client to OBS and set an upload callback, do as follows:

  1. Create a bucket and configure a CORS rule: Create a bucket on OBS Console for storing the uploaded file. Configure a cross-origin resource sharing (CORS) rule for the bucket to allow cross-origin access from the web client.
  2. Request a signature from the server: Request the server to generate a signature. The signature is generated using the security credentials and upload policy (with the bucket name, object name prefix, expiration time, and others included), which authorizes you to complete the file upload within a specified time.
  3. Upload a file using a form and set an upload callback: Construct a POST request in an HTML form on the web client and set an upload callback.

Procedure

Verification

The HTML form contains a file selection box and an upload button. You can select the file to be uploaded and submit the form.

When the form is submitted, JavaScript sends a request to the server to obtain the required signature and callback information. Once a valid response is received, the FormData object is constructed and populated with all required data. A POST request is then made to the OBS server using the fetch method to complete the file upload. Once the upload succeeds, a callback is triggered to the address specified by callbackUrl.

Upload the demo-object file to bucket post-callback-demo using the form. If the demo-object file can be found in the bucket post-callback-demo, the file is successfully uploaded.

Figure 3 Checking the uploaded file

References

To calculate POST upload signatures using web frameworks and OBS SDKs of other programming languages, refer to the following:

Java

Python

C: not supported

Go: not supported

BrowserJS

.NET: not supported

Android

iOS: not supported

PHP

Node.js