连接到Azure存储帐户直通代理服务器,微软Azure存储SDK的Java [英] Connecting to Azure storage account thru proxy server Microsoft Azure Storage SDK for Java

查看:230
本文介绍了连接到Azure存储帐户直通代理服务器,微软Azure存储SDK的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们需要通过代理服务器(鱿鱼)访问Blob存储。

In our project we need to access the Blob Storage through a Proxy Server (squid).

我们计划使用微软Azure存储SDK的Java版本2.2.0
但它看起来像设置代理不是由API提供。
我可以使它通过代理的唯一方法是通过设置系统属性。

We are planning to use the Microsoft Azure Storage SDK for Java version 2.2.0. But it looks like setting the proxy is not provided by the API. The only way I could make it go through the proxy is by setting the System properties

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

但这种影响在我的JVM上运行的所有服务,这是危害不应该通过代理去其他服务。

But this affect all services that are running on my JVM which harms other services that not supposed to go via the Proxy.

在java的$ C $展望c那么它看起来像
com.microsoft.azure.storage.core.BaseRequest.createURLConnection(URI,RequestOptions,UriQueryBuilder,的OperationContext)。无代理调用java.net.URL.openConnection()。
在使用java.net.URL.openConnection(代理)可以提供所需的支持?

Looking at the java code it looks like com.microsoft.azure.storage.core.BaseRequest.createURLConnection(URI, RequestOptions, UriQueryBuilder, OperationContext). Is calling java.net.URL.openConnection() without proxy. While using java.net.URL.openConnection(Proxy) could provide the required support?

看起来有线,我认为这是不支持?结果
难道我在这里错过了什么?

It looks wired to me that this is not supported?
Do I miss something here?

更新:我开了一个问题在这个蔚蓝中存储Java的混帐,我会很乐意让你的输入,因为我想建议这个拉请求。

UPDATE: I opened an issue on this in azure-storage-java git, I would be happy to get your input as I want to suggest a pull request for this.

推荐答案

到目前为止,已经通过代理服务器没有Java SDK API支持直接访问Azure存储,因为BaseRequest类小姐url.openConnection(代理)的功能公共静态的HttpConnection createURLConnection(......)。

So far there have been no Java SDK API support access directly Azure Storage through proxy server, because BaseRequest Class miss "url.openConnection(proxy)" in the function "public static HttpConnection createURLConnection(...)".

每我的经验,有两种方法可以帮助您实现访问功能。

Per my experience, there are two ways to help you implement the access function.

该之一是,你可以使用Azure存储REST API通过java.net.Proxy类访问存储服务。

The one is that you can use Azure Storage REST API through the java.net.Proxy Class to access storage service.

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
URLConnection conn = url.openConnection(proxy);
And if you should be authorize proxy user & password, you can do it as the follows:
//Proxy-Authorization: Basic <Base64.encode(user:password)>
String headerKey = "Proxy-Authorization";
String headerValue = "Basic " + Base64.encode(user+":"+password);
conn.setRequestProperty(headerKey, headerValue);

最后一个是可以修改的Azure SDK API和覆盖类中的方法createURLConnectionBaseRequest以实施访问。在GitHub上Azure存储SDK V2.2.0项目是 https://开头的github .COM /天青/ Azure的存储的Java /树/ V2.2.0 /

请注意:

公共静态HttpURLConnection的createURLConnection(最终URI URI,最终RequestOptions选项,UriQueryBuilder建设者,最终的OperationContext opContext, java.net.Proxy代理

public static HttpURLConnection createURLConnection(final URI uri, final RequestOptions options, UriQueryBuilder builder, final OperationContext opContext, java.net.Proxy proxy)

最后HttpURLConnection的retConnection =(HttpURLConnection类)resourceUrl.openConnection( 代理);

final HttpURLConnection retConnection = (HttpURLConnection) resourceUrl.openConnection(proxy);

public static HttpURLConnection createURLConnection(final URI uri, final RequestOptions options, UriQueryBuilder builder, final OperationContext opContext, java.net.Proxy proxy) throws IOException, URISyntaxException, StorageException {
    if (builder == null) {
        builder = new UriQueryBuilder();
    }

    final URL resourceUrl = builder.addToURI(uri).toURL();

    final HttpURLConnection retConnection = (HttpURLConnection) resourceUrl.openConnection(proxy);

    if (options.getTimeoutIntervalInMs() != null && options.getTimeoutIntervalInMs() != 0) {
        builder.add(TIMEOUT, String.valueOf(options.getTimeoutIntervalInMs() / 1000));
    }

    // Note: ReadTimeout must be explicitly set to avoid a bug in JDK 6.
    // In certain cases, this bug causes an immediate read timeout exception to be thrown even if ReadTimeout is not set.
    retConnection.setReadTimeout(Utility.getRemainingTimeout(options.getOperationExpiryTimeInMs(), options.getTimeoutIntervalInMs()));

    // Note : accept behavior, java by default sends Accept behavior as text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    retConnection.setRequestProperty(Constants.HeaderConstants.ACCEPT, Constants.HeaderConstants.XML_TYPE);
    retConnection.setRequestProperty(Constants.HeaderConstants.ACCEPT_CHARSET, Constants.UTF8_CHARSET);

    // Note : Content-Type behavior, java by default sends Content-type behavior as application/x-www-form-urlencoded for posts.
    retConnection.setRequestProperty(Constants.HeaderConstants.CONTENT_TYPE, Constants.EMPTY_STRING);

    retConnection.setRequestProperty(Constants.HeaderConstants.STORAGE_VERSION_HEADER,
        Constants.HeaderConstants.TARGET_STORAGE_VERSION);
    retConnection.setRequestProperty(Constants.HeaderConstants.USER_AGENT, getUserAgent());
    retConnection.setRequestProperty(Constants.HeaderConstants.CLIENT_REQUEST_ID_HEADER,
        opContext.getClientRequestID());

    return retConnection;
}

顺便说一句,你需要在每一个CloudXXXClient(CloudBlobClient等)级以上的方法来调用。

By the way, You need to call above method in every CloudXXXClient(CloudBlobClient, etc) Class.

这篇关于连接到Azure存储帐户直通代理服务器,微软Azure存储SDK的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