Upload files using OSS

更新时间:
复制 MD 格式

When you call API operations to apply for qualifications, signatures, or templates, you must upload supporting documents, such as qualification certificates or Letters of Authorization. This topic describes how to upload the required materials to Object Storage Service (OSS). After the upload is complete, you can submit the file path parameter in the API operation. Providing complete documentation helps reviewers understand your business details and improves review efficiency. If you do not submit the required materials, your review may be delayed.

image

Step 1: Get OSS configuration information

  • When you apply for qualifications: To upload qualification certificates, call the GetQualificationOssInfo API operation to obtain OSS configuration information. Set the input parameter to dysms.

  • When you upload a Letter of Authorization: Call the GetQualificationOssInfo API operation to obtain OSS configuration information. Set the input parameter to dysms.

  • When you upload a trademark: Call the GetQualificationOssInfo API operation to obtain OSS configuration information. Set the input parameter to dysms.

  • When you upload an ICP filing: Call the GetQualificationOssInfo API operation to obtain OSS configuration information. Set the input parameter to dysms.

  • When you apply for a signature or template: To upload supporting documents, call the GetOSSInfoForUploadFile API operation to obtain OSS configuration information. Set the input parameter to fcMediaSms.

  • When you apply for a template: If the traffic source is a domain name link (trfficDrivingType=DOMAIN), you must upload the traffic source information. Call the Get OSS information for OCR image recognition API operation to obtain OSS configuration information. Set the input parameter to ICP_DOMAIN.

Sample response:

{
  "RequestId": "A90E4451-FED7-49D2-87C8-00700EDCFD0D",
  "Message": "OK",
  "Model": {
    "Policy": "eyJleHBpcmF0aW9uIjoiMjAyN*************************************9udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0***sIiRrZXkiLCIiXV19",
    "StartPath": "123456",
    "AccessKeyId": "LTAI****************",
    "Signature": "BXnwCWPrhVb*****aoZHZfli5KE=",
    "Host": "https://alicom-fc-media.oss-cn-zhangjiakou.aliyuncs.com",
    "ExpireTime": "1719297445"// A timestamp. It represents the expiration time of the policy. Upload requests sent after this time will fail.
  },
  "Code": "OK",
  "Success": true
}

Step 2: Upload the file

After you obtain the OSS configuration, upload the file to the specified OSS location.

  • If you have multiple documents, you can combine them into a single file. The supported formats are PNG, JPG, JPEG, DOC, DOCX, and PDF.

  • The file name cannot contain Chinese characters or special symbols.

For file and document specifications, see Qualification materials | Letter of Authorization specifications | Signature application materials | Template specifications | User authorization materials.

