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

Update Storageblob sample #167

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 @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why CommandLineRunner and web application at the same time?

Copy link
Contributor Author

@backwind1233 backwind1233 Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to use this CommandLineRunner to initialize the application, create some data in the blob containers so that users don't need to create some data in order to test AzureStorageBlobProtocolResolver.

Resource[] resources = azureStorageBlobProtocolResolver.getResources("azure-blob://" + containerName + "/*.txt");

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