Skip to content

Instantly share code, notes, and snippets.

@saragluna
Last active December 17, 2021 14:39
Show Gist options
  • Save saragluna/562dde2a6b6b0e73a707333faf85bc82 to your computer and use it in GitHub Desktop.
Save saragluna/562dde2a6b6b0e73a707333faf85bc82 to your computer and use it in GitHub Desktop.
This the gist to demo how to use spring-cloud-azure-core module. The spring-cloud-azure-core module is the core module of all Spring Cloud Azure libraries. It tries to unify the build process of Azure Service client builders.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* The spring-cloud-azure-core module is the core module of all Spring Cloud Azure libraries. It tries to unify the
* build process of Azure Service client builders.
*/
public class SpringCoreUsages {
//============================
// Deomo how to use AzureStorageBlobProtocolResolver.java
@Service("BlobStorage")
public class BlobStorage implements Storage {
private static final String RESOURCE_SEARCH_PATTERN_PREFIX = "azure-blob://file-uploader/";
@Autowired
private AzureStorageBlobProtocolResolver storageResourcePatternResolver;
@Override
public List<String> listFiles() throws IOException {
Resource[] resources = storageResourcePatternResolver.getResources(RESOURCE_SEARCH_PATTERN_PREFIX + "*");
return Stream.of(resources)
.map(Resource::getFilename)
.collect(Collectors.toList());
}
}
//============================
// Deomo how to use the AzureServiceClientBuilderCustomizer.java
/**
* In the autoconfiguration module, we'll configure the builder factory according to the configuration properties
* user configured. But there are cases when users want to customize the builder factory, perhaps to set the credential
* to a user built one.
*/
// This bean will be picked up by the autoconfiguration for a `SecretClientBuilderFactory`
// https://github.com/Azure/azure-sdk-for-java/blob/f6f4d8263ae88e8896eac60a38fe600edbd53b79/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/keyvault/secrets/AzureKeyVaultSecretAutoConfiguration.java#L70
@Bean
CredentialCustomizer userDefinedCredentialCustomizer() {
AzureCliCredential cliCredential = new AzureCliCredentialBuilder().build();
CredentialCustomizer credentialCustomizer = new CredentialCustomizer(cliCredential);
return credentialCustomizer;
}
static class CredentialCustomizer implements AzureServiceClientBuilderCustomizer<SecretClientBuilder> {
private final TokenCredential credential;
CredentialCustomizer(TokenCredential tokenCredential) {
this.credential = tokenCredential;
}
@Override
public void customize(SecretClientBuilder builder) {
builder.credential(this.credential);
}
}
//============================
// Deomo how to use the StaticConnectionStringProvider.java
/**
* If the connection string are not convenient to pass via the application.properties, users can define a bean in the Spring context to provide it instead.
*/
// This StaticConnectionStringProvider will be picked up by the autoconfiguration of a `EventHubClientBuilderFactory`
// https://github.com/Azure/azure-sdk-for-java/blob/92af61521f76ea60e95cd5cec521ab1cf1c4d246/sdk/spring/spring-cloud-azure-autoconfigure/src/main/java/com/azure/spring/cloud/autoconfigure/eventhubs/AzureEventHubsClientBuilderConfiguration.java#L43
@Bean
StaticConnectionStringProvider<EventHubs> userDefinedEventHubsConnectionString() {
String connectionString = someMethodToGetConnectionString();
return new StaticConnectionStringProvider(new EventHubs(), connecionString);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment