java - 通过代理服务器指定 proxyUser 和 proxyPassword Microsoft Azure Storage SDK for Java

标签 java azure sdk azure-storage

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

我仍然不知道如何指定 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);
    }

最佳答案

要绕过代理,请像下面一样使用,它按预期工作并且已经过测试。

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>

关于java - 通过代理服务器指定 proxyUser 和 proxyPassword Microsoft Azure Storage SDK for Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35185987/

相关文章:

java - Java中用分隔符分割复杂字符串

azure - Azure Artifacts 是否支持 PyPi?

Eclipse 中的 Android SDK DDMS 无法识别我的 Android 手机

java - 如何在 Play 框架中运行异步/非阻塞 MySQL 查询?

java - 在 spring 工具套件中找不到丢失的 com.css maven Artifact

mongodb - 在Windows Server 2012上安装MongoDB

swift - Realm - 在 Azure 上同步数据的最佳方式

ios - 运行 1 个自定义 shell 脚本中的 1 个卡住

ios - Facebook 移动应用安装广告跟踪安装路径

java - 如何在 map 中拥有具有多个值的键?