2016-08-02 79 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

相关问题