请如何上传带有类型页面的Blob以使该页面为VHD文件“大内容”,请遵循以下功能:def upload(blob_service, container_name, blob_name, file_path):blob_service.create_container(container_name, None, None, False)blob_service.put_blob(container_name, blob_name, '', "PageBlob")data_sent=0sent = 0block_ids = []block_ids = []index = 0with open(file_path, 'rb') as f: while True: data = f.read(PAGE_SIZE) if data: length = len(data) #block_id = base64.b64encode(str(index)) x_range = 'bytes={}-{}'.format(index, index + pageSize - 1) blob_service.put_page(container_name, blob_name, data, x_ms_range = x_range,x_ms_page_write = 'clear') block_ids.append(block_id) index += 1 data_sent += PAGE_SIZE sent = data_sent/(1024*1024) sys.stdout.write("\rUploaded data = %d MB"%sent) sys.stdout.flush() else: breakblob_service.put_block_list(container_name, blob_name, block_ids)错误是: Traceback (most recent call last): File "ehcpazurenew.py", line 331, in <module> upload(blob_service,container_name,blob_name,file_path) File "ehcpazurenew.py", line 250, in upload blob_service.put_blob(container_name, blob_name, '', "PageBlob") File "/home/ahmed/Desktop/azure/storage/blobservice.py", line 486, in put_blob response = self._perform_request(request) File "/home/ahmed/Desktop/azure/storage/storageclient.py", line 145, in _perform_request _storage_error_handler(e) File "/home/ahmed/Desktop/azure/storage/__init__.py", line 757, in _storage_error_handler return _general_error_handler(http_error) File "/home/ahmed/Desktop/azure/__init__.py", line 649, in _general_error_handler raise WindowsAzureError(_ERROR_UNKNOWN % http_error.message + '\n' + http_error.respbody) azure.WindowsAzureError: Unknown error (An HTTP header that's mandatory for this request is not specified.) <?xml version="1.0" encoding="utf-8"?><Error><Code>MissingRequiredHeader</Code> <Message>An HTTP header that's mandatory for this request is not specified. RequestId:5a839a6d-2a0f-4559-bc6d-e3b2cccf84f5 Time:2013-11-17T13:12:03.8206435Z</Message><HeaderName>x-ms-blob-content-length</HeaderName></Error>但是HTTP标头是正确的: [('x-ms-blob-type', 'PageBlob'), ('Content-Encoding', None), ('Content-Language', None), ('Content-MD5', None), ('Cache-Control', None), ('x-ms-blob-content-type', None), ('x-ms-blob-content-encoding', None), ('x-ms-blob-content-language', None), ('x-ms-blob-content-md5', None), ('x-ms-blob-cache-control', None), ('x-ms-meta-name-values', None), ('x-ms-lease-id', None), ('x-ms-blob-content-length', None), ('x-ms-blob-sequence-number', None)] 最佳答案 您必须在x-ms-blob-content-length请求标头中传递适当的值,并根据上述问题中的标头代码段,将其作为None传递。我注意到的另一件事是,页面上传后正在调用put_block_list方法。请注意,页面Blob不需要此操作。一旦put_page操作成功完成,您的数据就会被提交。 put_block_list需要Block Blobs方法。我还注意到,您的index变量始终会增加1。现在,您的PAGE_SIZE变量都搞砸了。假设您的x_range变量是PAGE_SIZE,则x_range变量将是这样的:迭代号|索引| x_range0 | 0 | 0-10231 | 1 | 1-10242 | 2 | 2-1025您可能还想研究一下。==============================================更新所以我想我知道您为什么遇到这个问题。基本上您的这一行代码会引起问题:blob_service.put_page(container_name, blob_name, data, x_ms_range = x_range,x_ms_page_write = 'clear')如果您注意到,您正在将1024的值传递为x_ms_page_write。将此值更改为clear,您的代码应该可以正常工作。根据update操作的文档,当将Put Page指定为clear的值时,内容长度应为0。在任何情况下,由于要上传页面Blob,因此此标头的值应为。这是我编写的代码。在这里,我将10 MB VHD上载到Blob存储中:import sysfrom azure.storage import *def myupload(blob_service, container_name, blob_name, file_path): blob_service.create_container(container_name, None, None, False) blob_service.put_blob(container_name, blob_name, '', 'PageBlob',x_ms_blob_content_length=10486272) data_sent=0 sent = 0 block_ids = [] index = 0 PAGE_SIZE = 512 pageSize = 512 with open(file_path, 'rb') as f: while True: data = f.read(pageSize) if data: length = len(data) #sys.stdout.write("\rlength = %d "%length) #sys.stdout.flush() x_range = 'bytes={}-{}'.format(index, index + pageSize - 1) sys.stdout.write("\rx_range = %s "%x_range) #sys.stdout.flush() blob_service.put_page(container_name, blob_name, data, x_ms_range = x_range, x_ms_page_write = 'update') index += PAGE_SIZE data_sent += PAGE_SIZE sent = data_sent/(1024*1024) #sys.stdout.write("\rUploaded data = %d MB"%sent) #sys.stdout.flush() else: breakblob_service = BlobService(account_name="myaccountname", account_key="myaccountkey")myupload (blob_service, 'mycontainer', 'test.vhd', 'D:\\test.vhd')关于python - 使用REST API python将页面Blob上传到Azure,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20031449/
10-16 01:42