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

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

Convert DataLakeFileClient object to java Resource object

问题

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

import org.springframework.core.io.Resource

public Resource downloadFile() {
     Resource resource;
     DataLakeFileClient fileClient = fileSystemClient.getFileClient("test.txt"); // fileSystemClient是容器位置

     // 获取文件客户端对象中的内容并将其分配给resource的代码;

     return resource;
}
英文:

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.

        import org.springframework.core.io.Resource

        public Resource downloadFile() {
             Resource resource;
             DataLakeFileClient fileClient = fileSystemClient.getFileClient("test.txt"); // fileSystemClient is the container location
        
             // code to fetch content present in file client object and assign it to resource;
        
             return resource;
        }

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

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

@RestController
public class DownloadFile {

    @GetMapping("/download-file")
    public ResponseEntity<Resource> downloadFile() {
        String sasToken = "<storage_account_SAS_Token>";
        String accountName = "<Account_name>";
        String containerName = "<Container_Name>";
        String fileName = "<Blob_Nmae>";

        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                .endpoint("https://" + accountName + ".dfs.core.windows.net")
                .sasToken(sasToken)
                .buildClient();

        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.getBlobClient(fileName);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        blobClient.download(outputStream);

        final byte[] bytes = outputStream.toByteArray();
        Resource resource = new ByteArrayResource(bytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");

        System.out.println("Blob downloaded successfully: " + fileName);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .headers(headers)
                .body(resource);
    }
}
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-starter-storage</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-file-datalake</artifactId>
        <version>12.16.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</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:

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

@RestController
public class DownloadFile {

    @GetMapping(&quot;/download-file&quot;)
    public ResponseEntity&lt;Resource&gt; downloadFile() {
        String sasToken = &quot;&lt;storage_account_SAS_Token&gt;&quot;; 
        String accountName = &quot;&lt;Account_name&gt;&quot;;
        String containerName = &quot;&lt;Container_Name&gt;&quot;; 
        String fileName = &quot;&lt;Blob_Nmae&gt;&quot;;

        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                .endpoint(&quot;https://&quot; + accountName + &quot;.dfs.core.windows.net&quot;)
                .sasToken(sasToken)
                .buildClient();

        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        BlobClient blobClient = containerClient.getBlobClient(fileName);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        blobClient.download(outputStream);

        final byte[] bytes = outputStream.toByteArray();
        Resource resource = new ByteArrayResource(bytes);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;&quot; + fileName + &quot;\&quot;&quot;);

        System.out.println(&quot;Blob downloaded successfully: &quot; + fileName);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .headers(headers)
                .body(resource);
    }
}

pom.xml:

&lt;dependencies&gt;
       &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
	    &lt;groupId&gt;com.azure.spring&lt;/groupId&gt;
	    &lt;artifactId&gt;spring-cloud-azure-starter-storage&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
            &lt;groupId&gt;com.azure&lt;/groupId&gt;
            &lt;artifactId&gt;azure-storage-file-datalake&lt;/artifactId&gt;
            &lt;version&gt;12.16.0&lt;/version&gt;
        &lt;/dependency&gt;
	&lt;dependency&gt;
	    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	    &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
	    &lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;
&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:

确定