All Products
Search
Document Center

Object Storage Service:How do I add signatures to requests by using Python code on application servers,configure upload callbacks,and then use form upload to upload data to OSS?

Last Updated:Dec 16, 2024

This topic describes how to add signatures to requests by using Python code on application servers, configure upload callbacks, and then use form upload to upload data to Object Storage Service (OSS).

Procedure

ROS template file used in this topic: Python-ros.yml

Quick deployment

You can use Resource Orchestration Service (ROS) to configure one Elastic Compute Service (ECS) instance and one OSS bucket, and deploy the application server source code (Python) and client source code on the ECS instance. To use ROS to calculate signatures by using Python code on the application server, configure upload callbacks, and then use form upload to upload data to OSS, perform the following steps:

  1. Deploy cloud resources with a few clicks.

    1. Open the Quick deployment template link in the ROS console.

    2. In the Select Template step of the Create Stack wizard in the ROS console, enter a stack name and the name of an OSS bucket that you want to create. Specify the zone, instance type, system disk category, and password for the ECS instance that you want to purchase, and then click Create.

      On the Stack Information tab of the page that appears, the status of the stack is Creating.

    3. After the status of the stack becomes Created, click the Outputs tab to view the resources, such as the ECS instance and OSS bucket, that are deployed with a few clicks.

  2. Obtain signature information from the application server and upload data to OSS.

    1. On the Outputs tab, copy the value of UploadPath and open it in your browser.

    2. On the Transfer Data from Web Client to OSS page, click Select File, select a file of a specific type, and then click Upload.

  3. Release test resources to avoid unnecessary costs.

    1. In the upper-right corner of the stack page, click Delete.

    2. In the Delete Stack dialog box, set Method to Delete the Stack to Release Resources and click OK.

Manual deployment

Prerequisites

  • The domain name of the application server can be accessed over the Internet.

  • Python 2.6 or later is installed on the application server. To view the Python version, run the python --version command.

  • JavaScript is supported by the browser on your computer.

Step 1: Configure the application server

  1. Download the application server source code package in Python.

  2. In this example, Ubuntu 16.04 is used. Decompress the source code package to the /home/aliyun/aliyun-oss-appserver-python directory.

  3. Go to the directory, open the appserver.py file, and then modify the following snippet:

    # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
    access_key_id = 'yourAccessKeyId'
    access_key_secret = 'yourAccessKeySecret'
    # Specify the access address of the host in the https://bucketname.endpoint format. 
    host = 'https://examplebucket.oss-cn-hangzhou.aliyuncs.com'
    # Specify the URL of the application server to which an upload callback request is sent. This URL is used for communication between the application server and OSS. After you upload an object, OSS uses the URL to send information about the object upload to the application server. 
    callback_url = "https://192.168.0.0:8888"
    # Specify the prefix of the object that you want to upload. You can leave this parameter empty. If you do not specify this parameter, the object is uploaded to the root directory of the bucket. 
    upload_dir = 'exampledir/'

Step 2: Configure the client

  1. Download the client source code package.

  2. Decompress the package to a directory. In this example, the D:\aliyun\aliyun-oss-appserver-js directory is used.

  3. Go to the directory, open the upload.js file, and then find the following code:

    // serverUrl specifies the URL of the application server that returns information such as the signature and upload policies. Replace the value of serverUrl with the actual IP address and port number of the application server. 
    serverUrl = 'http://192.0.2.0:8888'
  4. Set serverUrl to the URL of the application server that returns information such as the signature and upload policies to the client. In this example, serverUrl is set to https://192.168.0.0:8888.

Step 3: Configure CORS

When you use form upload to upload data from the client to OSS, the client sends a request that contains the Origin header to OSS by using the browser. Then, OSS checks whether the request that contains the Origin header matches the cross-origin resource sharing (CORS) rules that you configured for the bucket. Therefore, you must configure CORS rules for the bucket before you use the POST method to upload data to the bucket. For more information, see CORS.

Step 4: Send an upload callback request

  1. Start the application server.

    In the /home/aliyun-oss-appserver-python directory, run the python appserver.py 11.22.XX.XX 1234 command to start the application server.

    Note

    Replace the IP address and port number with those of the application server that you configure.

  2. Start the client.

    On the client that is installed on your computer, open the index.html file in the directory of the client source code.

    Important

    The index.html file may be incompatible with Internet Explorer 10 or earlier. If you encounter any problems when you use Internet Explorer 10 or earlier, perform debugging.

  3. Upload a file.

    Click Select File, select a file of a specific type, and then click Upload. After you upload the object, the content that is returned by the application server is displayed.