背景
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)); } }
测试结果 :