• Java通过代理上传文件到Azure blob


    最近遇到一个问题,通过Azure的Java类库上传文件到Azure blob,但是客户环境通过Proxy上网的,这就需要通过代理连接blob。
    官方文档里都没有提及如何通过代理上传,解决这个问题浪费了一些时间,在这里记录一下。
    Maven引用最新版本的Azure类库

    <dependencies>
          <dependency>
              <groupId>com.azure</groupId>
              <artifactId>azure-storage-blob</artifactId>
              <version>12.8.0</version>
          </dependency>      
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>8.6.5</version>
        </dependency>
    <dependency>

    调用代码如下:

    import java.net.Authenticator;
    import java.net.InetSocketAddress;
    import java.net.PasswordAuthentication;
    import com.azure.core.http.HttpClient;
    import com.azure.core.http.ProxyOptions;
    import com.azure.core.http.netty.NettyAsyncHttpClientBuilder;
    import com.azure.storage.blob.BlobClient;
    import com.azure.storage.blob.BlobClientBuilder;
    public class BlobUpload {
        
        //TODO:填写正确的代理信息
        private String ProxyAddress = "";
        private int ProxyPort = 0;
        //TODO:填写正确的用户名和密码
        private String ProxyUserName = "";
        private String ProxyPassword = "";
        
        //TODO:填写正确的Blob信息
        private String ConnectStr = "";
        private String ContainerName = "";
        private String BlobName = "";
        
        //TODO:填写要上传的文件名字
        private String LocalFileName = "";
        
        public static void main(String args[]) throws Exception {      
            try {            
                new BlobUpload().upload();    
                System.out.println("上传成功。");
            }
            catch(Exception ex){
                ex.printStackTrace();
            }
        }    
        private void upload() {
            this.getBlobClient().uploadFromFile(LocalFileName, true);
        }
        
        private BlobClient getBlobClient() {
            return new BlobClientBuilder()
                    .connectionString(ConnectStr)                
                    .containerName(ContainerName)
                    .blobName(BlobName)
                    .httpClient(this.getHttpClient())
                    .buildClient();                
        }
        
        private HttpClient getHttpClient() {
            NettyAsyncHttpClientBuilder builder = new NettyAsyncHttpClientBuilder();    
            ProxyOptions proxy = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(ProxyAddress, ProxyPort));                
            Authenticator authenticator = new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(ProxyUserName, ProxyPassword.toCharArray()));
                }
            };
            Authenticator.setDefault(authenticator);
            return builder.proxy(proxy).build();        
        }
    }
  • 相关阅读:
    CSS 仿 iOS 系统通知数字样式
    Asp.Net Grieview Eval 绑定数据 调用JS事件
    C#:decimal的去0显示
    WebService 错误:无法加载协定为xxx的终结点配置部分,因为找到了该协定的多个终结点配置
    C#:调用webservice时提示对操作的回复消息正文进行反序列化时出错
    C#:生成短网址
    C#:优惠券代码
    C# 调用webservice 几种办法(转载)
    ASP.NET: 正在中止线程 错误原及解决方法
    IOS:Safari不兼容Javascript中的Date问题
  • 原文地址:https://www.cnblogs.com/Farseer1215/p/13514336.html
Copyright © 2020-2023  润新知