将DataLakeFileClient对象转换为Java Resource对象。

huangapple go评论124阅读模式
英文:

Convert DataLakeFileClient object to java Resource object

问题

我正在尝试从Azure Blob存储中下载文件并尝试将其转换为Resource对象。代码应该类似于以下内容。

  1. import org.springframework.core.io.Resource
  2. public Resource downloadFile() {
  3. Resource resource;
  4. DataLakeFileClient fileClient = fileSystemClient.getFileClient("test.txt"); // fileSystemClient是容器位置
  5. // 获取文件客户端对象中的内容并将其分配给resource的代码;
  6. return resource;
  7. }
英文:

I'm trying to download a file from Azure blob storage and trying to convert it into a Resource object. The code should look something like this.

  1. import org.springframework.core.io.Resource
  2. public Resource downloadFile() {
  3. Resource resource;
  4. DataLakeFileClient fileClient = fileSystemClient.getFileClient("test.txt"); // fileSystemClient is the container location
  5. // code to fetch content present in file client object and assign it to resource;
  6. return resource;
  7. }

I have tried many implementations using OutputStream and InputStream objects, but unable to find the exact code. Can anyone help me by providing the exact code for the same?

答案1

得分: 0

  1. import com.azure.storage.blob.BlobClient;
  2. import com.azure.storage.blob.BlobContainerClient;
  3. import com.azure.storage.blob.BlobServiceClient;
  4. import com.azure.storage.blob.BlobServiceClientBuilder;
  5. import org.springframework.core.io.ByteArrayResource;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.ByteArrayOutputStream;
  14. @RestController
  15. public class DownloadFile {
  16. @GetMapping("/download-file")
  17. public ResponseEntity<Resource> downloadFile() {
  18. String sasToken = "<storage_account_SAS_Token>";
  19. String accountName = "<Account_name>";
  20. String containerName = "<Container_Name>";
  21. String fileName = "<Blob_Nmae>";
  22. BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
  23. .endpoint("https://" + accountName + ".dfs.core.windows.net")
  24. .sasToken(sasToken)
  25. .buildClient();
  26. BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
  27. BlobClient blobClient = containerClient.getBlobClient(fileName);
  28. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  29. blobClient.download(outputStream);
  30. final byte[] bytes = outputStream.toByteArray();
  31. Resource resource = new ByteArrayResource(bytes);
  32. HttpHeaders headers = new HttpHeaders();
  33. headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
  34. System.out.println("Blob downloaded successfully: " + fileName);
  35. return ResponseEntity.ok()
  36. .contentType(MediaType.APPLICATION_OCTET_STREAM)
  37. .headers(headers)
  38. .body(resource);
  39. }
  40. }
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.azure.spring</groupId>
  8. <artifactId>spring-cloud-azure-starter-storage</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>com.azure</groupId>
  12. <artifactId>azure-storage-file-datalake</artifactId>
  13. <version>12.16.0</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>

This code is used to download a file from Azure blob storage and convert it into a Resource object from DataLakeGen2 Storage on Azure Portal.

英文:

I tried the below Spring boot code to download a file from Azure blob storage and converted it into a Resource object from DataLakeGen2 Storage from Azure Portal.

I have the below blob saved in my storage account container.

将DataLakeFileClient对象转换为Java Resource对象。

Code:

  1. import com.azure.storage.blob.BlobClient;
  2. import com.azure.storage.blob.BlobContainerClient;
  3. import com.azure.storage.blob.BlobServiceClient;
  4. import com.azure.storage.blob.BlobServiceClientBuilder;
  5. import org.springframework.core.io.ByteArrayResource;
  6. import org.springframework.core.io.Resource;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.ResponseEntity;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import java.io.ByteArrayInputStream;
  13. import java.io.ByteArrayOutputStream;
  14. @RestController
  15. public class DownloadFile {
  16. @GetMapping(&quot;/download-file&quot;)
  17. public ResponseEntity&lt;Resource&gt; downloadFile() {
  18. String sasToken = &quot;&lt;storage_account_SAS_Token&gt;&quot;;
  19. String accountName = &quot;&lt;Account_name&gt;&quot;;
  20. String containerName = &quot;&lt;Container_Name&gt;&quot;;
  21. String fileName = &quot;&lt;Blob_Nmae&gt;&quot;;
  22. BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
  23. .endpoint(&quot;https://&quot; + accountName + &quot;.dfs.core.windows.net&quot;)
  24. .sasToken(sasToken)
  25. .buildClient();
  26. BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
  27. BlobClient blobClient = containerClient.getBlobClient(fileName);
  28. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  29. blobClient.download(outputStream);
  30. final byte[] bytes = outputStream.toByteArray();
  31. Resource resource = new ByteArrayResource(bytes);
  32. HttpHeaders headers = new HttpHeaders();
  33. headers.add(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;&quot; + fileName + &quot;\&quot;&quot;);
  34. System.out.println(&quot;Blob downloaded successfully: &quot; + fileName);
  35. return ResponseEntity.ok()
  36. .contentType(MediaType.APPLICATION_OCTET_STREAM)
  37. .headers(headers)
  38. .body(resource);
  39. }
  40. }

pom.xml:

  1. &lt;dependencies&gt;
  2. &lt;dependency&gt;
  3. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  4. &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  5. &lt;/dependency&gt;
  6. &lt;dependency&gt;
  7. &lt;groupId&gt;com.azure.spring&lt;/groupId&gt;
  8. &lt;artifactId&gt;spring-cloud-azure-starter-storage&lt;/artifactId&gt;
  9. &lt;/dependency&gt;
  10. &lt;dependency&gt;
  11. &lt;groupId&gt;com.azure&lt;/groupId&gt;
  12. &lt;artifactId&gt;azure-storage-file-datalake&lt;/artifactId&gt;
  13. &lt;version&gt;12.16.0&lt;/version&gt;
  14. &lt;/dependency&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  17. &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
  18. &lt;scope&gt;test&lt;/scope&gt;
  19. &lt;/dependency&gt;
  20. &lt;/dependencies&gt;

Output:

It runs successfully and gives the print statement as Blob downloaded successfully as below,

将DataLakeFileClient对象转换为Java Resource对象。

With the above output port 8080, I got the blob downloaded at the browser as below,

将DataLakeFileClient对象转换为Java Resource对象。

And also, I checked this in Postman and got the data of that blob as below,

将DataLakeFileClient对象转换为Java Resource对象。

huangapple
  • 本文由 发表于 2023年7月31日 19:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定