0

Connecting to Azure storage account thru proxy server Microsoft Azure Storage SDK for Java tells me how to specify proxyHost and Port using OperationContext.

I still cannot figure out how to specify the proxyUser and proxyPassword attributes.

I get a StorageException encountered: The server encountered an unknown failure: when I try the following code:

Authenticator.setDefault(
           new Authenticator() {
              public PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(
                       authUser, authPassword.toCharArray());
              }
           }
        );

        System.setProperty("http.proxyHost", proxyHost);
        System.setProperty("http.proxyPort", proxyPort);



        System.setProperty("https.proxyHost", proxyHost);
        System.setProperty("https.proxyPort", proxyPort);



    System.setProperty("http.proxyUser", authUser);
    System.setProperty("http.proxyPassword", authPassword);

    System.setProperty("https.proxyUser", authUser);
    System.setProperty("https.proxyPassword", authPassword);



    try {
        CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);   
        CloudBlobClient serviceClient = account.createCloudBlobClient();     
         CloudBlobContainer container = serviceClient.getContainerReference(resourcePrefix);
        container.createIfNotExists(null, op);
        CloudBlockBlob blob = container.getBlockBlobReference(resourceName);
        File sourceFile = new File(resourceName);
        blob.upload(new FileInputStream(sourceFile), sourceFile.length());
    }
    catch (FileNotFoundException fileNotFoundException) {
        System.out.print("FileNotFoundException encountered: ");
        System.out.println(fileNotFoundException.getMessage());
        System.exit(-1);
    }
    catch (StorageException storageException) {
        System.out.print("StorageException encountered: ");
        System.out.println(storageException.getMessage());
        System.exit(-1);
    }
    catch (Exception e) {
        System.out.print("Exception encountered: ");
        System.out.println(e.getMessage());
        System.exit(-1);
    }
