Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Add support for emulator blob storage (#866)
Browse files Browse the repository at this point in the history
Co-authored-by: Xiaolu Dai <31124698+saragluna@users.noreply.github.com>
  • Loading branch information
hkakutalua and saragluna authored Apr 7, 2020
1 parent 370f9c0 commit 48257f3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 {
Expand Down

0 comments on commit 48257f3

Please sign in to comment.