Upload a document

Updated at:

This topic describes the syntax and provides examples of the Tongyi data mining API for uploading documents.

1. Request an upload lease

Request syntax

POST /zhiwen-file/apply_upload_lease HTTP/1.1

Request parameters

Name

Type

Required

Description

Example

fileName

string

Yes

The name of the document.

Alibaba Cloud Model Studio.pdf

sizeBytes

number

Yes

The size of the document to upload, in bytes.

28123

md5

string

Yes

The MD5 hash of the document.

Response parameters

Name

Type

Description

code

int

The status code.

data

object

The response data.

-param

object

The HTTP request parameters for uploading the document.

-headers

object

The key-value pairs to include in the header for step 2 (uploading the document). Both the key and value are strings.

-x-bailian-extra

string

-Content-Type

string

-method

string

The HTTP method to call.

-url

string

The URL to request for step 2 (uploading the document).

-type

string

The upload method for the document.

-lease_id

string

The unique ID of the lease. This parameter is required for step 3 (submitting the document for parsing).

requestId

string

The request ID.

success

boolean

Success

message

string

The response message.

Example

Sample success response

JSON format

{
  "code": 200,
  "data": {
    "param": {
      "headers": {
        "x-bailian-extra": "PkdiMDOidwEwMTE4KnY2MQ==",
        "Content-Type": "text/plain"
      },
      "method": "PUT",
      "url": "https://dashscope-file-datacenter-prod-01.oss-cn-beijing.aliyuncs.com/1880205101189661/10064170/zhiwen/3631bf9f24ea4ac7b362a135deee7fec.1753176187488.txt?Expires=1753182187&OSSAccessKeyId=YOUR_ACCESS_KEY_ID&Signature=YOUR_SIGNATURE"
    },
    "type": "OSS.PreSignedUrl",
    "lease_id": "3631bf9f24ea4ac7b3dr5135deee7fec.1752176287488"
  },
  "requestId": "e1cdcfab-897d-9b7b-8c38-72aa39a13c87",
  "success": true,
  "message": "Success"
}

2. Upload the document to OSS

Use the data.param.url, data.param.method, x-bailian-extra, and Content-Type parameters from data.param.headers returned in the previous step (requesting an upload lease) to upload your local document to Object Storage Service (OSS). The following code provides an example.

# This sample code is for reference only. Do not use it directly in a production environment.
import requests

def upload_file(pre_signed_url, file_path):
    try:
        # Set the request header.
        headers = {
            "x-bailian-extra": "Replace this with the value of the x-bailian-extra field from data.param.headers returned by the request an upload lease API call in the previous step.",
            "Content-Type": "Replace this with the value of the Content-Type field from data.param.headers returned by the request an upload lease API call in the previous step."
        }

        # Read and upload the document.
        with open(file_path, 'rb') as file:
            # The request method for uploading the document must be the same as the value of the method field in data.param returned by the request an upload lease API call in the previous step.
            response = requests.put(pre_signed_url, data=file, headers=headers)

        # Check the response status code.
        if response.status_code == 200:
            print("File uploaded successfully.")
        else:
            print(f"Failed to upload the file. ResponseCode: {response.status_code}")

    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":

    pre_signed_url_or_http_url = "Replace this with the value of the url field in data.param returned by the request an upload lease API call in the previous step."

    # The document source is local. Upload the local document to OSS.
    file_path = "Replace this with the actual local path of the document you want to upload."
    upload_file(pre_signed_url_or_http_url, file_path)
// This sample code is for reference only. Do not use it directly in a production environment.
import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UploadFile{

    public static void uploadFile(String preSignedUrl, String filePath) {
        HttpURLConnection connection = null;
        try {
            // Create a URL object.
            URL url = new URL(preSignedUrl);
            connection = (HttpURLConnection) url.openConnection();

            // Set the request method for the document upload. It must be the same as the value of the method field in data.param returned by the request an upload lease API call in the previous step.
            connection.setRequestMethod("PUT");

            // Allow output to the connection because this connection is used to upload the document.
            connection.setDoOutput(true);

            connection.setRequestProperty("x-bailian-extra", "Replace this with the value of the x-bailian-extra field from data.param.headers returned by the request an upload lease API call in the previous step.");
            connection.setRequestProperty("Content-Type", "Replace this with the value of the Content-Type field from data.param.headers returned by the request an upload lease API call in the previous step.");

            // Read the document and upload it through the connection.
            try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
                 FileInputStream fileInputStream = new FileInputStream(filePath)) {
                byte[] buffer = new byte[4096];
                int bytesRead;

                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }

                outStream.flush();
            }

            // Check the response.
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // Handle a successful document upload.
                System.out.println("File uploaded successfully.");
            } else {
                // Handle a failed document upload.
                System.out.println("Failed to upload the file. ResponseCode: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    public static void main(String[] args) {

        String preSignedUrlOrHttpUrl = "Replace this with the value of the url field in data.param returned by the request an upload lease API call in the previous step.";

        // The document source is local. Upload the local document to OSS.
        String filePath = "Replace this with the actual local path of the document you want to upload.";
        uploadFile(preSignedUrlOrHttpUrl, filePath);
    }
}

