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.Running this sample will be charged by Azure. You can check the usage and bill at this link.
Running this sample will be charged by Azure. You can check the usage and bill at this link.
此代码示例演示如何使用Spring Cloud Azure存储读取和写入带有Spring资源抽象的Azure存储文件起动装置,运行此示例将由Azure收费。你可以在这个链接上查看用法和帐单。
运行此示例将由Azure收费。你可以在这个链接上查看用法和帐单。
Maven coordinates:
com.microsoft.azure spring-starter-azure-storageGradle coordinates:
dependencies {
compile group: ‘com.microsoft.azure’, name: ‘spring-starter-azure-storage’
}
Access key based usage
Create Azure Storage
Update application.properties
spring.cloud.azure.storage.account=[storage-account-name]
# Fill storage account access key copied from portal
spring.cloud.azure.storage.access-key=[storage-account-accesskey]
Credential file based usage
Create azure credential file. Please see how to create credential file.
$ az login
$ az account set --subscription <name or id>
$ az ad sp create-for-rbac --sdk-auth > my.azureauth
Make sure my.azureauth is encoded with UTF-8.
Put credential file under src/main/resources/.
Create Azure Storage. Or enable auto create resources feature in application.properties:
spring.cloud.azure.auto-create-resources=true
# Example region is westUS, northchina
spring.cloud.azure.region=[region]
Update application.properties
# Enter 'my.azureauth' here if following step 1 and 2
spring.cloud.azure.credential-file-path=[credential-file-path]
spring.cloud.azure.resource-group=[resource-group]
spring.cloud.azure.storage.account=[account-name]
How to run
Update application.properties
# Default environment is GLOBAL. Provide your own if in another environment
# Example environment is China, GLOBAL
# spring.cloud.azure.environment=[environment]
# Change into your containerName, blobName
blob=azure-blob://containerName/blobName
Start the StorageApplication Spring Boot app.
$ mvn spring-boot:run
Send a POST request to update file contents:
$ curl -d 'new message' -H 'Content-Type: text/plain' localhost:8080/blob
Verify by sending a GET request
$ curl -XGET http://localhost:8080/blob
Delete the resources on Azure Portal to avoid unexpected charges.