【问题标题】:How to get Azure Storage account key programmatically using Java?如何使用 Java 以编程方式获取 Azure 存储帐户密钥?
【发布时间】:2016-12-07 21:21:09
【问题描述】:

我们如何使用 Java 获取 Azure 存储帐户访问密钥?需要什么所有细节才能做到这一点。

谢谢, 优先级

【问题讨论】:

    标签: java azure azure-storage azure-sdk


    【解决方案1】:

    要使用 java 获取存储帐户访问密钥,可以使用 Azure Rest API。提供了一个 java sdk,它可以让您轻松管理您的存储帐户。

    要获取访问密钥,您需要使用存储帐户所在的资源组名称和存储帐户名称。使用这些信息取回存储帐户后,名为“keys”的方法会返回访问密钥。

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

    Here 是一个完整的文档示例。

    问候

    【讨论】:

    • 谢谢蒂博。此示例似乎没有使用 Azure 提供的标准 java SDK。
    【解决方案2】:

    @Prit,您需要使用 Azure Storage Service Management SDK for Java 获取帐户密钥,请参阅以下步骤。

    1. 创建自签名证书并上传到Azure经典门户SETTINGS的标签MANAGEMENT CERTIFICATES,请参考blog

    我。使用 Java keytool 创建证书,请参见下面的命令。

    • keytool -genkeypair -alias mydomain -keyalg RSA -keystore WindowsAzureKeyStore.jks -keysize 2048 -storepass "test123";

    • keytool -v -export -file D:\WindowsAzureSMAPI.cer -keystore WindowsAzureKeyStore.jks -alias mydomain

    二。上传.cer 文件如下。

    您需要将这些依赖项添加到您的 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

    【讨论】:

    • 谢谢彼得。 Azure 建议现在使用 ARM 而不是证书,所以应该有一些不使用证书的方法。
    猜你喜欢
    • 2018-01-08
    • 2017-05-01
    • 2019-01-14
    • 2017-08-19
    • 1970-01-01
    • 2018-02-09
    • 2020-11-05
    • 1970-01-01
    • 2018-10-09
    相关资源
    最近更新 更多