3. Submit the document for parsing

Request syntax

POST /zhiwen-file/submit_parse_file HTTP/1.1

Request parameters

Name

Type

Required

Description

Example

leaseId

string

Yes

The unique ID of the lease. You can obtain this ID by requesting a lease.

3631bf9f24ea4ac7b3dr5135deee7fec.1752176287488

Response parameters

Name

Type

Description

code

int

The status code.

data

object

-fileSize

string

The document size.

-docId

string

docId

-name

string

The document name.

-pageSize

number

The number of pages.

-type

string

The document type.

-url

string

The document URL.

-fileId

string

The file ID. This ID is required for subsequent conversations.

requestId

string

The request ID.

success

boolean

Indicates whether the request was successful.

message

string

The response message.

Example

Sample success response

JSON format

{
  "code": 200,
  "data": {
    "fileSize": "280931",
    "docId": "1397267735092973568",
    "name": "Alibaba Cloud Model Studio",
    "pageSize": 5,
    "type": "pdf",
    "url": "https://dashscope-file-datacenter-prod-01.oss-cn-beijing.aliyuncs.com/1880205101189661/10064170/zhiwen/3631bf9f24ea4ac7b362a135deee7fec.1753176187488.pdf?Expires=1753435388&OSSAccessKeyId=YOUR_ACCESS_KEY_ID&Signature=YOUR_SIGNATURE",
    "fileId": "file_zhiwen_XXX"
  },
  "requestId": "e33ba5e9-fef6-96ae-b8b0-1b4a0c151e2b",
  "success": true,
  "message": "Success"
}

Call examples

import hashlib
import os
import requests
from http import HTTPStatus

'''
API name: Upload a document
API paths: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/apply_upload_lease
        https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/submit_parse_file
Environment requirements: Python >= 3.7
'''

apply_lease_url = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/apply_upload_lease"
submit_file_url = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/submit_parse_file"
request_headers = {
    "Authorization": os.environ.get("DASHSCOPE_API_KEY"),  # If you have not configured the environment variable, replace this with your API-KEY.
    "Content-Type": "application/json"
}

file_path = "./test.txt"
file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path)
with open(file_path, 'rb') as f:
    md5 = hashlib.md5()
    while True:
        data = f.read(4096)  # Read 4 KB of data at a time.
        if not data:
            break
        md5.update(data)
file_md5 = md5.hexdigest()

# 1. Request a lease.
response = requests.post(apply_lease_url,
                         json={
                             "fileName": file_name,
                             "sizeBytes": file_size,
                             "md5": file_md5
                         },
                         headers=request_headers)
lease_data = {}
if response.status_code == HTTPStatus.OK:
    print("1. Lease applied successfully.")
    print(response.json())
    lease_data = response.json()['data']
else:
    print(f'response={response.json()}')
    print(f'code={response.status_code}')

# 2. Upload the document.
upload_headers = {
    "X-bailian-extra": lease_data['param']['headers']['x-bailian-extra'],
    "Content-Type": lease_data['param']['headers']['Content-Type']
}
with open(file_path, 'rb') as file:
    # The request method for uploading the document must be the same as the value of the Method field in Data.Param returned by the ApplyFileUploadLease API call in the previous step.
    response = requests.put(lease_data['param']['url'], data=file, headers=upload_headers)
if response.status_code == HTTPStatus.OK:
    print("2. File uploaded successfully.")
else:
    print(f"Failed to upload the file. ResponseCode: {response.status_code}")

# 3. Submit for parsing.
response = requests.post(submit_file_url,
                         json={
                             'leaseId': lease_data['lease_id']
                         },

                         headers=request_headers)
if response.status_code == HTTPStatus.OK:
    print("3. File submit parse successfully.")
    print(response.json())
else:
    print(f'response={response.json()}')
    print(f'code={response.status_code}')

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * API name: Upload a document
 * API paths: https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/apply_upload_lease
 *         https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/submit_parse_file
 * Environment requirements: Java 8 or later
 */
public class FileUploadDemo {
    // Step 1. API address and configuration
    private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY");    // Enter the API KEY from the Model Studio platform.
    private static final String APPLY_UPLOAD_URL = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/apply_upload_lease";
    private static final String SUBMIT_PARSE_URL = "https://dashscope.aliyuncs.com/api/v2/apps/zhiwen-file/submit_parse_file";
    private static final String CHARSET = "UTF-8";
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static void main(String[] args) {
        String filePath = "your_file_path";
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                System.out.println("Document does not exist: " + filePath);
                return;
            }
            // Step 2. Request an upload lease.
            Map<String, String> leaseInfo = applyUploadLease(file.getName(), file.length());
            String leaseId = leaseInfo.get("lease_id");
            String uploadUrl = leaseInfo.get("url");

            Map<String, String> uploadHeaders = new HashMap<>();
            uploadHeaders.put("x-bailian-extra", leaseInfo.get("x-bailian-extra"));
            uploadHeaders.put("Content-Type", leaseInfo.get("Content-Type"));

