gpt4 book ai didi

python-2.7 - 如何使用 python 和 REST API 调用将动态 VHD 上传到 Azure?

转载 作者:行者123 更新时间:2023-12-03 07:48:54 26 4
gpt4 key购买 nike

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)

最佳答案

is there a python alternative that uploads dynamic VHD files

我们使用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 offical tutorial .

does checksum verification?

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

更多信息请引用此blog .

关于python-2.7 - 如何使用 python 和 REST API 调用将动态 VHD 上传到 Azure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52476986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com