【问题标题】:Is it possible to check if there is a lease on a specific Azure blob, using the Python SDK?是否可以使用 Python SDK 检查特定 Azure blob 上是否存在租约?
【发布时间】:2020-08-01 23:29:39
【问题描述】:

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

现在的问题是,如果 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 当前是否有租约。有没有这样的方法或解决方法,我可以像上面提供的示例那样编写我的代码?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2011-02-08
      • 2020-06-04
      • 2020-12-11
      • 2012-06-17
      • 1970-01-01
      • 2017-11-07
      相关资源
      最近更新 更多