Skip to content

Commit

Permalink
Update Storageblob sample (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
backwind1233 authored Jan 26, 2022
1 parent eb58e8c commit f219a53
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,59 @@

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

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

0 comments on commit f219a53

Please sign in to comment.