【问题标题】:Specifying proxyUser and proxyPassword thru proxy server Microsoft Azure Storage SDK for Java通过代理服务器 Microsoft Azure Storage SDK for Java 指定 proxyUser 和 proxyPassword
【发布时间】:2017-03-21 15:49:20
【问题描述】:

Connecting to Azure storage account thru proxy server Microsoft Azure Storage SDK for Java 告诉我如何使用 OperationContext 指定 proxyHost 和 Port。

我仍然不知道如何指定 proxyUser 和 proxyPassword 属性。

我遇到 StorageException:服务器遇到未知故障:当我尝试以下代码时:

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);
    }

【问题讨论】:

  • 几个问题: 1. 为什么要用setProperty设置proxyHost和proxyPort两次? 2. 下面我链接到this question 来演示如何在代理上设置用户名和密码。您提到您遵循了它,但我没有看到 setProperty 代码。当你尝试这个时发生了什么? 3. 为什么要同时制作 System 代理和 OperationContext 代理?
  • 我看了你提到的问题和其他类似的帖子。设置 proxyUser 和 proxyPassword 不适用于 URLConnections。根据帖子,您需要我包含的身份验证器代码。我尝试将 proxyUser 设置为 proxyPassword,但它没有用。至于设置 proxyHost 和 proxyPort twicw,我设置 http 和 https 只是为了安全。您对系统代理和操作上下文代理的重复是正确的。我不应该两者都需要。我尝试了各种排列组合 - 只是系统代理,只是操作上下文,两者都 - 没有任何效果。救命!
  • 无法在 URL 连接上设置代理用户和密码,但可以使用 System.setProperty 设置,如该链接所示。我在你的代码中没有看到。你能试试吗?
  • 好的。再次尝试(请参阅更新的代码)并得到相同的超时错误(遇到StorageException:发生未知故障:操作超时)

标签: java azure sdk azure-storage


【解决方案1】:

要绕过代理,请按如下方式使用,它可以按预期工作,并且已经过测试。

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 或 C# 然后请将以下代码添加到“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>

【讨论】:

    【解决方案2】:
    1. 您可以为 JDK 指定代理。这将适用于所有 Java HTTP 请求,并且是大多数人想要的。 System.setProperty(string, string) 允许您设置主机、端口、用户和密码。

    2. 您可以为库指定代理。查看 OperationContext 以了解代理设置。不幸的是,代理本身不支持用户名和密码,所以这不是通过此方法的选项。这是 Java 限制。在我们的下一个版本中,我们将为此支持库范围的标志,而不是要求每个请求都发送代理。

    【讨论】:

    • 嗨,Emily,我尝试了 System.setProperty 方法,但没有帮助。我还尝试使用 Authenticator 指定代理用户和密码,但这也没有帮助。这是我根据研究包含的身份验证器代码: Authenticator.setDefault( new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( authUser, authPassword.toCharArray()); } } );
    • 请参阅this question 了解更多信息,并根据您的尝试和发生的情况更新您的问题。
    • 是的,我基于我的代码,我遇到了 StorageException:服务器遇到未知故障:
    • @ashwin-iyengar 请更新您的问题。不要使用 cmets。 cmets的适当用途可以找到here
    • 嗨,Emily,我已经更新了这个问题。对此感到抱歉。
    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2020-03-13
    相关资源
    最近更新 更多