简体   繁体   English

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

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

Is there a way to upload files to Azure blob storage using rest template in Java - Spring framework?有没有办法使用 Java - Spring 框架中的 rest 模板将文件上传到 Azure blob 存储? I see all examples using SDK and it was successful but we are told not to use SDK - Java. I know question is 10,000 feet high, but any pointers/direction will help a lot.我看到所有使用 SDK 的示例都成功了,但我们被告知不要使用 SDK - Java。我知道问题是 10,000 英尺高,但任何指针/方向都会有很大帮助。

No idea why not to use SDK, Also no idea who might be told you.不知道为什么不使用 SDK,也不知道谁会告诉你。

Anyway, Yes you can use RestTemplate or even better webClient .无论如何,是的,您可以使用 RestTemplate 或更好的webClient

All you have to do is to map the request sent with the SDK to the cloud.您所要做的就是将 map 请求与 SDK 一起发送到云端。

You have to add header authentication manually.您必须手动添加 header 身份验证。 serialize the file I suppose.序列化我想的文件。

And it's a lot of work when you do have a supported SDK for client.当您为客户端提供受支持的 SDK 时,工作量很大。

For example, here is a simple request sent with WebClient for Redmine server例如,这是一个用 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);

And this is who the same example will look like using RestTemplate这就是使用 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);

Both of these, Were only possible because am trying to match this curl request这两个,都是可能的,因为我试图匹配这个 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"}}'

So once You know what the request looks like, You can use RestTemplate or WebClient, or any other similar class to build up your request.因此,一旦您知道请求是什么样子,您就可以使用 RestTemplate 或 WebClient,或任何其他类似的 class 来构建您的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM