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

simple improvement #205

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 @@ -22,21 +22,9 @@
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,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.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
Expand All @@ -18,24 +18,24 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Warren Zhu
*/
@RestController
@RequestMapping("blob")
public class BlobController {
public class BlobController implements ResourceLoaderAware {

final static Logger logger = LoggerFactory.getLogger(BlobController.class);

@Value("${resource.blob}")
private Resource azureBlobResource;

@Value("${storage-container-name}")
@Value("${spring.cloud.azure.storage.blob.container-name}")
private String containerName;

@Autowired
@Qualifier("webApplicationContext")
private ResourceLoader resourceLoader;

@Autowired
Expand All @@ -47,7 +47,6 @@ public class BlobController {
* @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 {
Expand All @@ -64,18 +63,12 @@ public String getResourceWithResourceLoader(@PathVariable String fileName) throw
* 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();
return Stream.of(resources).map(Resource::getFilename).collect(Collectors.joining("\n"));
}

@GetMapping
Expand All @@ -85,6 +78,11 @@ public String readBlobResource() throws IOException {
Charset.defaultCharset());
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

@PostMapping
public String writeBlobResource(@RequestBody String data) throws IOException {
try (OutputStream os = ((WritableResource) this.azureBlobResource).getOutputStream()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.WritableResource;
import org.springframework.stereotype.Component;

import java.io.OutputStream;

@Component
public class SampleDataInitializer implements CommandLineRunner, ResourceLoaderAware {
final static Logger logger = LoggerFactory.getLogger(SampleDataInitializer.class);

private ResourceLoader resourceLoader;

@Value("${spring.cloud.azure.storage.blob.container-name}")
private String containerName;

/**
* 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.
*/
@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 ...");
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,15 @@

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 implements CommandLineRunner {
final static Logger logger = LoggerFactory.getLogger(StorageApplication.class);

@Autowired
@Qualifier("webApplicationContext")
private ResourceLoader resourceLoader;

@Value("${storage-container-name}")
private String containerName;

public class StorageApplication {
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 {

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