Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify code and in favor of constructor based injection #207

Merged
merged 1 commit into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,46 +30,31 @@
*/
@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;
}

/**
* Using AzureStorageBlobProtocolResolver to get Azure Storage Blob resources with file pattern.
*
* @return fileNames in the container match pattern: *.txt
*/
@GetMapping("/getFileNamesWithProtocolResolver")
public String getFileNamesWithProtocolResolver() throws IOException {
@GetMapping
public List<String> 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}")
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
}
}