All Products
Search
Document Center

Alibaba Cloud Model Studio:Use API to upload files

Last Updated:Nov 27, 2024

You can upload your private files to Model Studio, so that the large language models (LLMs) can answer questions related to your private domain knowledge. This topic describes how to use API to upload files.

Procedure

Follow the steps below to upload a file to Model Studio:

image
  1. Call ApplyFileUploadLease to request an upload lease.

    Upon calling ApplyFileUploadLease, you receive an HTTP link for file uploads. The request parameter Md5 is the MD5 hash value of the file, used to ensure file integrity. You can use the MessageDigest class of Java to generate this value.

    For more information, see ApplyFileUploadLease.

    Note

    The HTTP link returned by ApplyFileUploadLease expires within a few minutes. Upload your file promptly to avoid expiration and upload failure.

    Sample response:

    {
      "RequestId": "778C0B3B-xxxx-5FC1-A947-36EDD13606AB",
      "Success": true,
      "Message": "",
      "Code": "success",
      "Status": "200",
      "Data": {
        "FileUploadLeaseId": "1e6a159107384782be5e45ac4759b247.1719325231035",
        "Type": "HTTP",
        "Param": {
          "Method": "PUT",
          "Url": "https://bailian-datahub-data-origin-prod.oss-cn-hangzhou.aliyuncs.com/1005426495169178/10024405/68abd1dea7b6404d8f7d7b9f7fbd332d.1716698936847.pdf?Expires=1716699536&OSSAccessKeyId=TestID&Signature=HfwPUZo4pR6DatSDym0zFKVh9Wg%3D",
          "Headers": "        \"X-bailian-extra\": \"MTAwNTQyNjQ5NTE2OTE3OA==\",\n        \"Content-Type\": \"application/pdf\""
        }
      }
    }
    1. Param: The temporary HTTP link.

    2. Method: The upload method.

    3. Headers: The required key-value pairs in the request header.

  2. Use the lease to upload your file to the Model Studio file server.

    1. Sample code:

      Note

      This sample codes are for demonstration purposes and should not be used in production environments.

      Click to expand

      import requests
      from urllib.parse import urlparse
      
      def upload_file(pre_signed_url, file_path):
          try:
              # Set request headers
              headers = {
                  "X-bailian-extra": "NTQ0MzUyMDc2MzgzNzcwMw==",
                  "Content-Type": "application/pdf"
              }
      
              # Read and upload the file
              with open(file_path, 'rb') as file:
                  response = requests.put(pre_signed_url, data=file, headers=headers)
      
              # Check 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)}")
      
      def upload_file_link(pre_signed_url, source_url_string):
          try:
              # Set request headers
              headers = {
                  "X-bailian-extra": "NTQ0MzUyMDc2MzgzNzcwMw==",
                  "Content-Type": "application/pdf"
              }
      
              # Get the source file
              source_response = requests.get(source_url_string)
              if source_response.status_code != 200:
                  raise RuntimeError("Failed to get source file.")
      
              # Upload the file
              response = requests.put(pre_signed_url, data=source_response.content, headers=headers)
      
              # Check 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 = "https://bailian-datahub-data-origin-prod.oss-cn-beijing.aliyuncs.com/1005426495169178/10036719/2070f50790a8482b985c36691cc7b093.1725003661081.pdf?Expires=1725004261&OSSAccessKeyId=LTAI5tKzNnKPFwCJSCpx****&Signature=OPgdNJ%2BMU%2FLtRjBzXiUjVYQsphw%3D"
      
          # Upload a network file
          file_path = "https://test-lxg-quanxian.oss-cn-beijing.aliyuncs.com/%E6%B5%8B%E8%AF%95-%E6%96%B0%E9%97%BB.pdf?Expires=1725010144&OSSAccessKeyId=TMP.3KfyS1Pyk8YQ4F9fTYGhVpRXe9QJbRfFrKiP6ujzXWr2zu77Pmb8syzh8nLBZkSUskbdLd9KsNTC6RpeUt8pzScnJ9****&Signature=4jxj7hfJTnHWeM49dcd9sWWkXWs%3D"
          upload_file_link(pre_signed_url_or_http_url, file_path)
      
          # Upload a local file
          # file_path = "/Users/test/Downloads/test.pdf"
          # upload_file(pre_signed_url_or_http_url, file_path)
      // This code is for demonstration purposes only and has not been tested. 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 URL object
                  URL url = new URL(preSignedUrl);
                  connection = (HttpURLConnection) url.openConnection();
      
                  // Set request method to PUT, as pre-signed URLs are used for PUT operations to upload files
                  connection.setRequestMethod("PUT");
      
                  // Allow output to the connection, as this connection is used for file upload
                  connection.setDoOutput(true);
      
                  // Set request headers, using the parameters from Data.Param.Headers returned by the ApplyFileUploadLease API
                  connection.setRequestProperty("X-bailian-extra", "NTQ0MzUyMDc2MzgzNzcwMw==");
                  connection.setRequestProperty("Content-Type", "application/pdf");
      
                  // Read and upload the file 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 response code
                  int responseCode = connection.getResponseCode();
                  if (responseCode == HttpURLConnection.HTTP_OK) {
                      // Handle successful file upload
                      System.out.println("File uploaded successfully.");
                  } else {
                      // Handle failed file 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 uploadFileLink(String preSignedUrl, String sourceUrlString) {
              HttpURLConnection connection = null;
              try {
                  // Create URL object
                  URL url = new URL(preSignedUrl);
                  connection = (HttpURLConnection) url.openConnection();
      
                  // Set request method to PUT, as pre-signed URLs are used for PUT operations to upload files
                  connection.setRequestMethod("PUT");
      
                  // Allow output to the connection, as this connection is used for file upload
                  connection.setDoOutput(true);
      
                  // Set request headers, using the parameters from Data.Param.Headers returned by the ApplyFileUploadLease API
                  connection.setRequestProperty("X-bailian-extra", "NTQ0MzUyMDc2MzgzNzcwMw==");
                  connection.setRequestProperty("Content-Type", "application/pdf");
      
                  URL sourceUrl = new URL(sourceUrlString);
                  HttpURLConnection sourceConnection = (HttpURLConnection) sourceUrl.openConnection();
      
                  // Set request method to GET
                  sourceConnection.setRequestMethod("GET");
                  // Get response code, 200 indicates a successful request
                  int sourceFileResponseCode = sourceConnection.getResponseCode();
      
                  // Read and upload the file through the connection
                  if (sourceFileResponseCode != HttpURLConnection.HTTP_OK){
                      throw new RuntimeException("Failed to get source file.");
                  }
                  try (DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
                       InputStream in = new BufferedInputStream(sourceConnection.getInputStream())) {
                      byte[] buffer = new byte[4096];
                      int bytesRead;
      
                      while ((bytesRead = in.read(buffer)) != -1) {
                          outStream.write(buffer, 0, bytesRead);
                      }
      
                      outStream.flush();
                  }
      
                  // Check response code
                  int responseCode = connection.getResponseCode();
                  if (responseCode == HttpURLConnection.HTTP_OK) {
                      // Handle successful file upload
                      System.out.println("File uploaded successfully.");
                  } else {
                      // Handle failed file 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 = "https://bailian-datahub-data-origin-prod.oss-cn-beijing.aliyuncs.com/1005426495169178/10036719/2070f50790a8482b985c36691cc7b093.1725003661081.pdf?Expires=1725004261&OSSAccessKeyId=LTAI5tKzNnKPFwCJSCpx****&Signature=OPgdNJ%2BMU%2FLtRjBzXiUjVYQsphw%3D";
      
              // The following code is for uploading a network file. Replace filePath with your own network file path.
              String filePath = "https://test-lxg-quanxian.oss-cn-beijing.aliyuncs.com/%E6%B5%8B%E8%AF%95-%E6%96%B0%E9%97%BB.pdf?Expires=1725010144&OSSAccessKeyId=TMP.3KfyS1Pyk8YQ4F9fTYGhVpRXe9QJbRfFrKiP6ujzXWr2zu77Pmb8syzh8nLBZkSUskbdLd9KsNTC6RpeUt8pzScnJ9****&Signature=4jxj7hfJTnHWeM49dcd9sWWkXWs%3D";
              uploadFileLink(preSignedUrlOrHttpUrl,filePath);
      
              // The following code is for uploading a local file
              // String filePath = "/Users/test/Downloads/test.pdf";
              // uploadFile(preSignedUrlOrHttpUrl, filePath);
          }
      }

      Replace the following parameters based on your actual business needs:

      Parameter

      Description

      connection.setRequestProperty

      The HTTP request header. Corresponds to the Headers value returned by ApplyFileUploadLease.

      preSignedUrlOrHttpUrl

      The HTTP link acquired upon requesting a file upload lease.

      Corresponds to the Url value returned by ApplyFileUploadLease.

      filePath

      The local file path.

  3. Call AddFile to add your file to the Model Studio system.

    For more information, see AddFile.

  4. Call DescribeFile to query the status of your file.

    After a file is added to the Model Studio system, the system initiates automatic parsing, which may take some time. To check the progress of file parsing, use one of the following methods:

    • Log on to the Model Studio console and go to Data Center > Data Management.

    • Call DescribeFile to check the parsing status.

      For more information, see DescribeFile.

What to do next

After importing data, you need to create a knowledge index so that the LLMs can use the data, see:

References

Apart from the API, you can also use the console to upload your files. For more information, see Import data.