From bc7800a8ee2277597b047d685f3b2e0a65f03c28 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Mon, 28 Feb 2022 16:14:29 +0800 Subject: [PATCH] simplify code --- .../storage-blob-sample/README.md | 13 +---- .../storage/resource/BlobController.java | 56 +++++++------------ .../resource/SampleDataInitializer.java | 22 +++----- 3 files changed, 31 insertions(+), 60 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 56e57dde2..fd0fb5fab 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 @@ -142,19 +142,10 @@ mvn clean spring-boot:run curl -XGET http://localhost:8080/blob/file1.txt ``` -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. +2. [Optional] Using AzureStorageBlobProtocolResolver to get Azure Storage Blob resources with file pattern. ```shell - curl -XGET http://localhost:8080/blob/getFileNamesWithProtocolResolver + curl -XGET http://localhost:8080/blob ``` Verify in app's log that a similar messages was posted: 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 9decce642..cd7333767 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 @@ -6,18 +6,22 @@ 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.Value; -import org.springframework.context.ResourceLoaderAware; 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.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -26,34 +30,19 @@ */ @RestController @RequestMapping("blob") -public class BlobController implements ResourceLoaderAware { +public class BlobController { final static Logger logger = LoggerFactory.getLogger(BlobController.class); + private final String containerName; + private final ResourceLoader resourceLoader; + private final AzureStorageBlobProtocolResolver azureStorageBlobProtocolResolver; - @Value("${spring.cloud.azure.storage.blob.container-name}") - private String containerName; - - 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. - */ - @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; + public BlobController(@Value("${spring.cloud.azure.storage.blob.container-name}") String containerName, + ResourceLoader resourceLoader, + AzureStorageBlobProtocolResolver patternResolver) { + this.containerName = containerName; + this.resourceLoader = resourceLoader; + this.azureStorageBlobProtocolResolver = patternResolver; } /** @@ -61,11 +50,11 @@ public String getResourceWithResourceLoader(@PathVariable String fileName) throw * * @return fileNames in the container match pattern: *.txt */ - @GetMapping("/getFileNamesWithProtocolResolver") - public String getFileNamesWithProtocolResolver() throws IOException { + @GetMapping + public List listTxtFiles() throws IOException { Resource[] resources = azureStorageBlobProtocolResolver.getResources("azure-blob://" + containerName + "/*.txt"); logger.info("{} resources founded with pattern:*.txt",resources.length); - return Stream.of(resources).map(Resource::getFilename).collect(Collectors.joining("\n")); + return Stream.of(resources).map(Resource::getFilename).collect(Collectors.toList()); } @GetMapping("/{fileName}") @@ -77,11 +66,6 @@ public String readBlobResource(@PathVariable("fileName") String fileName) throws Charset.defaultCharset()); } - @Override - public void setResourceLoader(ResourceLoader resourceLoader) { - this.resourceLoader = resourceLoader; - } - @PostMapping("/{fileName}") public String writeBlobResource(@PathVariable("fileName") String fileName, @RequestBody String data) throws IOException { // get a BlobResource diff --git a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/SampleDataInitializer.java b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/SampleDataInitializer.java index 76b29d2a5..77eb6da7b 100644 --- a/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/SampleDataInitializer.java +++ b/storage/spring-cloud-azure-starter-storage-blob/storage-blob-sample/src/main/java/com/azure/spring/sample/storage/resource/SampleDataInitializer.java @@ -2,11 +2,8 @@ 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.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.WritableResource; @@ -15,13 +12,17 @@ import java.io.OutputStream; @Component -public class SampleDataInitializer implements CommandLineRunner, ResourceLoaderAware { +public class SampleDataInitializer implements CommandLineRunner { final static Logger logger = LoggerFactory.getLogger(SampleDataInitializer.class); - private ResourceLoader resourceLoader; + private final ResourceLoader resourceLoader; - @Value("${spring.cloud.azure.storage.blob.container-name}") - private String containerName; + private final String containerName; + public SampleDataInitializer(@Value("${spring.cloud.azure.storage.blob.container-name}") String containerName, + ResourceLoader resourceLoader) { + this.containerName = containerName; + this.resourceLoader = resourceLoader; + } /** * This is used to initialize some data in Azure Storage Blob. @@ -38,14 +39,9 @@ public void run(String... args) throws Exception { 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("write data to container={}, fileName={}.txt", containerName, fileName); } } logger.info("StorageApplication data initialization end ..."); } - - @Override - public void setResourceLoader(ResourceLoader resourceLoader) { - this.resourceLoader = resourceLoader; - } }