gpt4 book ai didi

java - 通过代理服务器连接到 Azure 存储帐户 Microsoft Azure Storage SDK for Java

转载 作者:行者123 更新时间:2023-11-30 08:10:19 25 4
gpt4 key购买 nike

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

我们计划使用 Microsoft Azure Storage SDK for Java version 2.2.0 。但API似乎没有提供设置代理。我可以让它通过代理的唯一方法是设置系统属性

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

但这会影响我的 JVM 上运行的所有服务,从而损害不应该通过代理的其他服务。

看一下java代码,它看起来像com.microsoft.azure.storage.core.BaseRequest.createURLConnection(URI、RequestOptions、UriQueryBuilder、OperationContext)。正在调用 java.net.URL.openConnection() 而不使用代理。使用 java.net.URL.openConnection(Proxy) 可以提供所需的支持吗?

我觉得这不受支持?
我在这里错过了什么吗?

更新:我打开了 issue关于 azure-storage-java git 中的这一点,我很高兴收到您的意见,因为我想为此建议一个拉取请求。

最佳答案

到目前为止,还没有 Java SDK API 支持通过代理服务器直接访问 Azure Storage,因为 BaseRequest 类在函数“public static HttpConnection createURLConnection(...)”中缺少“url.openConnection(proxy)”。

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

一是可以通过java.net.Proxy Class使用Azure Storage REST API来访问存储服务。

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,覆盖BaseRequest类中的“createURLConnection”方法来实现访问。 GitHub 上的 Azure 存储 SDK v2.2.0 项目是 https://github.com/Azure/azure-storage-java/tree/v2.2.0/ .

注意:

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

final HttpURLConnection retConnection = (HttpURLConnection) resourceUrl.openConnection(代理);

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等)类中调用上述方法。

关于java - 通过代理服务器连接到 Azure 存储帐户 Microsoft Azure Storage SDK for Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31776190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com