2016-08-02 75 views

回答

0

通過使用Java獲取存儲帳戶訪問鍵,你可以使用Azure的REST API。 Java sdk可用,它可以讓您輕鬆管理您的存儲帳戶。

要獲取訪問密鑰,您需要使用存儲帳戶所在的資源組名稱和存儲帳戶名稱。一旦您使用這些信息取回您的存儲帳戶,稱爲「密鑰」的方法會返回訪問密鑰。

List<StorageAccountKey> storageAccountKeys = storageAccount.keys(); 

Here是一個完整的文檔樣本。

問候

+0

謝謝Thibaut。此示例似乎並未使用Azure提供的標準Java SDK。 – Prit

0

@Prit,您需要使用Azure存儲服務管理SDK for Java來獲得帳號鍵,請參閱下面的步驟。

  1. 創建一個自簽名證書,並上傳在標籤的SETTINGSMANAGEMENT CERTIFICATES在Azure上經典的門戶網站,請參閱blog

I.使用Java keytool創建證書,請參閱下面的命令。

  • 密鑰工具-genkeypair -alias MYDOMAIN -keyalg RSA -keystore WindowsAzureKeyStore.jks -keysize 2048 -storepass 「test123」;

  • 密鑰工具-v -export -file d:\ WindowsAzureSMAPI.cer -keystore WindowsAzureKeyStore.jks -alias MYDOMAIN

II。如下所示上傳.cer文件。 enter image description here

您需要將這些依賴項添加到您的maven項目的pom.xml文件中。

<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-svc-mgmt --> 
<dependency> 
    <groupId>com.microsoft.azure</groupId> 
    <artifactId>azure-svc-mgmt</artifactId> 
    <version>0.9.3</version> 
</dependency> 
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-svc-mgmt-storage --> 
<dependency> 
    <groupId>com.microsoft.azure</groupId> 
    <artifactId>azure-svc-mgmt-storage</artifactId> 
    <version>0.9.3</version> 
</dependency> 

這是我的代碼獲取帳戶密鑰。

import org.xml.sax.SAXException; 

import com.microsoft.windowsazure.Configuration; 
import com.microsoft.windowsazure.core.utils.KeyStoreType; 
import com.microsoft.windowsazure.exception.ServiceException; 
import com.microsoft.windowsazure.management.configuration.ManagementConfiguration; 
import com.microsoft.windowsazure.management.storage.StorageManagementClient; 
import com.microsoft.windowsazure.management.storage.StorageManagementService; 
import com.microsoft.windowsazure.management.storage.models.StorageAccountGetKeysResponse; 

public class AccountKeys { 

    public static void main(String[] args) throws IOException, URISyntaxException, ServiceException, ParserConfigurationException, SAXException { 

     String uri = "https://management.core.windows.net/"; 
     String subscriptionId = "<subscription-id>"; 
     String keyStorePath = "<path>/WindowsAzureKeyStore.jks"; 
     String keyStorePassword = "test123"; 
     String storageName 

     Configuration config =  ManagementConfiguration.configure(
        new URI(uri), 
        subscriptionId, 
        keyStorePath, // the file path to the JKS 
        keyStorePassword, // the password for the JKS 
        KeyStoreType.jks // flags that I'm using a JKS keystore 
       ); 
     StorageManagementClient client = StorageManagementService.create(config); 
     StorageAccountGetKeysResponse response = client.getStorageAccountsOperations().getKeys(storageName); 
     String pk = response.getPrimaryKey(); 
     String sk = response.getSecondaryKey(); 
     System.out.println(pk); 
     System.out.println(sk); 
    } 
} 

作爲參考,相關的REST API是here

+0

謝謝彼得。 Azure建議現在使用ARM而不是證書,因此應該有某種方法而不使用證書。 – Prit

相關問題