且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

连接到Azure存储帐户直通代理服务器,微软Azure存储SDK的Java

更新时间:2023-01-15 12:47:32

到目前为止,已经通过代理服务器没有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.