【问题标题】:How to get or set x-ms-client-request-id or x-ms-request-id in Azure Storage SDK for Python如何在 Azure Storage SDK for Python 中获取或设置 x-ms-client-request-id 或 x-ms-request-id
【发布时间】:2018-09-04 20:04:28
【问题描述】:

有人知道如何在 Azure Storage SDK for Python 中获取或设置 x-ms-client-request-id(客户端请求 id)或 x-ms-request-id(请求 id)吗?

通过 SDK 执行对 Storage 的请求时,将 x-ms-client-request-id 和 x-ms-request-id 附加到标头中。如果打开存储分析,也会记录这些内容。我想通过使用 x-ms-client-request-id 或 x-ms-request-id 来确认请求是否正确进入存储。 我正在使用这些方法。

block_blob_service.put_block(container_name, blob_name, blob_stream, block_id)
block_blob_service.put_block_list(container_name, blob_name, block_ids)

我在网上搜索,但找不到示例代码。

如果是 C#,我知道我们可以在 OperationContext 属性中获取 x-ms-client-request-id。

https://docs.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.blob.cloudblockblob.uploadfromfileasync?view=azure-dotnet#Microsoft_WindowsAzure_Storage_Blob_CloudBlockBlob_UploadFromFileAsync_System_String_Microsoft_WindowsAzure_Storage_AccessCondition_Microsoft_WindowsAzure_Storage_Blob_BlobRequestOptions_Microsoft_WindowsAzure_Storage_OperationContext_System_IProgress_Microsoft_WindowsAzure_Storage_Core_Util_StorageProgress__System_Threading_CancellationToken_

【问题讨论】:

  • 嗨,现在有什么进展吗?

标签: python azure-storage azure-blob-storage


【解决方案1】:

我在source code 中搜索了函数:block_blob_service.put_block_list,但没有找到与x-ms-request-id 相关的任何参数。

def _put_block_list(
        self, container_name, blob_name, block_list, content_settings=None,
        metadata=None, validate_content=False, lease_id=None, if_modified_since=None,
        if_unmodified_since=None, if_match=None, if_none_match=None,
        timeout=None, encryption_data=None):
    '''
    See put_block_list for more details. This helper method
    allows for encryption or other such special behavior because
    it is safely handled by the library. These behaviors are
    prohibited in the public version of this function.
    :param str encryption_data:
        A JSON formatted string containing the encryption metadata generated for this 
        blob if it was encrypted all at once upon upload. This should only be passed
        in by internal methods.
    '''

    _validate_not_none('container_name', container_name)
    _validate_not_none('blob_name', blob_name)
    _validate_not_none('block_list', block_list)
    request = HTTPRequest()
    request.method = 'PUT'
    request.host_locations = self._get_host_locations()
    request.path = _get_path(container_name, blob_name)
    request.query = {
        'comp': 'blocklist',
        'timeout': _int_to_str(timeout),
    }
    request.headers = {
        'x-ms-lease-id': _to_str(lease_id),
        'If-Modified-Since': _datetime_to_utc_string(if_modified_since),
        'If-Unmodified-Since': _datetime_to_utc_string(if_unmodified_since),
        'If-Match': _to_str(if_match),
        'If-None-Match': _to_str(if_none_match),
    }
    _add_metadata_headers(metadata, request)
    if content_settings is not None:
        request.headers.update(content_settings._to_headers())
    request.body = _get_request_body(
        _convert_block_list_to_xml(block_list))

    if validate_content:
        computed_md5 = _get_content_md5(request.body)
        request.headers['Content-MD5'] = _to_str(computed_md5)

    if encryption_data is not None:
        request.headers['x-ms-meta-encryptiondata'] = encryption_data

    return self._perform_request(request, _parse_base_properties)

根据rest apix-ms-client-request-id 是可选的。所以我建议您采用解决方法,您可以将此参数添加到上述函数中。

if request_client_id is not None:
        request.headers['x-ms-client-request-id'] = _to_str(request_client_id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多