Unable to access the azure data lake contents through network proxy using Azure SDK for Java

I am trying to list out the files in azure data lake Gen 2 container using azure sdk for java API through network proxy, but getting connection timed out errors every time. Followed the sample code from azure website itself.

Sample Code Link:: https://learn.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-java

Code

public void connectToAzureDataLake() {
    String endpoint = "endPoint_URL";

    ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("clientId")
            .clientSecret("clientSecret")
            .tenantId("tenantId")
            .build();

    int proxyPort = 1111;
    HttpClient httpClient = new NettyAsyncHttpClientBuilder()
            .proxy(new ProxyOptions(ProxyOptions.Type.HTTP,
                    new InetSocketAddress("proxyURL", proxyPort))
                    .setCredentials("proxyUsername", "proxyPassword")).build();


    DataLakeServiceClientBuilder builder = new DataLakeServiceClientBuilder();
    DataLakeServiceClient dataLakeServiceClient = builder.credential(clientSecretCredential)
                                                    .endpoint(endpoint).httpClient(httpClient).buildClient();


    DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder()
            .endpoint(endpoint + "/file_system_name")
            .credential(clientSecretCredential)
            .buildClient();
    listFilesInDirectory(dataLakeFileSystemClient);
}

private void listFilesInDirectory(DataLakeFileSystemClient fileSystemClient) {
    ListPathsOptions options = new ListPathsOptions();
    options.setPath("folder_to_list_contents");
    PagedIterable<PathItem> pagedIterable =
            fileSystemClient.listPaths(options, null);
    java.util.Iterator<PathItem> iterator = pagedIterable.iterator();
    PathItem item = iterator.next();

    while (item != null) {
        System.out.println(item.getName());
        if (!iterator.hasNext()) {
            break;
        }
        item = iterator.next();
    }
}

Even tried providing the proxy details through system.properties and -D options as well, but not succeeded.

-D options given:

-Dhttp.proxyHost
-Dhttp.proxyPort
-Dhttp.proxyUser
-Dhttp.proxyPassword
-Dhttps.proxyHost
-Dhttps.proxyPort
-Dhttps.proxyUser
-Dhttps.proxyPassword

Any pointers to resolve the issue would be highly appreciated. Thanks in advance.

Upvotes: 1

Views: 948

Answers (2)

Solution

public void connectToAzureDataLake() {
    String endpoint = "endPoint_URL";
    int proxyPort = 1111;
    ProxyOptions proxyOptions = new new ProxyOptions(ProxyOptions.Type.HTTP,
                    new InetSocketAddress("proxyURL", proxyPort))
                    .setCredentials("proxyUsername", "proxyPassword");
    HttpClient httpClient = new NettyAsyncHttpClientBuilder()
            .proxy(proxyOptions).build();

    ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("clientId")
            .clientSecret("clientSecret")
            .tenantId("tenantId")
            .httpClient(httpClient)
            .build();


    DataLakeServiceClientBuilder builder = new DataLakeServiceClientBuilder();
    DataLakeServiceClient dataLakeServiceClient = builder.credential(clientSecretCredential)
                                                    .endpoint(endpoint).httpClient(httpClient).buildClient();


    DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder()
            .endpoint(endpoint + "/file_system_name")
            .credential(clientSecretCredential)
            .httpClient(httpClient)
            .buildClient();
    listFilesInDirectory(dataLakeFileSystemClient);
}

Upvotes: 1

krishg
krishg

Reputation: 6508

[I would otherwise put this as comment since I do not have any proxy handy to test at this moment, but due to char limit, posting as answer].

It looks like you also need to set proxy option to ClientSecretCredentialBuilder

public void connectToAzureDataLake() {
    String endpoint = "endPoint_URL";
    int proxyPort = 1111;
    ProxyOptions proxyOptions = new new ProxyOptions(ProxyOptions.Type.HTTP,
                    new InetSocketAddress("proxyURL", proxyPort))
                    .setCredentials("proxyUsername", "proxyPassword");

    ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("clientId")
            .clientSecret("clientSecret")
            .tenantId("tenantId")
            .proxyOptions(proxyOptions)
            .build();

    HttpClient httpClient = new NettyAsyncHttpClientBuilder()
            .proxy(proxyOptions).build();


    DataLakeServiceClientBuilder builder = new DataLakeServiceClientBuilder();
    DataLakeServiceClient dataLakeServiceClient = builder.credential(clientSecretCredential)
                                                    .endpoint(endpoint).httpClient(httpClient).buildClient();


    DataLakeFileSystemClient dataLakeFileSystemClient = new DataLakeFileSystemClientBuilder()
            .endpoint(endpoint + "/file_system_name")
            .credential(clientSecretCredential)
            .buildClient();
    listFilesInDirectory(dataLakeFileSystemClient);
}

Upvotes: 1

Related Questions