2

I am trying to use the Azure Storage Java library with the Azure Government endpoints. My code is as follows.

CloudStorageAccount account = CloudStorageAccount.parse(connectionString);

connectionString has the suffix of Azure Gov cloud. For some reason the values for the blob.storage URI are still marked as blob.core.windows.net and I receive the following error. I am unable to run any blob operations.

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

Is there any way I can get this to work ?

Update

I was using an earlier version of the Azure storage Java. Storage endpoints have not been added at this point. Updating to the newer version fixed it.

nwarriorch
  • 337
  • 6
  • 16
  • Please edit your question and include the connection string you're using? Please replace account name/key with random values before sharing. – Gaurav Mantri Nov 16 '17 at 02:38
  • Any progress now? – Jay Gong Nov 20 '17 at 01:35
  • yes, I forgot updating this ticket. I was using an earlier version of the Azure storage SDK and they did not add the gov cloud endpoints at that time. updating it to the newer version fixed it. – nwarriorch Nov 21 '17 at 18:53

2 Answers2

1

The only modification you need from the Code Sample in the azure-storage-java README.md is to add EndpointSuffix in your connection string for the Azure Government endpoints.

Here's the modified sample Java code:

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);
        }
    }
}
Saca
  • 10,355
  • 1
  • 34
  • 47
0

I found the snippet of C# code from official page.

var credentials = new StorageCredentials(storageAccountName, storageAccountKey);

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

So , I think you need to change the way you initialize the client.

I found the above method in the Java official sdk.

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

So , please refer to the sample code below to modify your code.

 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();

Please note that you can find your own gov Endpointsuffix parameters at the end of the connectstring on portal.

Hope it helps you.

Jay Gong
  • 23,163
  • 2
  • 27
  • 32