簡體   English   中英

通過代理服務器連接到Azure存儲帳戶的Microsoft Azure Java存儲SDK

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

在我們的項目中,我們需要通過代理服務器(Squid)訪問Blob存儲。

我們計划使用Java版本2.2.0Microsoft Azure存儲SDK 但似乎設置代理不是由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)時可以提供所需的支持嗎?

對我來說似乎不支持此功能?
我在這里想念什么嗎?

更新:我在azure-storage-java git中打開了一個問題 ,我很樂意得到您的輸入,因為我想為此提出一個請求。

到目前為止,還沒有Java SDK API支持通過代理服務器直接訪問Azure存儲,因為BaseRequest類在函數“ public static HttpConnection createURLConnection(...)”中缺少“ url.openConnection(proxy)”。

根據我的經驗,有兩種方法可以幫助您實現訪問功能。

一種是您可以通過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);

最后一個是您可以修改Azure SDK API並覆蓋類“ BaseRequest”中的方法“ createURLConnection”以實現訪問。 GitHub上的Azure存儲SDK v2.2.0項目為https://github.com/Azure/azure-storage-java/tree/v2.2.0/

注意:

公共靜態HttpURLConnection createURLConnection(最終URI uri,最終的RequestOptions選項,UriQueryBuilder構建器,最終的OperationContext opContext, java.net.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;
}

順便說一句,您需要在每個CloudXXXClient(CloudBlobClient等)類中調用上述方法。

在我根據此問題打開了issue-48和strazh issue-65打開了另一個問題之后 ,在4.2.0版本中改進了代理支持,請參見此處

添加了對設置庫范圍代理的支持。 可以在OperationContext上設置默認代理。

有關完整示例,請參見JUnits。https ://github.com/Azure/azure-storage-java/blob/master/microsoft-azure-storage-test/src/com/microsoft/azure/storage/GenericTests.java

尋找testDefaultProxy和testProxy

Azure存儲團隊發布了一個新的SDK(v10),現在通過HttpPipeline支持該代理。 您可以通過將管道傳遞給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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM