user2123507
user2123507

Reputation: 55

Azure SDK for Java - sample program throwing InvalidKeyException

Using Azure Storage SDK for Java, I am trying to perform basic create, read, update, delete operations on Azure Table Storage as given in the link below: https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-table-storage/

Sample program for creating a table:

package com.azure.test;
import java.io.UnsupportedEncodingException;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.table.CloudTable;
import com.microsoft.azure.storage.table.CloudTableClient;
import com.microsoft.windowsazure.core.utils.Base64;

public class App 
{

    public static void main( String[] args ) throws StorageException,    UnsupportedEncodingException
{

    String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=accountname;" + 
            "AccountKey=storagekey;"+
           "EndpointSuffix=table.core.windows.net"; 

    try
    {
        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount =
           CloudStorageAccount.parse(storageConnectionString);

        CloudTableClient tableClient =               storageAccount.createCloudTableClient();

      //Create the table if it doesn't exist.
       String tableName = "MyTable";
       CloudTable cloudTable = tableClient.getTableReference(tableName);
       cloudTable.createIfNotExists();               

    }
    catch (Exception e)
    {
        // Output the stack trace.
        e.printStackTrace();
        System.out.println(e.getMessage());
        }
    }
}

The code seems to be fairly simple to understand. It would connect to the Azure table storage and if a table with a given name does not exist it will create it. But I am getting a InvalidKeyException(full exception pasted below).

java.security.InvalidKeyException: Storage Key is not a valid base64 encoded string.
at com.microsoft.azure.storage.StorageCredentials.tryParseCredentials(StorageCredentials.java:68)
at com.microsoft.azure.storage.CloudStorageAccount.tryConfigureServiceAccount(CloudStorageAccount.java:408)
at com.microsoft.azure.storage.CloudStorageAccount.parse(CloudStorageAccount.java:259)
at com.azure.test.App.main(App.java:71)

I am surprised that not many people using Azure Storage are facing this issue. I tried to encode the storage key using and used the encoded key in the connection string but still no use.

String encodedKey=Base64.encode(storageKey.getBytes())

String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=accountname" + 
            "AccountKey="+encodedKey+
           "EndpointSuffix=table.core.windows.net;"; 

Can anyone please help me with this? I searched in google a lot and I am able to find one user raised a similar issue on discus but there is no answer provided for that or rather that answer was not helpful.

Upvotes: 1

Views: 1407

Answers (2)

user2123507
user2123507

Reputation: 55

Update:/Resolution of the issue

First of all I ensured that all the properties in connection string are separated by ';' as suggested by Gaurav(below)

It turns out that I have to manually set the proxy settings in my program since my company work machine is using a proxy to connect to the internet.

System.getProperties().put("http.proxyHost", "myproxyHost");
System.getProperties().put("http.proxyPort", "myProxyPort");
System.getProperties().put("http.proxyUser", "myProxyUser");
System.getProperties().put("http.proxyPassword","myProxyPassword");

Updating the proxy settings solved the issue for me.

Upvotes: 3

Gaurav Mantri
Gaurav Mantri

Reputation: 136266

Please change the following line of code:

String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=accountname" + 
            "AccountKey="+encodedKey+
           "EndpointSuffix=table.core.windows.net;"; 

To

String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=accountname" + 
            ";AccountKey="+encodedKey+
           ";EndpointSuffix=core.windows.net;"; 

Essentially in your code, there was no separator (;) between AccountName, AccountKey and EndpointSuffix. Also, if you're connecting to standard endpoint (core.windows.net), you don't need to specify EndpointSuffix in your connection string.

Lastly, please ensure that the account key is correct.

Upvotes: 1

Related Questions