背景
1.很多客戶使用雲端api ,做固件上傳調試不通 ,一方面是雲端接口設計不是很合理
比如policy ,signature失效比較短 ,第二是postObject是oss的接口 ,涉及到多個産品的配合
測試過程
1. 調用本接口生成更新封包件上傳到對象存儲(OSS)的資訊。得到這些參數
Key、OSSAccessKeyId、Signature和Policy。
2.調用OSS
PostObject上傳更新封包件
代碼
pom包
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-iot</artifactId>
<version>7.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.10</version>
</dependency>
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.GenerateOTAUploadURLRequest;
import com.aliyuncs.iot.model.v20180120.GenerateOTAUploadURLResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
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.HttpClients;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author: gj
* @Date: 2021/2/10 14:36
* @Version 1.0
*/
@Slf4j
public class Demo {
@Test
public void postObject() throws IOException, ClientException {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "LTAIlUxNzwkVzyli", "y9oXEKt2Nk6umvUSNdarZKkgpFchY4");
IAcsClient client = new DefaultAcsClient(profile);
GenerateOTAUploadURLRequest request = new GenerateOTAUploadURLRequest();
request.setFileSuffix("zip");
GenerateOTAUploadURLResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
String key=response.getData().getKey();
String host=response.getData().getHost();
String policy=response.getData().getPolicy();
String ossAccessKeyId=response.getData().getOSSAccessKeyId();
String signature=response.getData().getSignature();
//上面是擷取參數
InputStream in = new FileInputStream(new File(Demo.class.getClassLoader().getResource("build.zip").getPath()));
byte[] buf = new byte[2048]; //資料中轉站 臨時緩沖區
int length = 0;
//循環讀取檔案内容,輸入流中将最多buf.length個位元組的資料讀入一個buf數組中,傳回類型是讀取到的位元組數。
//當檔案讀取到結尾時傳回 -1,循環結束。
while((length = in.read(buf)) != -1){
// System.out.println(new String(buf, 0, length));
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(host);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("key", key, ContentType.TEXT_PLAIN);
builder.addTextBody("policy", policy, ContentType.TEXT_PLAIN);
builder.addTextBody("OSSAccessKeyId", ossAccessKeyId, ContentType.TEXT_PLAIN);
builder.addTextBody("signature", signature, ContentType.TEXT_PLAIN);
builder.addTextBody("success_action_status", "200", ContentType.TEXT_PLAIN);
builder.addBinaryBody("file", buf);
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response11 = httpClient.execute(uploadFile);
log.info(new Gson().toJson(response11));
}
}