您在創建簽名或模板時,可通過本文檔所示方法上傳帶有鏈接的登錄頁面、后臺頁面截圖、軟著、協議補充等資料。有助于審核人員了解您的業務詳情,提高審核效率。若多個資料可拼接成一個文件,支持png、jpg、jpeg、doc、docx、pdf格式。
步驟一:獲取OSS授權信息
通過GetOSSInfoForUploadFile接口獲取OSS授權信息。
返回示例:
{
"RequestId": "A90E4451-FED7-49D2-87C8-00700EDCFD0D",
"Message": "OK",
"Model": {
"Policy": "eyJleHBpcmF0aW9uIjoiMjAyN***Ni0yNVQwNjozNzoyNS45NzBaI**iY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0***sIiRrZXkiLCIiXV19",
"StartPath": "123456",
"AccessKeyId": "LTAIxetqt1Dg****",
"Signature": "BXnwCWPrhVb*****aoZHZfli5KE=",
"Host": "https://alicom-fc-media.oss-cn-zhangjiakou.aliyuncs.com",
"ExpireTime": "1719297445"
},
"Code": "OK",
"Success": true
}
步驟二:上傳文件
說明
調用接口前需配置環境變量,通過環境變量讀取訪問憑證。AccessKey ID和AccessKey Secret的環境變量名:SMS_ACCESS_KEY_ENV 、SMS_ACCESS_KEY_SECRET_ENV。配置詳情請參見配置訪問憑證。
獲取授權信息后,可通過授權信息上傳文件到OSS,本文給出服務端上傳文件示例。更多詳情請參見OSS官網文檔在客戶端直接上傳文件到OSS、上傳文件到OSS。
Java后端上傳文件示例
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 {
//對應返回值中的Signature
private String signature = "BXnwCWPrhVb*****aoZHZfli5KE=";
//對應返回值中的Policy
private String policy = "eyJleHBpcmF0aW9uIjoiMjAyN***Ni0yNVQwNjozNzoyNS45NzBaI**iY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0***sIiRrZXkiLCIiXV19";
//本地文件路徑
private String localFilePath = "/Users/Downloads/test.jpg";
//對應返回值中的AccessKeyId
private String accessKeyId = "LTAIxetqt1Dg****";
//對應返回值中的StartPath,注意以 / 結尾
private String startPath = "123456/";
//對應返回值中的Host
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,".");
//文件名進行編碼
String encodeFileName = new String(prefFileName.getBytes(),StandardCharsets.UTF_8);
//加一個時間戳,防止文件重名覆蓋
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("oss key : " + fileKey);
} else {
throw new IOException("Server returned HTTP response code: " + statusCode + " for URL: " + this.url.toString());
}
}
}
相關依賴:
<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>
步驟三:設置參數
文件上傳成功后,使用示例代碼中的fileKey,設置CreateSmsSign、CreateSmsTemplate接口中的MoreData參數值。
MoreData參數填寫示例:["123456/test1719383196031.jpg"]
文檔內容是否對您有幫助?