使用Azure存储Java库尝试使用Azure政府端点时出错

时间:2017-11-15 17:21:43

标签: java azure azure-storage azure-storage-blobs azure-gov

我正在尝试将Azure存储Java库与Azure政府端点一起使用。我的代码如下。

CloudStorageAccount account = CloudStorageAccount.parse(connectionString);

connectionString具有Azure Gov云的后缀。由于某种原因,blob.storage URI的值仍标记为blob.core.windows.net,并且收到以下错误。我无法运行任何blob操作。

com.microsoft.azure.storage.StorageException: The server encountered an unknown failure: at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:178)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:214)
at com.microsoft.azure.storage.blob.CloudBlobContainer.exists(CloudBlobContainer.java:749)
at com.microsoft.azure.storage.blob.CloudBlobContainer.exists(CloudBlobContainer.java:736)
at com.microsoft.azure.storage.blob.CloudBlobContainer.exists(CloudBlobContainer.java:710)
at com.scalegrid.cloudconnector.azure.AzureStorageClient.createContainerIfItDoesntExist(AzureStorageClient.java:369)

java.net.UnknownHostException: XXXX.core.usgovcloudapi.net
ERROR ~ s failed.
Code:12207

有什么方法可以让它发挥作用吗?

更新

我使用的是早期版本的Azure存储Java。此时尚未添加存储端点。更新到更新的版本修复了它。

2 个答案:

答案 0 :(得分:1)

azure-storage-java README.md中代码示例所需的唯一修改是在Azure政府端点的连接字符串中添加EndpointSuffix

以下是修改后的示例Java代码:

import java.io.*;

import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;

public class BlobSample {
    public static final String storageConnectionString =
        "DefaultEndpointsProtocol=http;"
        + "AccountName=your_account_name;"
        + "AccountKey=your_account_key"
        + "EndpointSuffix=core.usgovcloudapi.net";

    public static void main(String[] args) {
        try {
            CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
            CloudBlobClient serviceClient = account.createCloudBlobClient();

            // Container name must be lower case.
            CloudBlobContainer container = serviceClient.getContainerReference("myimages");
            container.createIfNotExists();

            // Upload an image file.
            CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
            File sourceFile = new File("c:\\myimages\\image1.jpg");
            blob.upload(new FileInputStream(sourceFile), sourceFile.length());

            // Download the image file.
            File destinationFile = new File(sourceFile.getParentFile(), "image1Download.tmp");
            blob.downloadToFile(destinationFile.getAbsolutePath());
        }
        catch (FileNotFoundException fileNotFoundException) {
            System.out.print("FileNotFoundException encountered: ");
            System.out.println(fileNotFoundException.getMessage());
            System.exit(-1);
        }
        catch (StorageException storageException) {
            System.out.print("StorageException encountered: ");
            System.out.println(storageException.getMessage());
            System.exit(-1);
        }
        catch (Exception e) {
            System.out.print("Exception encountered: ");
            System.out.println(e.getMessage());
            System.exit(-1);
        }
    }
}

答案 1 :(得分:0)

我从official page找到了C#代码片段。

var credentials = new StorageCredentials(storageAccountName, storageAccountKey);

var storageAccount = new CloudStorageAccount(credentials, "core.usgovcloudapi.net", useHttps: true);  

因此,我认为您需要更改初始化客户端的方式。

我在Java official sdk中找到了上述方法。

CloudStorageAccount(StorageCredentials storageCredentials, boolean useHttps, String endpointSuffix)
Creates an instance of the CloudStorageAccount class using the specified account credentials.

因此,请参阅下面的示例代码修改您的代码。

 StorageCredentialsAccountAndKey storageCredentials =  new StorageCredentialsAccountAndKey(<your account name>, <your account key>);

CloudStorageAccount storageAccount  = new CloudStorageAccount(storageCredentials, true, "core.windows.net");

// Create the Azure Files client.
 CloudFileClient fileClient = storageAccount.createCloudFileClient();

请注意,您可以在门户网站的连接字符串末尾找到自己的gov Endpointsuffix参数。

希望它对你有所帮助。