Azure Python下载存储blob返回';不满足使用HTTP条件标头指定的条件。';

Azure Python下载存储blob返回';不满足使用HTTP条件标头指定的条件。';,python,azure,azure-storage-blobs,Python,Azure,Azure Storage Blobs,使用以下代码: import os from azure.identity import ( ClientSecretCredential ) # Import the client object from the Azure library from azure.storage.blob import BlobClient t_id = "<tenant_id>" c_id = "<client_id>" s_acc

使用以下代码:


import os
from azure.identity import (
    ClientSecretCredential
)

# Import the client object from the Azure library
from azure.storage.blob import BlobClient

t_id = "<tenant_id>"
c_id = "<client_id>"
s_acct = "<storage_account_name>"
s_acct_url = "%s.blob.core.windows.net" % s_acct
sek = "<client_sekret>"

print("+ Setup credentials.")
credential = ClientSecretCredential(t_id, c_id, sek)
print("+ Setup Blob Client")
bobc = BlobClient(s_acct_url, <container_name>, <blob_name>,
                  credential=credential)
print("+ Setup streamer")
ssd = bobc.download_blob()
print("+ Get properties")
print(ssd.get_blob_properties())

导入操作系统
从azure.identity导入(
客户端加密证书
)
#从Azure库导入客户端对象
从azure.storage.blob导入BlobClient
t_id=“”
c_id=“”
s_acct=“”
s_acct_url=“%s.blob.core.windows.net”%s_acct
瑞典克朗=“”
打印(“+设置凭据”)
凭证=客户端安全凭证(t\U id、c\U id、sek)
打印(“+设置Blob客户端”)
bobc=BlobClient(s_账户_url,
凭证=凭证)
打印(“+设置拖缆”)
ssd=bobc.download_blob()
打印(“+获取属性”)
打印(ssd.get\u blob\u properties())
但我得到了以下错误:

