python - 是否可以使用 Python SDK 检查特定 Azure blob 上是否有租约?

标签 python azure azure-blob-storage azure-sdk-python

我有一段代码,通常由多个进程运行,其中必须使用一些新信息更新某个 blob 文件。因为我不希望这些进程同时写入该文件(这会导致冲突错误),所以当进程开始修改该文件时,我在该文件上放置了租约,并希望在修改完成后中断该租约。

现在的问题是,我必须检查每个进程 blobfile 上是否已经有租约,如果没有,则继续等待,直到租约被打破,然后为当前进程获取租约。我现在实现这一点的方法是运行以下代码:

stream = io.BytesIO()
if self.blob_service.exists(self.container, self.changelog):
   while True:
       try:
          self.blob_service.acquire_blob_lease(self.container, self.changelog,
                                                         lease_duration=-1)
          break
       except AzureConflictHttpError:
          pass
   self.blob_service.get_blob_to_stream(self.container, self.changelog, stream)
   stream.seek(0)

   .... do some processing and write to blobfile
   self.blob_service.break_blob_lease(self.container, self.changelog)

blob_service这是 BlockBlobService 的一个实例。然而,这会导致连续的日志记录 2020-04-19 16:19:51,517 ERROR Client-Request-ID=d53b7e6e-8248-11ea-a073-dca90492a6b8 Retry policy did not allow for a retry: Server-Timestamp=Sun, 19 Apr 2020 14:19:50 GMT, Server-Request-ID=c339439f-e01e-0058-1d55-16e3bc000000, HTTP status code=409, Exception=There is already a lease present. ErrorCode: LeaseAlreadyPresent<?xml version="1.0" encoding="utf-8"?><Error><Code>LeaseAlreadyPresent</Code><Message>There is already a lease present.RequestId:c339439f-e01e-0058-1d55-16e3bc000000Time:2020-04-19T14:19:51.5047575Z</Message></Error>. 这将一直持续到进程 1 中断租约且进程 2 可以获得租约为止。

现在当然有办法抑制这种日志记录,但我更愿意能够做这样的事情。

while True:
    if self.blob_service.blob_has_lease(self.container, self.changelog):
        continue
    else:
        self.blob_service.acquire_blob_lease(self.container, self.changelog,
                                                         lease_duration=-1)

if self.blob_service.blob_has_lease(self.container, self.changelog)是我自己编造的。但是,我无法以任何方式找到这样的方法或属性来检查某个 blob 当前是否有租约。是否有任何这样的方法或解决方法可以让我像上面提供的示例一样编写代码?

最佳答案

您可以调用get_blob_properties方法来获取 blob 的属性。如果租用了一个 blob,那么您应该在 lease 中获得一些值。 blob's properties下的变量.

关于python - 是否可以使用 Python SDK 检查特定 Azure blob 上是否有租约?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305916/

相关文章:

python - 并行azure blob上传收到警告 'urllib3.connectionpool WARNING - Connection pool is full, discarding connection'

azure - 在 Azure 开发存储上,并行上传 block blob 时收到 "the specified blob already exists"

azure - 在 Azure 中安装 CloudDrive 快照时出错

python - Django 搜索多个过滤器

python - systemd中pyenv下的jupyter笔记本仅提供系统python

python - 为 python 2.7 安装 mod_wsgi 时出错

azure - 从 k8s 解析 yaml 配置映射数据

python - 为什么 .pth 文件在 Python 虚拟环境中加载两次?

azure - MS图API : invalid authentication token

android - NativeMessageHandler+<SendAsync> System.OperationCanceledException : The operation was canceled