diff --git a/azure-spring-boot-samples/azure-storage-spring-boot-sample/src/main/resources/application.properties b/azure-spring-boot-samples/azure-storage-spring-boot-sample/src/main/resources/application.properties index 5ed18a75f..53a356f6f 100644 --- a/azure-spring-boot-samples/azure-storage-spring-boot-sample/src/main/resources/application.properties +++ b/azure-spring-boot-samples/azure-storage-spring-boot-sample/src/main/resources/application.properties @@ -2,3 +2,7 @@ azure.storage.account-name=put-your-azure-storage-account-name-here azure.storage.account-key=put-your-azure-storage-account-key-here azure.storage.container-name=put-your-azure-storage-container-name-here azure.storage.enable-https=true + +# Set the below property to true if you want to use the emulator +azure.storage.use-emulator=false +azure.storage.emulator-blob-host=put-your-emulator-blob-service-host-here diff --git a/azure-spring-boot-starters/azure-storage-spring-boot-starter/README.md b/azure-spring-boot-starters/azure-storage-spring-boot-starter/README.md index ae63ee6d0..a376fb628 100644 --- a/azure-spring-boot-starters/azure-storage-spring-boot-starter/README.md +++ b/azure-spring-boot-starters/azure-storage-spring-boot-starter/README.md @@ -26,6 +26,19 @@ azure.storage.container-name=put-your-azure-storage-container-name-here With above configuration, a `ServiceURL` and a `ContainerURL` bean will be created. `azure.storage.container-name` is optional, you can also create `ContainerURL` from `ServiceURL` by `ServiceURL#createContainerURL(String containerName)`. +### Use storage emulators for non production environments + +If you intend to use [Azure Storage Emulator](https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite) or [Azurite](https://github.com/Azure/Azurite) in non production environment, you can enable the emulator use and specify the blob service endpoint. +Open 'application-dev.properties' file and add below property taken from your emulator configurations. + +``` +azure.storage.account-name=put-your-emulator-storage-account-name-here +azure.storage.account-key=put-your-emulator-storage-account-key-here +azure.storage.container-name=put-your-emulator-storage-container-name-here +azure.storage.use-emulator=true +azure.storage.emulator-blob-host=put-your-emulator-blob-service-host-here +``` + ### Add auto-wiring code Add below alike code to auto-wire the `ServiceURL` bean and `ContainerURL` bean. For details usage, please reference this [document](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-java-v10#upload-blobs-to-the-container). diff --git a/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageAutoConfiguration.java b/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageAutoConfiguration.java index 150899a02..d57667c2d 100644 --- a/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageAutoConfiguration.java +++ b/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageAutoConfiguration.java @@ -64,6 +64,10 @@ public ServiceURL createServiceUrl(@Autowired(required = false) PipelineOptions } private URL getURL() throws MalformedURLException { + if (properties.isUseEmulator()) { + LOG.debug("Using emulator address instead.."); + return new URL(String.format("%s/%s", properties.getEmulatorBlobHost(), properties.getAccountName())); + } if (properties.isEnableHttps()) { return new URL(String.format(BLOB_HTTPS_URL, properties.getAccountName())); } diff --git a/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageProperties.java b/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageProperties.java index a60b6bbc4..8f16ce2e5 100644 --- a/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageProperties.java +++ b/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/storage/StorageProperties.java @@ -25,6 +25,14 @@ public class StorageProperties { @Setter private String accountKey; + @Getter + @Setter + private boolean useEmulator = false; + + @Getter + @Setter + private String emulatorBlobHost; + @Getter @Setter private String containerName; diff --git a/azure-spring-boot/src/test/java/com/microsoft/azure/spring/autoconfigure/storage/StoragePropertiesTest.java b/azure-spring-boot/src/test/java/com/microsoft/azure/spring/autoconfigure/storage/StoragePropertiesTest.java index 0469601f5..e21651e61 100644 --- a/azure-spring-boot/src/test/java/com/microsoft/azure/spring/autoconfigure/storage/StoragePropertiesTest.java +++ b/azure-spring-boot/src/test/java/com/microsoft/azure/spring/autoconfigure/storage/StoragePropertiesTest.java @@ -16,10 +16,14 @@ public class StoragePropertiesTest { private static final String ACCOUNT_NAME_PROP = "azure.storage.account-name"; private static final String ACCOUNT_KEY_PROP = "azure.storage.account-key"; private static final String CONTAINER_NAME_PROP = "azure.storage.container-name"; + private static final String USE_EMULATOR_PROP = "azure.storage.use-emulator"; + private static final String EMULATOR_BLOB_HOST_PROP = "azure.storage.emulator-blob-host"; private static final String ACCOUNT_NAME = "fakeStorageAccountName"; private static final String ACCOUNT_KEY = "ZmFrZUFjY291bnRLZXk="; /* Base64 encoded for string fakeAccountKey */ private static final String CONTAINER_NAME = "fakestoragecontainername"; + private static final boolean USE_EMULATOR = true; + private static final String EMULATOR_BLOB_HOST = "http://127.0.0.1:1000"; private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(StorageAutoConfiguration.class)); @@ -36,6 +40,19 @@ public void canSetProperties() { }); } + @Test + public void canSetEmulatorProperties() { + contextRunner.withPropertyValues(propValuePair(ACCOUNT_NAME_PROP, ACCOUNT_NAME), + propValuePair(ACCOUNT_KEY_PROP, ACCOUNT_KEY), propValuePair(CONTAINER_NAME_PROP, CONTAINER_NAME), + propValuePair(USE_EMULATOR_PROP, Boolean.toString(USE_EMULATOR)), + propValuePair(EMULATOR_BLOB_HOST_PROP, EMULATOR_BLOB_HOST)) + .run(context -> { + final StorageProperties properties = context.getBean(StorageProperties.class); + assertThat(properties.isUseEmulator()).isEqualTo(USE_EMULATOR); + assertThat(properties.getEmulatorBlobHost()).isEqualTo(EMULATOR_BLOB_HOST); + }); + } + @Test public void emptySettingNotAllowed() { try {