将DataLakeFileClient对象转换为java Resource对象

8wigbo56  于 2023-08-01  发布在  Java
关注(0)|答案(1)|浏览(94)

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

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

字符串
我尝试了许多使用OutputStream和InputStream对象的实现,但无法找到确切的代码。有人能帮我提供同样的确切代码吗?

mwg9r5ms

mwg9r5ms1#

我尝试了下面的Sping Boot 代码从Azure blob存储下载文件,并将其转换为Azure Portal的DataLakeGen2存储中的Resource对象。
我在我的存储帐户容器中保存了下面的blob。


的数据

验证码:

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);
    }
}

字符串

pom.xml:

<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>

输出:

运行成功,给出print语句为Blob下载成功如下,



通过上面的输出端口8080,我在浏览器上下载了blob,如下所示:



而且,我在Postman中检查了这个,并获得了该blob的数据,如下所示,


相关问题