簡體   English   中英

是否可以使用 resttemplate 在 azure blob 存儲中上傳文件 - Java

[英]Is it possible to use resttemplate to upload file in azure blob storage - Java

有沒有辦法使用 Java - Spring 框架中的 rest 模板將文件上傳到 Azure blob 存儲? 我看到所有使用 SDK 的示例都成功了,但我們被告知不要使用 SDK - Java。我知道問題是 10,000 英尺高,但任何指針/方向都會有很大幫助。

不知道為什么不使用 SDK,也不知道誰會告訴你。

無論如何,是的,您可以使用 RestTemplate 或更好的webClient

您所要做的就是將 map 請求與 SDK 一起發送到雲端。

您必須手動添加 header 身份驗證。 序列化我想的文件。

當您為客戶端提供受支持的 SDK 時,工作量很大。

例如,這是一個用 WebClient 發送給 Redmine 服務器的簡單請求

String url = "http://localhost:3001/projects.json"; //Redmine local server
RestTemplate restTemplate = new RestTemplate();
JSONObject object = new JSONObject();   //Json object that will need to be sent to redmine
object.put("name", "dummyName");        // Should look like this
object.put("identifier", "dummyId");    // {"project":{"identifier":"dummyId","name":"dummyName"}}
JSONObject body = new JSONObject();     
body.put("project", object);            
String plainCreds = "user:bitnami1"; // default basic auth encoding
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds); 
headers.add("Content-Type", "application/json");
RequestEntity<JSONObject> requestEntity = RequestEntity
                .post(new URI(url))
                .accept(MediaType.APPLICATION_JSON)
                .headers(headers)
                .body(body);
ResponseEntity<String> r = restTemplate.exchange(requestEntity, String.class);

這就是使用 RestTemplate 的相同示例的樣子

String url = "http://localhost:3001/projects.json"; //Redmine local server
RestTemplate restTemplate = new RestTemplate();
JSONObject object = new JSONObject();   //Json object that will need to be sent to redmine
object.put("name", "dummyName");        // Should look like this
object.put("identifier", "dummyId");    // {"project":{"identifier":"dummyId","name":"dummyName"}}
JSONObject body = new JSONObject();     
body.put("project", object);            
String plainCreds = "user:bitnami1"; // default basic auth encoding
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds); 
headers.add("Content-Type", "application/json");
HttpEntity<Object> entity = new HttpEntity<Object>(body, headers);
ResponseEntity<String> result = restTemplate.exchange("http://localhost:3001/projects.json",
                HttpMethod.POST,
                entity,
                String.class);

這兩個,都是可能的,因為我試圖匹配這個 curl 請求

curl --location --request POST 'localhost:3001/projects.json' \
--header 'Authorization: Basic dXNlcjpiaXRuYW1pMQ==' \
--header 'Content-Type: application/json' \
--data-raw '{"project":{"identifier":"dummyId","name":"dummyName"}}'

因此,一旦您知道請求是什么樣子,您就可以使用 RestTemplate 或 WebClient,或任何其他類似的 class 來構建您的請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM