英文:
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.
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("/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>
Output:
It runs successfully and gives the print statement as Blob downloaded successfully as below,
With the above output port 8080, I got the blob downloaded at the browser as below,
And also, I checked this in Postman and got the data of that blob as below,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论