How can we get Azure Storage account access key using Java? What all details are required to get that.
Thanks, Prit
To get storage account access keys by using java, you can use Azure Rest API. A java sdk is available, it lets you manage easily your storage accounts.
To get access keys you need to use the resource Group name where you storage account is and the storage account name. Once you get back your storage account using those informations, a method called "keys" returns access keys.
List<StorageAccountKey> storageAccountKeys = storageAccount.keys();
Here is a complete documentation sample.
Regards
@Prit, You need to use Azure Storage Service Management SDK for Java to get account keys, please see the steps below.
MANAGEMENT CERTIFICATES
of SETTINGS
on Azure classic portal, please refer to the blog.I. Create a certificate using Java keytool, see the commands below.
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
II. Upload the .cer
file as below.
You need to add these dependencies into your pom.xml
file of maven project.
<!-- 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>
Here is my code for getting account keys.
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);
}
}
As reference, the related REST API is here.