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).
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:
Deploy cloud resources with a few clicks.
Open the Quick deployment template link in the ROS console.
On the Configure Template Parameters tab of the Use New Resources (Standard) page in the Resource orchestration ROS console, enter a stack name and the name of the 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.
In the left-side navigation pane, click Stacks. On the Stacks page, click the stack that you create. On the Stack Information tab, the status of the stack is Creating.
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.
Obtain signature information from the application server and upload data to OSS.
On the Outputs tab, copy the value of UploadPath and open it in your browser.
On the Transfer Data from Web Client to OSS page, click Select File, select a file of a specific type, and then click Upload.
Release test resources to avoid unnecessary costs.
In the upper-right corner of the stack page, click Delete.
In the Delete Stack dialog box, set Method to Delete the Stack to Release Resources and click OK.
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.
Step 1: Configure the application server
Download the application server source code package in Python.
In this example, Ubuntu 16.04 is used. Decompress the source code package to the /home/aliyun/aliyun-oss-appserver-python directory.
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
Download the client source code package.
Decompress the package to a directory. In this example, the D:\aliyun\aliyun-oss-appserver-js directory is used.
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'
serverUrlto 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
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.
Log on to the OSS console.
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
- In the left-side navigation tree, choose .
On the Cross-Origin Resource Sharing (CORS) page, click Create Rule and configure the parameters showed in the following figure.
Step 4: Send an upload callback request
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.
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, you must perform debugging.
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.