【问题标题】:How to upload dynamic VHD to Azure using python and rest API calls?如何使用 python 和 rest API 调用将动态 VHD 上传到 Azure?
【发布时间】:2018-09-24 10:07:51
【问题描述】:

https://github.com/Microsoft/azure-vhd-utils 是用 Go 编写的。 Add-AzureRMVhd 是 powershell cmd。 同样,是否有 python 替代方法可以上传动态 VHD 文件并进行校验和验证?

    #Working code to list blobs using GET API:
    import requests
    import datetime
    import hmac
    import hashlib
    import base64

    storage_account_name = 'abcd'
    storage_account_key = '4********************************************$'
    api_version = '2018-03-28'
    request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

    string_params = {
            'verb': 'GET',
            'Content-Encoding': '',
            'Content-Language': '',
            'Content-Length': '',
            'Content-MD5': '',
            'Content-Type': '',
            'Date': '',
            'If-Modified-Since': '',
            'If-Match': '',
            'If-None-Match': '',
            'If-Unmodified-Since': '',
            'Range': '',
            'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
        'CanonicalizedResource': '/' + storage_account_name + '/containername\ncomp:list\nrestype:container'
    }

    string_to_sign = (string_params['verb'] + '\n' 
                                        + string_params['Content-Encoding'] + '\n'
                                        + string_params['Content-Language'] + '\n'
                                        + string_params['Content-Length'] + '\n'
                                        + string_params['Content-MD5'] + '\n' 
                                        + string_params['Content-Type'] + '\n' 
                                        + string_params['Date'] + '\n' 
                                        + string_params['If-Modified-Since'] + '\n'
                                        + string_params['If-Match'] + '\n'
                                        + string_params['If-None-Match'] + '\n'
                                        + string_params['If-Unmodified-Since'] + '\n'
                                        + string_params['Range'] + '\n'
                                        + string_params['CanonicalizedHeaders']
                                        + string_params['CanonicalizedResource'])

    signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

    headers = {
            'x-ms-date' : request_time,
            'x-ms-version' : api_version,
            'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
    }



    url = ('https://' + storage_account_name + '.blob.core.windows.net/containername?restype=container&comp=list')

    r = requests.get(url, headers = headers)

    print(r.content)

这是上传页面 blob 的正确规范化资源吗? 'CanonicalizedResource': '/' + storage_account_name + '/containername/vhdname.vhd'

#Failing PUT request to upload page blob
import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'abc'
storage_account_key = '4*******************************='
api_version = '2018-03-28'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

string_params = {
        'verb': 'PUT',
        'Content-Encoding': '',
        'Content-Language': '',
        'Content-Length': '',
        'Content-MD5': '',
        'Content-Type': '',
        'Date': '',
        'If-Modified-Since': '',
        'If-Match': '',
        'If-None-Match': '',
        'If-Unmodified-Since': '',
        'Range': '',
        'CanonicalizedHeaders': 'x-ms-blob-type:PageBlob' + '\nx-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
        'CanonicalizedResource': '/' + storage_account_name + '/containername/vhdname.vhd'
}

string_to_sign = (string_params['verb'] + '\n' 
                                    + string_params['Content-Encoding'] + '\n'
                                    + string_params['Content-Language'] + '\n'
                                    + string_params['Content-Length'] + '\n'
                                    + string_params['Content-MD5'] + '\n' 
                                    + string_params['Content-Type'] + '\n' 
                                    + string_params['Date'] + '\n' 
                                    + string_params['If-Modified-Since'] + '\n'
                                    + string_params['If-Match'] + '\n'
                                    + string_params['If-None-Match'] + '\n'
                                    + string_params['If-Unmodified-Since'] + '\n'
                                    + string_params['Range'] + '\n'
                                    + string_params['CanonicalizedHeaders']
                                    + string_params['CanonicalizedResource'])

signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

headers = {
        'x-ms-date' : request_time,
        'x-ms-version' : api_version,
        'Content-Length' : '0',
        'x-ms-blob-type': 'PageBlob',
        'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}

url = ('https://' + storage_account_name + '.blob.core.windows.net/containername/vhdname.vhd')
r = requests.get(url, headers = headers)

print(r.content)

【问题讨论】:

    标签: python-2.7 api azure python-requests vhd


    【解决方案1】:

    有没有可以上传动态 VHD 文件的 python 替代方法

    我们使用Azure python sdk 将 VHD 文件上传到 Azure 存储。

    block_blob_service = BlockBlobService(account_name='accountname', account_key='accountkey') 
    block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file)
    

    更多信息请参考azureoffical tutorial

    校验和验证吗?

    是的,Azure Blob 服务提供了确保应用程序层和传输层数据完整性的机制。这篇文章将从服务和客户端的角度详细介绍这些机制。 MD5 检查 在 PUT 和 GET 操作中都是可选的。

    更多信息,请参考这个blog

    【讨论】:

    • 感谢您的回复!曾尝试过 block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file),但是它无法上传动态 vhd,即无法正确展开图像。将不胜感激微软 vhd 页面 blob 上的示例“发布”rest API,因为它无法正常运行。
    • however it fails to upload dynamic vhds ie wont expand the image correctly 有错误信息吗?如果可以使用az cli 命令,可以使用az storage blob upload --account-name mystorageaccount \ --account-key key1 \ --container-name mydisks \ --type page \ --file /path/to/disk/mydisk.vhd \ --name myDisk.vhd 我们也可以使用python 调用az 命令,更多信息请参考另一个SO thread
    • 感谢您的回复!如文档中所述,Azure 不支持通过 python sdk 上传动态 vhd 文件(仅允许静态)..因此我想在我的 python 脚本中使用 rest 调用(curl cmds)来实现相同的功能。如果没有 Microsoft Azure 存储模拟器,是否可以这样做?参考docs.microsoft.com/en-us/rest/api/storageservices/put-page
    • 是的,其余 API 可用于 Azue 存储帐户。
    • 任何用于上传 vhd 页面 blob(PUT 请求)的 API 调用输入将不胜感激!
    猜你喜欢
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 2013-09-21
    • 2021-10-08
    • 2016-10-07
    相关资源
    最近更新 更多