惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 佛西亚

https访问 asp.net Core上传文件 .NET Framework 4.0 DLL注册GAC 使用iText 7读取PDF文件中的文本和图片 用SQL Server Profiler跟踪AX执行的SQL语句 D365升级包 Ant Design Pro V5 + Django Restful Framework Token认证前台实现 Ant Design Pro V5 + Django Restful Framework Token认证后台实现(二) Ant Design Pro V5 + Django Restful Framework Token认证后台实现(一) D365 FO产生随机字符串 D365 FO Array增加排序 D365 FO无法命中断点 Ant Design Pro V5 开发时使用后台服务数据 JavaScript跨域访问 同步数据库报错 DataEntity增加关联DataSource 使用iText7操作PDF D365 FO Json序列化和反序列化 D365 FO操作Azure Blob
Java通过代理上传文件到Azure blob
佛西亚 · 2020-08-16 · via 博客园 - 佛西亚

最近遇到一个问题,通过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();        
    }
}