This topic describes how to calculate signatures in Go on the server, configure upload callback, and use form upload to upload data to OSS.

Prerequisites

  • The domain name of the application server is accessible over the Internet.
  • The application server has Go 1.6 or later installed. To verify the Go version, run the go version command.
  • The browser on the PC supports JavaScript.

Step 1: Configure the application server

  1. Download the application server source code that is in Go to a directory of the application server.
  2. Ubuntu 16.04 is used in the example. Download the source code to the /home/aliyun/aliyun-oss-appserver-go directory.
  3. Go to the directory. Open the appserver.go file that contains the source code. Modify the following snippet:
    // Enter your AccessKey ID. 
    var accessKeyId string = "<yourAccessKeyId>"
    
    // Enter your AccessKey secret. 
    var accessKeySecret string = "<yourAccessKeySecret>"
    
    // Set host to a value that is in the format of bucketname.endpoint. 
    var host string = "https://bucket-name.oss-cn-hangzhou.aliyuncs.com'"
    
    // Specify the URL of the application server to which an upload callback request is sent. Replace the IP address and port number with your actual information. 
    var callbackUrl string = "http://192.0.2.0:8888";
    
    // Specify the prefix for the name of the object you want to upload. 
    var upload_dir string = "user-dir-prefix/"
    
    // Specify the validity period in seconds for the upload policy. 
    var expire_time int64 = 30
    • accessKeyId: Enter your AccessKey ID.
    • accessKeySecret: Enter your AccessKey secret.
    • host: The format is https://bucketname.endpoint. Example: https://bucket-name.oss-cn-hangzhou.aliyuncs.com. For more information about endpoints, see the "Endpoint" section of the Terms topic.
    • callbackUrl: Specify the URL of the application server to which an upload callback request is sent. This URL is used to communicate between the application server and OSS. After you upload an object, OSS uses the URL to send information about object upload to the application server. In this example, set the URL to var callbackUrl string="http://192.0.2.0:1234";.
    • dir: Specify the prefix for the name of the object. You can also leave this parameter empty.

Step 2: Configure the client

  1. Download the client source code to the local directory on the PC.
  2. Decompress the package. Open the upload.js file. Find the following code:
    // serverUrl specifies the URL of the application server that returns signature information and upload policies. Replace the sample IP address and port number with actual values in your business scenario. 
    serverUrl ='http://192.0.2.0:8888'
  3. Set severUrl to the URL of the application server. In this example, specify serverUrl ='http://192.0.2.0:1234'.

Step 3: Modify CORS configurations

When you use form upload to upload data from the client to OSS, the client includes the Origin header in the request and sends the request to OSS by using the browser. OSS verifies the request message that includes the Origin header for cross-origin resource sharing (CORS) verification. To use the POST method, configure CORS rules for a bucket.

  1. Log on to the OSS console.
  2. In the left-side navigation pane, click Buckets. On the Buckets page, click the name of the desired bucket.
  3. In the left-side navigation tree, choose Content Security > Cross-Origin Resource Sharing (CORS).
  4. Click Create Rule and configure the parameters as shown in the following figure.
    Note To ensure data security, we recommend that you specify the actual domain name from which you want OSS to allow requests in Sources. For more information about CORS configurations, see Configure CORS.

Step 4: Send an upload callback request

  1. Start the application server.
    In the /home/aliyun/aliyun-oss-appserver-go directory, run the go run appserver.go 192.0.2.0 1234 command.
    Note Replace the IP address and port number with the IP address and port number of the application server that you configured.
  2. Start the client.
    1. On the PC, open the index.html file in the directory that contains 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.
    2. Click Select File. Select the file of a specified type. Click Upload. After the object is uploaded, the content returned by the callback server is displayed.

Core code analysis of the application server

The source code of the application server is used to implement the signature-based upload and upload callback.

  • Signature-based upload

    During signature-based upload, the application server responds to the GET message that is sent from the client. An example of the snippet:

    func handlerRequest(w http.ResponseWriter, r *http.Request) {   
            if (r.Method == "GET") {
                    response := get_policy_token()
                    w.Header().Set("Access-Control-Allow-Methods", "POST")
                    w.Header().Set("Access-Control-Allow-Origin", "*")
                    io.WriteString(w, response)
            }
  • Upload callback

    During upload callback, the application server responds to the POST message that is sent from OSS. An example of the snippet:

    if (r.Method == "POST") {
                    fmt.Println("\nHandle Post Request ... ")
    
                    // Get PublicKey bytes
                    bytePublicKey, err := getPublicKey(r)
                    if (err != nil) {
                            responseFailed(w)
                            return
                    }
    
                    // Get Authorization bytes : decode from Base64String
                    byteAuthorization, err := getAuthorization(r)
                    if (err != nil) {
                            responseFailed(w)
                            return
                    }
    
                    // Get MD5 bytes from Newly Constructed Authorization String. 
                    byteMD5, err := getMD5FromNewAuthString(r)
                    if (err != nil) {
                            responseFailed(w)
                            return
                    }
    
                    // verifySignature and response to client 
                    if (verifySignature(bytePublicKey, byteMD5, byteAuthorization)) {
                            // do something you want according to callback_body ...
    
                            responseSuccess(w)  // response OK : 200  
                    } else {
                            responseFailed(w)   // response FAILED : 400 
                    }
            }

    For more information, see Callback.