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

avoid hard code resource #206

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 @@ -135,11 +135,11 @@ mvn clean spring-boot:run
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"
curl http://localhost:8080/blob/file1.txt -d "new message" -H "Content-Type: text/plain"
```
1.2 Verify by sending a GET request.
```shell
curl -XGET http://localhost:8080/blob
curl -XGET http://localhost:8080/blob/file1.txt
```

2. [Optional] Using resourceLoader to get Azure Storage Blob resource with filename.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ public class BlobController implements ResourceLoaderAware {

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

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

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

Expand Down Expand Up @@ -71,10 +68,12 @@ public String getFileNamesWithProtocolResolver() throws IOException {
return Stream.of(resources).map(Resource::getFilename).collect(Collectors.joining("\n"));
}

@GetMapping
public String readBlobResource() throws IOException {
@GetMapping("/{fileName}")
public String readBlobResource(@PathVariable("fileName") String fileName) throws IOException {
// get a BlobResource
Resource storageBlobResource = resourceLoader.getResource("azure-blob://" + containerName + "/" + fileName);
return StreamUtils.copyToString(
this.azureBlobResource.getInputStream(),
storageBlobResource.getInputStream(),
Charset.defaultCharset());
}

Expand All @@ -83,9 +82,11 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

@PostMapping
public String writeBlobResource(@RequestBody String data) throws IOException {
try (OutputStream os = ((WritableResource) this.azureBlobResource).getOutputStream()) {
@PostMapping("/{fileName}")
public String writeBlobResource(@PathVariable("fileName") String fileName, @RequestBody String data) throws IOException {
// get a BlobResource
Resource storageBlobResource = resourceLoader.getResource("azure-blob://" + containerName + "/" + fileName);
try (OutputStream os = ((WritableResource) storageBlobResource).getOutputStream()) {
os.write(data.getBytes());
}
return "blob was updated";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,4 @@ spring:
blob:
account-name: ${AZURE_STORAGE_ACCOUNT}
container-name: ${STORAGE_CONTAINER_NAME}
# Resource location, used in this sample
resource:
blob: azure-blob://${STORAGE_CONTAINER_NAME}/youfilename.txt