Как загрузить динамический виртуальный жесткий диск в Azure, используя вызовы Python и остальных API? - PullRequest
0 голосов
/ 24 сентября 2018

https://github.com/Microsoft/azure-vhd-utils написано на Go.Add-AzureRMVhd - это команда powershell.Аналогично, есть ли альтернатива 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)

Является ли это правильным канонизированным ресурсом для загрузки блоба страницы?'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)

1 Ответ

0 голосов
/ 25 сентября 2018

существует ли альтернатива Python, которая загружает динамические файлы VHD

Мы используем 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)

Дополнительную информацию см. В официальном учебнике azure .

проверяет контрольную сумму?

Да, служба Azure Blob предоставляетмеханизмы обеспечения целостности данных как на прикладном, так и на транспортном уровнях.В этом посте будут подробно описаны эти механизмы с точки зрения сервиса и клиента. Проверка MD5 необязательна для операций PUT и GET.

Для получения дополнительной информации, пожалуйста, обратитесь к этому блогу .

...