Community
  • 1
  • 1
  • Couple questions: 1. Why are you setting proxyHost and proxyPort twice using setProperty? 2. Below I linked to [this question](http://stackoverflow.com/questions/1626549/authenticated-http-proxy-with-java) as a demonstration of how to set username and password on the proxy. You mentioned you followed it but I don't see the setProperty code. What happened when you tried this? 3. Why are you trying to both make a System proxy and an OperationContext proxy? – Emily Gerner Feb 04 '16 at 17:49
  • I looked at the question you refer to and other similar posts. Setting proxyUser and proxyPassword does not work for URLConnections. According to the posts, you need the Authenticator code which I have included. I tried it with setting proxyUser an proxyPassword and it did not work. As for setting the proxyHost and proxyPort twicw, i am setting both http and https just to be safe. You are right about the duplication of system proxy and operationcontext proxy. I shouldn't need both. I hve tried various permutation combinations - just system proxy, just op context, both - nothing works. Help! – Ashvin Iyengar Feb 04 '16 at 21:51
  • Proxy user and password can't be set on URL connections but they can be set with System.setProperty like that link demonstrates. I don't see that in your code. Could you try that? – Emily Gerner Feb 05 '16 at 17:35
  • Ok. Tried it again (please see updated code) and get the same timeout error (StorageException encountered: An unknown failure occurred : Operation timed out) – Ashvin Iyengar Feb 05 '16 at 18:24

2 Answers2

1

To by pass the proxy then please use like below, it works as expected and same has been tested.

public class AzureUpload {

    // Define the connection-string with your values
    /*public static final String storageConnectionString =
        "DefaultEndpointsProtocol=http;" +
        "AccountName=your_storage_account;" +
        "AccountKey=your_storage_account_key";*/
    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=test2rdrhgf62;" +
            "AccountKey=1gy3lpE7Du1j5ljKiupjhgjghjcbfgTGhbntjnRfr9Yi6GUQqVMQqGxd7/YThisv/OVVLfIOv9kQ==";

    // Define the path to a local file.
    static final String filePath = "D:\\Project\\Supporting Files\\Jar's\\Azure\\azure-storage-1.2.0.jar";
    static final String file_Path = "D:\\Project\\Healthcare\\Azcopy_To_Azure\\data";

    public static void main(String[] args) {
        try
        {
            // Retrieve storage account from connection-string.
            //String storageConnectionString = RoleEnvironment.getConfigurationSettings().get("StorageConnectionString");
            //Proxy httpProxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("132.186.192.234",8080));
            System.setProperty("http.proxyHost", "102.122.15.234");
            System.setProperty("http.proxyPort", "80");
            System.setProperty("https.proxyUser", "ad001\\empid001");
            System.setProperty("https.proxyPassword", "pass!1");
            // Retrieve storage account from connection-string.
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();


            // Get a reference to a container.
            // The container name must be lower case
            CloudBlobContainer container = blobClient.getContainerReference("rpmsdatafromhospital");

            // Create the container if it does not exist.
            container.createIfNotExists();

            // Create a permissions object.
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Include public access in the permissions object.
            containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

            // Set the permissions on the container.
            container.uploadPermissions(containerPermissions);

            // Create or overwrite the new file to blob with contents from a local file.
            /*CloudBlockBlob blob = container.getBlockBlobReference("azure-storage-1.2.0.jar");
            File source = new File(filePath);
            blob.upload(new FileInputStream(source), source.length());*/

            String envFilePath = System.getenv("AZURE_FILE_PATH");

            //upload list of files/directory to blob storage
            File folder = new File(envFilePath);
            File[] listOfFiles = folder.listFiles();

            for (int i = 0; i < listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());

                CloudBlockBlob blob = container.getBlockBlobReference(listOfFiles[i].getName());
                File source = new File(envFilePath+"\\"+listOfFiles[i].getName());
                blob.upload(new FileInputStream(source), source.length());
                System.out.println("File " + listOfFiles[i].getName()+ " upload successful");

              }
              //directory upload
              /*else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());

                CloudBlockBlob blob = container.getBlockBlobReference(listOfFiles[i].getName());
                File source = new File(file_Path+"\\"+listOfFiles[i].getName());
                blob.upload(new FileInputStream(source), source.length());
              }*/
            }

        }catch (Exception e)
        {
            // Output the stack trace.
            e.printStackTrace();
        }
    }
}

.Net or C# then please add below code to "App.config"

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <system.net> 
     <defaultProxy enabled="true" useDefaultCredentials="true"> 
       <proxy usesystemdefault="true" /> 
     </defaultProxy>
    </system.net>

</configuration>
0
  1. You can specify a proxy for the JDK. This will apply to all Java HTTP requests and is what most folks want. System.setProperty(string, string) allows you to set host, port, user and password.

  2. You can specify a Proxy for the library. Take a look at OperationContext for the proxy setting. Unfortunately Proxy itself does not support username and password so this is not an option via this method. This is a Java limitation. In our next release we will support a library-wide flag for this rather than requiring proxy be sent with each request.

Emily Gerner
  • 2,427
  • 16
  • 16
  • Hi Emily, I tried the System.setProperty methods but it did not help. I alos tried specifying the proxyUser and Password using Authenticator but that did not help either. Here's the Authenticator code I included based on research: Authenticator.setDefault( new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( authUser, authPassword.toCharArray()); } } ); – Ashvin Iyengar Feb 03 '16 at 22:18
  • See [this question](http://stackoverflow.com/questions/1626549/authenticated-http-proxy-with-java) for more info and please update your question with what you tried and what happened. – Emily Gerner Feb 03 '16 at 22:24
  • Yes I based my code from that and I get StorageException encountered: The server encountered an unknown failure: – Ashvin Iyengar Feb 03 '16 at 22:43
  • @ashwin-iyengar Please update your question. Do not use the comments. Appropriate uses for comments can be found [here](http://stackoverflow.com/help/privileges/comment). – Emily Gerner Feb 03 '16 at 22:58
  • Hi Emily, I have updated the question. Sorry about that. – Ashvin Iyengar Feb 04 '16 at 00:49