From 74bfb3ccf2dbdc43c3d8dc81d35fa2fd580bf88c Mon Sep 17 00:00:00 2001 From: zhihaoguo Date: Fri, 21 Jan 2022 16:48:12 +0800 Subject: [PATCH 1/2] update storageblob samples --- .../storage-blob-sample/README.md | 38 ++++++++++--- .../storage/resource/BlobController.java | 55 +++++++++++++++++-- .../storage/resource/StorageApplication.java | 37 ++++++++++++- 3 files changed, 116 insertions(+), 14 deletions(-) diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md index 0559e2183..cef76306c 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/README.md @@ -110,16 +110,36 @@ mvn clean spring-boot:run ``` ## Verify This Sample -Send a POST request to update file contents: -```shell -curl http://localhost:8080/blob -d "new message" -H "Content-Type: text/plain" -``` - -Verify by sending a GET request +1. Write and read a file. + 1.1 Send a POST request to update file contents. + ```shell + curl http://localhost:8080/blob -d "new message" -H "Content-Type: text/plain" + ``` + 1.2 Verify by sending a GET request. + ```shell + curl -XGET http://localhost:8080/blob + ``` + +2. [Optional] Using resourceLoader to get Azure Storage Blob resource with filename. + ```shell + curl -XGET http://localhost:8080/blob/getResourceWithResourceLoader/fileName1.txt + ``` + + Verify in app's log that a similar messages was posted: + ```shell + Blob content retrieved: fileName=fileName1.txt, fileContent=data1 + ``` + +3. [Optional] Using AzureStorageBlobProtocolResolver to get Azure Storage Blob resources with file pattern. + ```shell + curl -XGET http://localhost:8080/blob/getFileNamesWithProtocolResolver + ``` + + Verify in app's log that a similar messages was posted: + ```shell + 10 resources founded with pattern:*.txt + ``` -```shell -curl -XGET http://localhost:8080/blob -``` ## Clean Up Resources After running the sample, if you don't want to run the sample, remember to destroy the Azure resources you created to avoid unnecessary billing. diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/BlobController.java b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/BlobController.java index 58c82f47d..8dbe4f1df 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/BlobController.java +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/BlobController.java @@ -3,10 +3,14 @@ package com.azure.spring.sample.storage.resource; +import com.azure.spring.core.resource.AzureStorageBlobProtocolResolver; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.WritableResource; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.*; @@ -24,13 +28,56 @@ public class BlobController { final static Logger logger = LoggerFactory.getLogger(BlobController.class); - public BlobController() { - logger.info("spring.cloud.azure.storage.blob.endpoint configured"); - } - @Value("${resource.blob}") private Resource azureBlobResource; + @Value("${storage-container-name}") + private String containerName; + + @Autowired + @Qualifier("webApplicationContext") + private ResourceLoader resourceLoader; + + @Autowired + private AzureStorageBlobProtocolResolver azureStorageBlobProtocolResolver; + + /** + * Using resourceLoader to get Azure Storage Blob resource with filename. + * + * @param fileName the fileName(contains extension name) stored in Storage Blob Container. + * eg: fileName = fileName1.txt + * @return the content stored in the file. + * @throws IOException + */ + @GetMapping("/getResourceWithResourceLoader/{fileName}") + public String getResourceWithResourceLoader(@PathVariable String fileName) throws IOException { + // get a BlobResource + Resource storageBlobResource = resourceLoader.getResource("azure-blob://" + containerName + "/" + fileName); + String fileContent = StreamUtils.copyToString( + storageBlobResource.getInputStream(), + Charset.defaultCharset()); + logger.info("Blob content retrieved: fileName={}, fileContent={}", fileName, fileContent); + return "fileContent=" + fileContent; + } + + /** + * Using AzureStorageBlobProtocolResolver to get Azure Storage Blob resources with file pattern. + * + * @return fileNames in the container match pattern: *.txt + * @throws IOException + */ + @GetMapping("/getFileNamesWithProtocolResolver") + public String getFileNamesWithProtocolResolver() throws IOException { + Resource[] resources = azureStorageBlobProtocolResolver.getResources("azure-blob://" + containerName + "/*.txt"); + logger.info("{} resources founded with pattern:*.txt",resources.length); + StringBuffer sb = new StringBuffer(); + for (Resource resource : resources) { + sb.append(resource.getFilename()) + .append("\n"); + } + return sb.toString(); + } + @GetMapping public String readBlobResource() throws IOException { return StreamUtils.copyToString( diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java index 3f0421bf8..288b46525 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java @@ -3,16 +3,51 @@ package com.azure.spring.sample.storage.resource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.core.io.WritableResource; + +import java.io.OutputStream; /** * @author Warren Zhu */ @SpringBootApplication -public class StorageApplication { +public class StorageApplication implements CommandLineRunner { + final static Logger logger = LoggerFactory.getLogger(StorageApplication.class); + + @Autowired + @Qualifier("webApplicationContext") + private ResourceLoader resourceLoader; + + @Value("${storage-container-name}") + private String containerName; public static void main(String[] args) { SpringApplication.run(StorageApplication.class, args); } + + @Override + public void run(String... args) throws Exception { + + logger.info("StorageApplication data initialization begin ..."); + for (int i = 0; i < 10; i++) { + String fileName = "fileName" + i; + String data = "data" + i; + Resource storageBlobResource = resourceLoader.getResource("azure-blob://" +containerName+"/" + fileName + ".txt"); + try (OutputStream os = ((WritableResource) storageBlobResource).getOutputStream()) { + os.write(data.getBytes()); + logger.info("write data to container={}, fileName={}", containerName, fileName); + } + } + logger.info("StorageApplication data initialization end ..."); + } } From 37637bf958753e8fac22f00b3bad2e39965afb32 Mon Sep 17 00:00:00 2001 From: zhihaoguo Date: Tue, 25 Jan 2022 10:58:13 +0800 Subject: [PATCH 2/2] add more java doc --- .../sample/storage/resource/StorageApplication.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java index 288b46525..b78a70fe4 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/StorageApplication.java @@ -35,6 +35,14 @@ public static void main(String[] args) { SpringApplication.run(StorageApplication.class, args); } + /** + * This is used to initialize some data in Azure Storage Blob. + * So users can use `curl -XGET http://localhost:8080/blob/getFileNamesWithProtocolResolver` to test + * AzureStorageBlobProtocolResolver without initializing data. + * + * @param args + * @throws Exception + */ @Override public void run(String... args) throws Exception {