简体   繁体   English

通过代理服务器连接到Azure存储帐户的Microsoft Azure Java存储SDK

[英]Connecting to Azure storage account thru proxy server Microsoft Azure Storage SDK for Java

In our project we need to access the Blob Storage through a Proxy Server (squid). 在我们的项目中,我们需要通过代理服务器(Squid)访问Blob存储。

We are planning to use the Microsoft Azure Storage SDK for Java version 2.2.0 . 我们计划使用Java版本2.2.0Microsoft Azure存储SDK But it looks like setting the proxy is not provided by the API. 但似乎设置代理不是由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");

But this affect all services that are running on my JVM which harms other services that not supposed to go via the Proxy. 但这会影响在我的JVM上运行的所有服务,这会损害不应通过代理传递的其他服务。

Looking at the java code it looks like com.microsoft.azure.storage.core.BaseRequest.createURLConnection(URI, RequestOptions, UriQueryBuilder, OperationContext). 看一下Java代码,它看起来像com.microsoft.azure.storage.core.BaseRequest.createURLConnection(URI,RequestOptions,UriQueryBuilder,OperationContext)。 Is calling java.net.URL.openConnection() without proxy. 正在调用没有代理的java.net.URL.openConnection()。 While using java.net.URL.openConnection(Proxy) could provide the required support? 在使用java.net.URL.openConnection(Proxy)时可以提供所需的支持吗?

It looks wired to me that this is not supported? 对我来说似乎不支持此功能?
Do I miss something here? 我在这里想念什么吗?

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. 更新:我在azure-storage-java git中打开了一个问题 ,我很乐意得到您的输入,因为我想为此提出一个请求。

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

Per my experience, there are two ways to help you implement the access function. 根据我的经验,有两种方法可以帮助您实现访问功能。

The one is that you can use Azure Storage REST API through the java.net.Proxy Class to access storage service. 一种是您可以通过java.net.Proxy类使用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);

The last one is that you can modify Azure SDK API and overwrite the method “createURLConnection” in Class “BaseRequest” to implement accessing. 最后一个是您可以修改Azure SDK API并覆盖类“ BaseRequest”中的方法“ createURLConnection”以实现访问。 The Azure Storage SDK v2.2.0 project on GitHub is https://github.com/Azure/azure-storage-java/tree/v2.2.0/ . GitHub上的Azure存储SDK v2.2.0项目为https://github.com/Azure/azure-storage-java/tree/v2.2.0/

Note: 注意:

public static HttpURLConnection createURLConnection(final URI uri, final RequestOptions options, UriQueryBuilder builder, final OperationContext opContext, java.net.Proxy proxy ) 公共静态HttpURLConnection createURLConnection(最终URI uri,最终的RequestOptions选项,UriQueryBuilder构建器,最终的OperationContext opContext, java.net.Proxy代理

and

final HttpURLConnection retConnection = (HttpURLConnection) resourceUrl.openConnection( proxy ); 最后的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;
}

By the way, You need to call above method in every CloudXXXClient(CloudBlobClient, etc) Class. 顺便说一句,您需要在每个CloudXXXClient(CloudBlobClient等)类中调用上述方法。

Following the issue-48 opened by me based on this question and additional one opened by strazh issue-65 , The proxy support was improved in version 4.2.0 see here . 在我根据此问题打开了issue-48和strazh issue-65打开了另一个问题之后 ,在4.2.0版本中改进了代理支持,请参见此处

Added support for setting a library-wide proxy. 添加了对设置库范围代理的支持。 The default proxy can be set on OperationContext. 可以在OperationContext上设置默认代理。

See the JUnits for full example https://github.com/Azure/azure-storage-java/blob/master/microsoft-azure-storage-test/src/com/microsoft/azure/storage/GenericTests.java 有关完整示例,请参见JUnits。https ://github.com/Azure/azure-storage-java/blob/master/microsoft-azure-storage-test/src/com/microsoft/azure/storage/GenericTests.java

Look for testDefaultProxy and testProxy 寻找testDefaultProxy和testProxy

Azure Storage team has released a new SDK (v10), where the Proxy is now supported through the HttpPipeline. Azure存储团队发布了一个新的SDK(v10),现在通过HttpPipeline支持该代理。 You can share the pipeline across all operations by passing it to StorageURL or just use in a single Blob by passing it to the BlobURL object. 您可以通过将管道传递给StorageURL来在所有操作之间共享管道,也可以通过将其传递给BlobURL对象在单个Blob中使用。

AnonymousCredentials creds = new AnonymousCredentials();

// Use PipelineOptions to define a retry strategy and a proxy - you can also pass your own HttpClient to this if you like
PipelineOptions po = new PipelineOptions();

// Proxy configuration shown here as a sample
HttpClientConfiguration configuration = new HttpClientConfiguration(
      new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 8888)), false); //pass true if the Proxy endpoint is HTTPS
po.client = HttpClient.createDefault(configuration);


// Create a URI with SAS token, and pass it to the PageBlobURL with an HttpPipeline created by createPipeline method
URL u = new URL("https://myaccount.blob.core.windows.net/mycontainer/myfile?sv=2017-04-17&sr=b&si=14169767-6354-41ed-a99b-c9db8dcc66bc&sig=8NUr%2BSpmRH%2BB2z%2FpQZDPDquTQ7rbgWfE9a6AePLlFT0%3D");
PageBlobURL blobURL = new PageBlobURL(u, PageBlobURL.createPipeline(creds, po));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过代理服务器指定proxyUser和proxyPassword Java的Microsoft Azure存储SDK - Specifying proxyUser and proxyPassword thru proxy server Microsoft Azure Storage SDK for Java Azure存储Java API:检查存储帐户是否支持安全传输或不使用Java SDK - Azure Storage Java API: Check if storage account support secure transfer or not using java SDK Azure 存储 - Java 代码中的代理设置 - Azure Storage - Proxy Setting in Java Code Blob 存储 java sdk 授权 azure AD - Blob storage java sdk authorize by azure AD 无法从 Azure 存储帐户中删除 blob - Azure Function (Java) - Not able to delete blob from Azure storage Account - Azure Function (Java) Azure SDK API和JAVA用法-列出存储帐户中的所有容器和BLOBS的问题 - Azure SDK API and JAVA usage - Issue Listing all Containers and BLOBS inside a storage account 使用Java SDK 0.9.7通过代理服务器连接到Azure服务总线 - Connecting to Azure service bus through a proxy server using Java SDK 0.9.7 如何使用Java在Azure文件存储帐户中写入CSV文件? - How to write CSV file on azure file storage account using java? Azure存储Java API:帐户不支持HTTP - Azure Storage Java API: Account does not support HTTP 使用代理获取Azure Blob存储值Java - Get Azure Blob Storage values Java using Proxy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM