turcsanyip commented on a change in pull request #3742: NIFI-6550: Create 
controller service for Azure Storage Credentials
URL: https://github.com/apache/nifi/pull/3742#discussion_r326641562
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-azure-bundle/nifi-azure-processors/src/main/java/org/apache/nifi/processors/azure/storage/utils/AzureStorageUtils.java
 ##########
 @@ -112,55 +125,76 @@ private AzureStorageUtils() {
      *                 Account Name, Account Key or SAS Token. This can be 
null if not available.
      */
     public static CloudBlobClient createCloudBlobClient(ProcessContext 
context, ComponentLog logger, FlowFile flowFile) {
-        final String accountName;
-        final String accountKey;
-        final String sasToken;
-
-        if (flowFile == null) {
-            accountName = 
context.getProperty(AzureStorageUtils.ACCOUNT_NAME).evaluateAttributeExpressions().getValue();
-            accountKey = 
context.getProperty(AzureStorageUtils.ACCOUNT_KEY).evaluateAttributeExpressions().getValue();
-            sasToken = 
context.getProperty(AzureStorageUtils.PROP_SAS_TOKEN).evaluateAttributeExpressions().getValue();
+        final AzureStorageCredentialsDetails storageCredentialsDetails = 
getStorageCredentialsDetails(context, flowFile);
+
+        final URI baseUri = getBaseUri(FORMAT_BASE_URI, 
storageCredentialsDetails.getStorageAccountName(), context, logger);
+
+        final CloudBlobClient cloudBlobClient = new CloudBlobClient(baseUri, 
storageCredentialsDetails.getStorageCredentials());
+
+        return cloudBlobClient;
+    }
+
+    public static AzureStorageCredentialsDetails 
getStorageCredentialsDetails(PropertyContext context, FlowFile flowFile) {
+        final Map<String, String> attributes = flowFile != null ? 
flowFile.getAttributes() : Collections.emptyMap();
+
+        final AzureStorageCredentialsService storageCredentialsService = 
context.getProperty(STORAGE_CREDENTIALS_SERVICE).asControllerService(AzureStorageCredentialsService.class);
+
+        if (storageCredentialsService != null) {
+            return 
storageCredentialsService.getStorageCredentialsDetails(attributes);
         } else {
-            accountName = 
context.getProperty(AzureStorageUtils.ACCOUNT_NAME).evaluateAttributeExpressions(flowFile).getValue();
-            accountKey = 
context.getProperty(AzureStorageUtils.ACCOUNT_KEY).evaluateAttributeExpressions(flowFile).getValue();
-            sasToken = 
context.getProperty(AzureStorageUtils.PROP_SAS_TOKEN).evaluateAttributeExpressions(flowFile).getValue();
+            return createStorageCredentialsDetails(context, attributes);
+        }
+    }
+
+    public static AzureStorageCredentialsDetails 
createStorageCredentialsDetails(PropertyContext context, Map<String, String> 
attributes) {
+        final String accountName = 
context.getProperty(ACCOUNT_NAME).evaluateAttributeExpressions(attributes).getValue();
+        final String accountKey = 
context.getProperty(ACCOUNT_KEY).evaluateAttributeExpressions(attributes).getValue();
+        final String sasToken = 
context.getProperty(PROP_SAS_TOKEN).evaluateAttributeExpressions(attributes).getValue();
+
+        if (StringUtils.isBlank(accountName)) {
+            throw new IllegalArgumentException(String.format("'%s' must not be 
empty.", ACCOUNT_NAME.getDisplayName()));
         }
 
-        CloudBlobClient cloudBlobClient;
+        StorageCredentials storageCredentials;
+
+        if (StringUtils.isNotBlank(accountKey)) {
+            storageCredentials = new 
StorageCredentialsAccountAndKey(accountName, accountKey);
 
 Review comment:
   Actually, it did work. But after having a look at the source code of the 
Azure lib (and some debugging), it has been revealed that CloudStorageAccount 
also handles the secondary storage urls by default (more info: 
https://docs.microsoft.com/en-us/azure/storage/common/storage-redundancy-grs).
   I modified the code to use CloudStorageAccount in case of both Account Name 
/ Key and SAS Token authentication (the secondary url was not handled for the 
SAS Token case earlier).
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to