2016-07-15 76 views
2

我正在使用azure-sdk-for-python來創建和刪除虛擬機。如何使用azure-sdk-for-python刪除磁盤?

https://github.com/Azure/azure-sdk-for-python

http://azure-sdk-for-python.readthedocs.io/en/latest/

我已經成功地設法編寫代碼來創建和使用資源管理器方法(而不是經典)刪除我的虛擬機。

基本創建一個虛擬機可以在這裏看到: http://azure-sdk-for-python.readthedocs.io/en/latest/resourcemanagementcomputenetwork.html

我不擔心刪除資源組存儲賬戶,因爲我使用了相同的我所有的虛擬機。

要刪除創建的VM我有這樣的事情:

# 1. Delete the virtual machine 
result = compute_client.virtual_machines.delete(
    group_name, 
    vm_name    
) 
result.wait() 
# 2. Delete the network interface 
result = network_client.network_interfaces.delete(
    group_name, 
    network_interface_name 
) 
result.wait() 
# 3. Delete the ip 
result = network_client.public_ip_addresses.delete(
    group_name, 
    public_ip_address_name 
) 
result.wait() 

正如一些知道數據磁盤沒有與它的虛擬機一起被刪除。 我知道它可以與Azure的CLI來完成: https://azure.microsoft.com/en-us/documentation/articles/storage-azure-cli/

azure storage blob delete -a <storage_account_name> -k <storage_account_key> -q vhds <data_disk>.vhd 

但我不知道如何與Azure的SDK換蟒蛇做編程。我不想依賴Azure CLI,因爲我的代碼的其餘部分正在使用python sdk。

我將不勝感激一些如何做到這一點的幫助。

感謝

回答

1

這裏有一個小更多的代碼:

storage_account = <name of storage account> 

storage_client = StorageManagementClient(...) 

keys = storage_client.storage_accounts.list_keys(...) 

for key in keys.keys: 
    # Use the first key; adjust accordingly if your set up is different 
    break 

block_blob_service = BlockBlobService(
    account_name=storage_account, account_key=key.value) 

for blob in block_blob_service.list_blobs(container): 
    print blob.name 

我希望你發現這很有用。感謝Laurent的指點。

+1

感謝這個有用的代碼。一個注意:對於list_keys,appid需要配置爲資源組的「所有者」 – arved