This topic provides a Java sample for server-side uploads. For more information, see PostObject.

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class HttpOssClient {

    // Corresponds to the Signature in the return value
    private String signature = "BXnwCW**********aoZHZfli5KE=";
    // Corresponds to the Policy in the return value
    private String policy = "eyJleHBpcmF0aW9uIjoiMjAyN******************************************9udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0***sIiRrZXkiLCIiXV19";
    // The path of the local file you want to upload
    private String localFilePath = "<PATH_TO_YOUR_FILE>";
    // Corresponds to the AccessKeyId in the return value
    private String accessKeyId = "LTAI****************";
    // Corresponds to the StartPath in the return value. Note that it must end with a forward slash (/).
    private String startPath = "123456" + "/";
    // Corresponds to the Host in the return value
    private String url = "https://alicom-fc-media.oss-cn-zhangjiakou.aliyuncs.com";


    public static void main(String[] args) throws Exception {
        new HttpOssClient().upload();
    }

    private String getFileName() {
        String fileName = localFilePath;
        if(localFilePath.contains(File.separator)){
            fileName = FilenameUtils.getName(localFilePath);
        }
        String extension = FilenameUtils.getExtension(fileName);
        String prefFileName = StringUtils.substringBefore(fileName,".");
        // Encode the file name
        String encodeFileName = new String(prefFileName.getBytes(),StandardCharsets.UTF_8);
        // Add a timestamp to prevent overwriting files with the same name
        fileName = encodeFileName + System.currentTimeMillis() + FilenameUtils.EXTENSION_SEPARATOR + extension ;

        return fileName;
    }

    private void upload() throws Exception {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse httpResponse = null;
        HttpPost httpPost = new HttpPost(url);

        ContentType contentType = ContentType.create("text/plain",StandardCharsets.UTF_8);

        String fileName = getFileName();
        String fileKey = this.startPath + fileName;
        File file = new File(localFilePath);

        HttpEntity requestHttpEntity = MultipartEntityBuilder.create()
                .addTextBody("OSSAccessKeyId", this.accessKeyId)
                .addTextBody("Signature", this.signature)
                .addTextBody("policy", this.policy)
                .setContentType(ContentType.MULTIPART_FORM_DATA)
                .addTextBody("key", fileKey,contentType)
                .addBinaryBody("file", file)
                .setCharset(StandardCharsets.UTF_8)
                .build();

        httpPost.setEntity(requestHttpEntity);

        httpPost.setHeader("User-Agent",
                "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
        httpPost.setHeader("Accept", "*/*");


        httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("http status  " + statusCode);

        if (null != entity) {
            String str = EntityUtils.toString(entity);
            System.out.println("http res:  " + str);
        }
        EntityUtils.consume(entity);
        httpClient.close();
        if (httpResponse != null) {
            httpResponse.close();
        }
        if (statusCode >= 200 && statusCode < 300) {
            System.out.println("fileKey : " + fileKey);
        } else {
            throw new IOException("Server returned HTTP response code: " + statusCode + " for URL: " + this.url.toString());
        }
    }

}
Note

Add a timestamp to the file name to prevent files from being overwritten. For more information, see getFileName in the sample code.

Related Maven dependencies

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.0</version>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.5.14</version>
</dependency>

Step 3: Submit the file path parameter

After the file is uploaded, submit the fileKey value that is returned in the sample code.

  • When you request qualifications: You must upload the required qualification files. Then, set the fileKey for each file as the value for the BusinessLicensePics[LicensePic], LegalPersonIdCardFrontSide, LegalPersonIdCardBackSide, AdminIDCardPic, AdminIDCardFrontFace, and OtherFiles[LicensePic] parameters in the SubmitSmsQualification API operation.

  • When you upload a Letter of Authorization: Set the AuthorizationLetterPic parameter to the fileKey value in the CreateSmsAuthorizationLetter API operation.

  • When you upload a trademark: Set the TrademarkPic parameter to the fileKey value in the Create Trademark Entity API operation.

  • When you upload an ICP filing: Set the AppIcpRecordPic parameter to the fileKey value in the Create ICP Filing Entity API operation.

  • When you apply for a signature or template: To upload supporting documents, set the MoreData parameter to the fileKey value in the CreateSmsSign and CreateSmsTemplate API operations.

  • When you apply for a template: To upload traffic source information (ICP filing screenshot), set the TrafficDriving[icpPicOssKey] parameter to the fileName value in the CreateSmsTemplate and UpdateSmsTemplate API operations.

    Note

    The sample code returns a fileKey, but you must pass the fileName when you apply for a template.

    The fileName is the part of the fileKey that follows the last forward slash (/).
    For example:

    • fileKey = "123456/test1719383196031.jpg"

    • The corresponding fileName = "test1719383196031.jpg"

    Ensure that you correctly extract and pass the fileName when you call the API operation.

FAQ

If the following error occurs when you upload a local file to OSS, the request policy has expired. To resolve this issue, repeat Step 1 to obtain new configuration information and update your code with the new return values.

http status 403
<Error>
 <Code>AccessDenied</Code>
 <Message>Invalid according to Policy: Policy expired.</Message>
 <!--Other information is omitted-->
</Error>