Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUERY] What is the recommended way to configure a BlobContainerClientBuilder to use a non-public cloud? #44146

Open
2 tasks done
fabiim opened this issue Feb 11, 2025 · 3 comments
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Storage Storage Service (Queues, Blobs, Files)

Comments

@fabiim
Copy link

fabiim commented Feb 11, 2025

Query/Question
Hi all, I'm a bit lost on how to configure a BlobContainerClientBuilder to connect to different clouds.

  1. Is there any way I can use the AZURE_CLOUD environment variable for that?
  2. If I can't use the environment variable do I need to do anything other than setup the endpoint AND the authority host (in DefaultAzureCredentialBuilder()) ?

Setup (please complete the following information if applicable):

  • Library/Libraries: latest

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Query Added
  • Setup information Added
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 11, 2025
@joshfree
Copy link
Member

Here's a quick example of how to override the end points. You can also use the com.azure.core.amangement.AzureEnvironment class to get different endpoints.

import com.azure.storage.blob.BlobContainerClientBuilder;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;

public class BlobStorageConfig {
    /**
     * Configures a BlobContainerClientBuilder using Managed Identity authentication.
     *
     * @param containerName The name of the blob container. Must be between 3-63 characters,
     *                     start with a letter or number, and can contain only lowercase letters,
     *                     numbers, and dashes. Example: "my-container-name"
     * 
     * @param endpointUrl The complete URL to your blob storage endpoint. Format varies by environment:
     *                    - Azure Stack Hub: 
     *                      https://[account-name].blob.[region].[stack-domain]
     *                      Example: https://myaccount.blob.east.azurestack.contoso.com
     *                    
     *                    - Private Azure Cloud: 
     *                      https://[account-name].blob.core.[private-dns-zone]
     *                      Example: https://myaccount.blob.core.private.cloud.contoso.com
     *                    
     *                    - Custom Endpoint:
     *                      Any valid HTTPS endpoint that accepts Azure Blob storage API requests
     *                      Example: https://storage.internal.company.com
     *
     * @param clientId Optional: The client ID of a user-assigned managed identity.
     *                Pass null to use system-assigned managed identity.
     * 
     * @return Configured BlobContainerClientBuilder instance
     */
    public BlobContainerClientBuilder configureBlobClient(
            String containerName,
            String endpointUrl,
            String clientId) {
        
        // Create ManagedIdentityCredential
        ManagedIdentityCredential credential;
        if (clientId != null && !clientId.isEmpty()) {
            // Use user-assigned managed identity
            credential = new ManagedIdentityCredentialBuilder()
                .clientId(clientId)
                .build();
        } else {
            // Use system-assigned managed identity
            credential = new ManagedIdentityCredentialBuilder()
                .build();
        }

        // Configure the blob container client builder
        return new BlobContainerClientBuilder()
            .credential(credential)
            .containerName(containerName)
            .endpoint(endpointUrl);
    }

    // Example usage
    public static void main(String[] args) {
        // Container name example: lowercase letters, numbers, and dashes only
        String containerName = "user-uploads-2024";

        // Example endpoint URLs for different environments:
        
        // Azure Stack Hub example
        String azureStackEndpoint = "https://myaccount.blob.eastus.azurestack.contoso.com";
        
        // Private cloud example
        String privateCloudEndpoint = "https://myaccount.blob.core.private.cloud.contoso.com";
        
        // Custom endpoint example
        String customEndpoint = "https://storage.internal.company.com";

        // Choose the appropriate endpoint for your environment
        String endpointUrl = azureStackEndpoint;  // Change this to your actual endpoint
        
        // Optional: client ID for user-assigned managed identity
        String clientId = null;

        BlobStorageConfig config = new BlobStorageConfig();
        BlobContainerClientBuilder builder = config.configureBlobClient(
            containerName,
            endpointUrl,
            clientId
        );

        // Create the client
        BlobContainerClient containerClient = builder.buildClient();
    }
}

@joshfree joshfree added issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. Storage Storage Service (Queues, Blobs, Files) and removed needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. labels Feb 11, 2025
Copy link

Hi @fabiim. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

@fabiim
Copy link
Author

fabiim commented Feb 12, 2025

/unresolve

Thanks for the help @joshfree . I missed that you can have different blob DNS names beyond the variation of the other cloud types (like gov).

I am still unsure what to do about authentication.
In my case, the user will launch my app and can configure the credentials through whatever is supported by DefaultAzureCredential. If the user uses a non-public cloud setup, he can specify the blob storage endpoint. Will he need to give me the authorityHost endpoint (the one I pass to DefaultAzureCredentialBuilder#authorityHost for some or all types of authentication?

@github-actions github-actions bot added needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team and removed issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

2 participants