【问题标题】:upload a page blob to azure using REST API python使用 REST API python 将页面 blob 上传到 azure
【发布时间】:2013-11-17 13:50:20
【问题描述】:

请我如何上传带有类型页面的 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=0
sent = 0
block_ids = []
block_ids = []
index = 0
with 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:
            break

blob_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)] 

【问题讨论】:

    标签: python linux azure storage blob


    【解决方案1】:

    您必须在x-ms-blob-content-length 请求标头中传递正确的值,并且基于您上述问题中的标头 sn-p,您将其传递为 None

    我注意到的另一件事是您在页面上传后调用put_block_list 方法。请注意,页 blob 不需要此操作。只要put_page 操作成功完成,您的数据就会被提交。 Block Blobs 需要 put_block_list 方法。

    我还注意到您的 index 变量总是递增 1。它不应该递增 PAGE_SIZE 吗?现在的情况是,您的 x_range 变量都搞砸了。假设您的 PAGE_SIZE 变量是 1024,这就是 x_range 变量的样子:

    迭代# |索引 | x_range

    0 | 0 | 0-1023

    1 | 1 | 1-1024

    2 | 2 | 2-1025

    您可能也想调查一下。

    ===============================================

    更新

    所以我想我知道你为什么会遇到这个问题。基本上你的这行代码导致了问题:

    blob_service.put_page(container_name, blob_name, data, x_ms_range = x_range,x_ms_page_write = 'clear')
    

    如果您注意到,您将 x_ms_page_write 的值传递为 clear。将此值更改为update,您的代码应该可以正常工作。根据Put Page操作的文档,当您将clear指定为x_ms_page_write的值时,内容长度应为0。无论如何,由于您正在上传页面blob,因此此标头的值应为@ 987654340@.

    这是我编写的代码。在这里,我将 10 MB VHD 上传到 blob 存储:

    import sys
    from 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:
                    break
    
    blob_service = BlobService(account_name="myaccountname", account_key="myaccountkey")
    myupload (blob_service, 'mycontainer', 'test.vhd', 'D:\\test.vhd')
    

    【讨论】:

    • 在您发表评论之前我已经修改了我的脚本,但感谢您的帮助,azure 文档中提到的 PAGE_SIZE 必须为 512 字节!我不知道为什么..但我读过。
    • 这是修改后的输出:InvalidHeaderValue值为一HTTP 标头的格式不正确。 RequestId:8f2ae87d-46e3-4d60-b884-fee92f9b0803 时间:2013-11-17T17:07:37.9286660ZContent-Length512
    • 你的 PAGE_SIZE 变量的值是多少?每个页面大小应为 512 字节的倍数。
    猜你喜欢
    • 2013-09-21
    • 2021-10-08
    • 2013-10-30
    • 2012-02-18
    • 2016-10-07
    • 2017-07-10
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    相关资源
    最近更新 更多