$ python azdown.py
+ Setting up stream
+
+ Download stream:
+    Size: 136365212160
Traceback (most recent call last):
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 192, in _download_chunk
    _, response = self.client.download(
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_generated\operations\_blob_operations.py", line 179, in download
    raise models.StorageErrorException(response, self._deserialize)
azure.storage.blob._generated.models._models_py3.StorageErrorException: Operation returned an invalid status 'The condition specified using HTTP conditional header(s) is not met.'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "azdown.py", line 29, in <module>
    download_stream.download_to_stream(my_blob)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 579, in download_to_stream
    self.readinto(stream)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 561, in readinto
    downloader.process_chunk(chunk)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 125, in process_chunk
    chunk_data = self._download_chunk(chunk_start, chunk_end - 1)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 201, in _download_chunk
    process_storage_error(error)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_shared\response_handlers.py", line 147, in process_storage_error
    raise error
azure.core.exceptions.ResourceModifiedError: The condition specified using HTTP conditional header(s) is not met.
RequestId:<request id>
Time:2021-01-10T01:23:24.8981731Z
ErrorCode:ConditionNotMet
Error:None
$python azdown.py
+设置流
+
+下载流:
+尺寸:136365212160
回溯(最近一次呼叫最后一次):
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第192行,在下载块中
_,response=self.client.download(
文件“C:\git\python38\lib\site packages\azure\storage\blob\\生成的\operations\\ blob\\操作.py”,第179行,下载
raise models.StorageErrorException(响应,自。\u反序列化)
azure.storage.blob.\u生成的.models.\u models\u py3.StorageErrorException:操作返回无效状态“不满足使用HTTP条件标头指定的条件”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“azdown.py”,第29行,在
下载流。下载到流(我的blob)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第579行,在下载到流中
self.readinto(流)
readinto中第561行的文件“C:\git\python38\lib\site packages\azure\storage\blob\\ u download.py”
downloader.process_块(chunk)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第125行,进程中
chunk\u data=self.\u下载\u chunk(chunk\u开始,chunk\u结束-1)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第201行,在下载块中
进程存储错误(错误)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\u shared\response\u handlers.py”,第147行,进程中存储错误
提出错误
azure.core.exceptions.ResourceModifierRor:不满足使用HTTP条件标头指定的条件。
请求ID:
时间:2021-01-10T01:23:24.8981731Z
错误代码:ConditionNotMet
错误:无
我查看了[1],但我正在使用download_to_stream()。然后我尝试了[2],但我的存储是gen1容器。有一个链接表明它与容器的网络权限有关,但容器设置为允许所有人下载。问题是,它确实下载到某一点(atm,120G中的1.2G)所以这不是一个许可问题,而是一种竞赛条件

[1] -

[2] -

[编辑]

我还尝试使用连接字符串方法:

credential = DefaultAzureCredential()

    blob_url = "DefaultEndpointsProtocol=https;AccountName=<storage_acct>;" + \
               "AccountKey=<account_key>;" + \
               "EndpointSuffix=core.windows.net"

    # Create the client object using the storage URL and the credential
    blob_client = BlobClient.from_connection_string(
        blob_url,
        container_name=<container_name>,
        blob_name=<blob_item>)

    if download_now:
        with open(<blob_item>, "wb") as my_blob:
            print("+ Setting up stream")
            download_stream = blob_client.download_blob()
            print("+")
            print("+ Download stream:")
            print("+    Size: %d" % download_stream.size)
            download_stream.download_to_stream(my_blob)

    else:
        print(blob_client.get_blob_properties())
credential=DefaultAzureCredential()
blob_url=“DefaultEndpointsProtocol=https;AccountName=;”+\
“AccountKey=;”+\
“EndpointSuffix=core.windows.net”
#使用存储URL和凭据创建客户端对象
blob\u client=BlobClient.from\u connection\u字符串(
blob_url,
容器名称=,
blob_name=)
如果现在下载(u):
以open(,“wb”)作为my_blob:
打印(“+设置流”)
下载\u stream=blob\u客户端。下载\u blob()
打印(“+”)
打印(“+下载流:”)
打印(“+大小:%d”%download\u stream.Size)
下载流。下载到流(我的blob)
其他:
打印(blob_客户端。获取blob_属性())
我得到了同样的错误,即使在它已经流了一个吉格左右

错误:

Traceback (most recent call last):
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 192, in _download_chunk
    _, response = self.client.download(
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_generated\operations\_blob_operations.py", line 179, in download
    raise models.StorageErrorException(response, self._deserialize)
azure.storage.blob._generated.models._models_py3.StorageErrorException: Operation returned an invalid status 'The condition specified using HTTP conditional header(s) is not met.'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "azuredl.py", line 52, in <module>
    download_stream.download_to_stream(my_blob)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 579, in download_to_stream
    self.readinto(stream)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 561, in readinto
    downloader.process_chunk(chunk)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 125, in process_chunk
    chunk_data = self._download_chunk(chunk_start, chunk_end - 1)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_download.py", line 201, in _download_chunk
    process_storage_error(error)
  File "C:\git\python38\lib\site-packages\azure\storage\blob\_shared\response_handlers.py", line 147, in process_storage_error
    raise error
azure.core.exceptions.ResourceModifiedError: The condition specified using HTTP conditional header(s) is not met.
RequestId:<request id>
Time:2021-01-10T07:43:24.4927426Z
ErrorCode:ConditionNotMet
Error:None

回溯(最近一次呼叫最后一次):
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第192行,在下载块中
_,response=self.client.download(
文件“C:\git\python38\lib\site packages\azure\storage\blob\\生成的\operations\\ blob\\操作.py”,第179行,下载
raise models.StorageErrorException(响应,自。\u反序列化)
azure.storage.blob.\u生成的.models.\u models\u py3.StorageErrorException:操作返回无效状态“不满足使用HTTP条件标头指定的条件”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“azuredl.py”,第52行,在
下载流。下载到流(我的blob)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第579行,在下载到流中
self.readinto(流)
readinto中第561行的文件“C:\git\python38\lib\site packages\azure\storage\blob\\ u download.py”
downloader.process_块(chunk)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第125行,进程中
chunk\u data=self.\u下载\u chunk(chunk\u开始,chunk\u结束-1)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\下载.py”,第201行,在下载块中
进程存储错误(错误)
文件“C:\git\python38\lib\site packages\azure\storage\blob\\u shared\response\u handlers.py”,第147行,进程中存储错误
提出错误
azure.core.exceptions.ResourceModifierRor:不满足使用HTTP条件标头指定的条件。
请求ID:
时间:2021-01-10T07:43:24.4927426Z
错误代码:ConditionNotMet
错误:无

有人有什么想法吗?

当我对正在下载的文件进行一些更新(如更改内容或添加一些元数据)时,我可以重新编写此问题:

此问题是由于blob更新后,其Etag会同时更改。这会导致此问题。详细信息请参阅

为了解决这个问题,您可以要求为这个blob添加一个锁,这样在downloa时这个blob就不会被编辑或删除
blob_client = BlobClient.from_connection_string(conn_str='', container_name='',blob_name='')

#require lease that never expires
lease = blob_client.acquire_lease(lease_duration=-1)

with open("<some path>", "wb") as my_blob:
            print("+ Setting up stream")
            download_stream = blob_client.download_blob()
            print("+")
            print("+ Download stream:")
            print("+    Size: %d" % download_stream.size)
            download_stream.download_to_stream(my_blob)

#break lease after download
lease.break_lease()