Java 的 Azure SDK - 抛出 InvalidKeyException 的示例程序

Azure SDK for Java - sample program throwing InvalidKeyException

使用 Java 的 Azure 存储 SDK,我正在尝试在 Azure Table 存储上执行基本的创建、读取、更新和删除操作,如下面 link 所示: https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-table-storage/

创建 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());
        }
    }
}

代码看起来相当简单易懂。它将连接到 Azure table 存储,如果具有给定名称的 table 不存在,它将创建它。但是我收到了一个 InvalidKeyException(下面粘贴了完整的异常)。

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)

令我惊讶的是,使用 Azure 存储的人很少遇到此问题。我尝试使用连接字符串中的编码密钥对存储密钥进行编码,但仍然没有用。

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

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

谁能帮我解决这个问题?我在 google 中搜索了很多,我发现一位用户在 discus 上提出了类似的问题,但没有为此提供答案,或者更确切地说,答案没有帮助。

请更改以下代码行:

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

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

基本上在您的代码中,AccountNameAccountKeyEndpointSuffix 之间没有分隔符 (;)。此外,如果您连接到标准端点 (core.windows.net),则无需在连接字符串中指定 EndpointSuffix

最后,请确保账号密码正确。

更新:/问题的解决

首先我确保连接字符串中的所有属性都用';'分隔正如 Gaurav 所建议的(下)

原来我必须在我的程序中手动设置代理设置,因为我公司的工作机器正在使用代理连接到互联网。

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

更新代理设置解决了我的问题。