2

我正在尝试将 Azure Storage Java 库与 Azure Government 端点一起使用。我的代码如下。

CloudStorageAccount account = CloudStorageAccount.parse(connectionString);

connectionString后缀为 Azure Gov cloud。由于某种原因,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。此时尚未添加存储端点。更新到新版本修复了它。

4

2 回答 2

1

您需要对azure-storage-java README.md中的代码示例进行的唯一修改是添加EndpointSuffixAzure Government endpoints 的连接字符串

这是修改后的示例 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);
        }
    }
}
于 2017-11-17T05:34:47.227 回答
0

我从官方页面找到了 C# 代码片段。

var credentials = new StorageCredentials(storageAccountName, storageAccountKey);

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

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

我在Java官方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();

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

希望它可以帮助你。

于 2017-11-16T03:10:36.677 回答