            // Step 3. Upload the document to Model Studio OSS.
            boolean uploadSuccess = uploadFileToOSS(uploadUrl, uploadHeaders, file);

            if (uploadSuccess) {
                // Step 4. Submit for parsing.
                String parseResult = submitFileForParsing(leaseId);
                System.out.println("\nParsing result: ");
                // The fileId field in the returned result is used for subsequent document conversations.
                System.out.println(parseResult);
            } else {
                System.out.println("Failed to upload the document.");
            }
        } catch (Exception e) {
            System.err.println("Error occurred:");
            e.printStackTrace();
        }
    }

    /**
     * Requests an upload lease.
     * This method requests an upload lease from the server, including the required URL and related parameters for the upload.
     *
     * @param fileName The name of the document to upload.
     * @param fileSize The size of the document in bytes, used to request appropriate storage space.
     * @return A Map containing the information required for the upload, including the lease ID, upload URL, and extra request header parameters.
     * @throws Exception Throws an exception if the request for an upload lease fails or an error occurs during processing.
     */
    private static Map<String, String> applyUploadLease(String fileName, long fileSize) throws Exception {
        // Create the request body, including the document name, size, and MD5 hash.
        ObjectNode requestBodyMap = objectMapper.createObjectNode();
        requestBodyMap.put("fileName", fileName);
        requestBodyMap.put("sizeBytes", fileSize);
        // You can enter any value for the MD5 hash.
        requestBodyMap.put("md5", "md5");
        String requestBody = objectMapper.writeValueAsString(requestBodyMap);
        // Configure the request timeout period.
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();

        HttpPost httpPost = new HttpPost(APPLY_UPLOAD_URL);
        httpPost.setHeader("Authorization", API_KEY);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(requestBody, CHARSET));

        try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build()) {
            HttpResponse response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                throw new IOException("HTTP request failed with status code: " + statusCode);
            }
            String responseBody = EntityUtils.toString(response.getEntity());
            JsonNode rootNode = objectMapper.readTree(responseBody);
            if (!rootNode.path("success").asBoolean()) {
                throw new RuntimeException("Apply upload lease failed: " + responseBody);
            }

            JsonNode dataNode = rootNode.path("data");
            JsonNode paramNode = dataNode.path("param");
            JsonNode headersNode = paramNode.path("headers");

            Map<String, String> result = new HashMap<>();
            result.put("lease_id", dataNode.path("lease_id").asText());
            result.put("url", paramNode.path("url").asText());
            result.put("x-bailian-extra", headersNode.path("x-bailian-extra").asText());
            result.put("Content-Type", headersNode.path("Content-Type").asText());

            result.forEach((key, value) -> System.out.println(key + ": " + value));
            return result;
        }
    }

    /**
     * Uploads a document to Object Storage Service (OSS).
     *
     * @param uploadUrl The URL for the document upload.
     * @param headers The header of the HTTP request.
     * @param file The document to upload.
     * @return Returns true if the document is uploaded successfully, otherwise returns false.
     * @throws Exception Throws an exception if a document read or network request error occurs.
     */
    private static boolean uploadFileToOSS(String uploadUrl, Map<String, String> headers, File file) throws Exception {
        // Configure the connection timeout period for the request.
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();

        try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build()) {
            HttpPut httpPut = new HttpPut(uploadUrl);

            // Set the request header.
            if (headers != null) {
                headers.forEach(httpPut::setHeader);
            }

            // Use streaming upload.
            FileEntity entity = new FileEntity(file);
            httpPut.setEntity(entity);

            try (CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPut)) {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    System.out.println("Document uploaded successfully.");
                    return true;
                } else {
                    String errorBody = EntityUtils.toString(response.getEntity());
                    System.err.println("Failed to upload the document: " + errorBody);
                    return false;
                }
            }
        }
    }

    /**
     * Submits a document for parsing.
     *
     * @param leaseId The lease ID, used to identify and track the document parsing request.
     * @return The response content of a successful parsing, which usually contains the parsed data.
     * @throws Exception Throws an exception if an error occurs during document parsing.
     */
    private static String submitFileForParsing(String leaseId) throws Exception {
        if (leaseId == null || leaseId.isEmpty()) {
            throw new IllegalArgumentException("leaseId cannot be empty");
        }
        // Create the request body and set the lease ID.
        ObjectNode requestBody = objectMapper.createObjectNode();
        requestBody.put("leaseId", leaseId);

        // Configure the timeout settings for the HTTP request.
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30 * 1000).build();

        HttpPost httpPost = new HttpPost(SUBMIT_PARSE_URL);
        httpPost.setHeader("Authorization", API_KEY);
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(requestBody.toString(), CHARSET));

        try (CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(requestConfig)
                .build();
             CloseableHttpResponse response = httpClient.execute(httpPost)) {
            if (response == null || response.getEntity() == null) {
                throw new IOException("HTTP response is empty");
            }
            String responseBody = EntityUtils.toString(response.getEntity(), CHARSET);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed to parse the document: " + responseBody);
            }

            System.out.println("Document parsed successfully.");
            return responseBody;
        }
    }
}

Error codes

For more information, see Error codes for Tongyi data mining.