This code sample demonstrates how to read and write files with the Spring Resource abstraction for Azure Storage using the Spring Cloud Azure storage starter, and build the native executable with Spring Native.
You will build an application that use Spring Resource abstraction to read and write data with Azure Storage Blob.
-
Docker for Buildpacks usage
-
Native Image for Native Build Tools usage
Windows version link: https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-22.0.0.2/graalvm-ce-java11-windows-amd64-22.0.0.2.zip
-
Maven
-
You can also import the code straight into your IDE:
- Starter the docker service.
- Run the command to start the Azurite with Docker:
docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
- Download Azure Storage Explorer and install.
- Configure Azure Storage Explorer:
There are two main ways to build a Spring Boot native application.
- System Requirements
Docker should be installed, see System Requirements for more details.
- Build the native application
Run mvn -Pbuildpack package spring-boot:build-image
, see Build the native application for more details.
mvn -Pbuildpack package spring-boot:build-image
- Run the native application
Run docker run --rm -p 8080:8080 issue-azure-storage-native:0.0.1-SNAPSHOT
, see Run the native application for more details.
docker run --rm -p 8080:8080 issue-azure-storage-native:0.0.1-SNAPSHOT
- System Requirements
GraalVM native-image
compiler should be installed, see System Requirements for more details. If using the Windows platform, you need to install Visual Studio Build Tools
, please use VS BuildTools 16.9.
- Build the native application
Run mvn -Pnative -DskipTests package
command using x64 Native Tools Command Prompt
, see Build the native application for more details.
mvn -Pnative -DskipTests package
This step will encounter the following exception:
java.lang.SecurityException: class "com.azure.spring.cloud.autoconfigure.compatibility.ContextBootstrapInitializer"'s signer information does not match signer information of other classes in the same package
If the build is successful, continue with the following content.
- Run the native application
Run target\issue-azure-storage-native
, see Run the native application for more details.
target\issue-azure-storage-native
-
Check out the following console log:
StorageApplication data initialization begin ... Write data to container=blobcontainer, filePath=azure-blob://blobcontainer/fileName-*.txt Downloaded data from the azure storage blob resource: data-* Get the data content through this address 'curl -XGET http://localhost:8080/blob/fileName-*.txt'. StorageApplication data initialization end ...
-
[Optional] Write and read a file.
2.1 Send a POST request to update file contents.curl http://localhost:8080/blob/file1.txt -d "new message" -H "Content-Type: text/plain"
2.2 Verify by sending a GET request.
curl -XGET http://localhost:8080/blob/file1.txt
-
[Optional] Using AzureStorageBlobProtocolResolver to get Azure Storage Blob resources with file pattern.
curl -XGET http://localhost:8080/blob
Verify in app's log that a similar messages was posted:
1 resources founded with pattern:*.txt
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.
The terraform destroy command terminates resources managed by your Terraform project.
To destroy the resources you created.
terraform -chdir=./terraform destroy -auto-approve
terraform -chdir=terraform destroy -auto-approve