如何使用 ETag 更新 Azure Table 存储实体

How to update of Azure Table Storage Entity with ETag

概述:当我在 container/productID(文件夹)/blobName 下的 blob 存储中上传 blob 时,事件订阅会将此事件保存在存储队列中。之后 azure 函数轮询此事件并执行以下操作:

1- read from the corresponding table the current count property (how many blobs are stored under productID(folder)) with the etag

2- increase the count + 1

3- write it back in the corresponding table, if ETag is matched, then the field count will be increased, otherwise throws an error. if err is thrown, sleep while and go to step 1 (while loop)

4- if property field successful increased, then return

场景:尝试将五个项目上传到 blob 存储

期望值: 属性 in table storage 存储 5.

问题:插入前四项成功后,插入第五项代码陷入死循环,计数属性一直增加。为什么会发生这种情况,我没有任何想法。你的任何想法都会很好

#more code
        header_etag = "random-etag"
        response_etag = "random-response"
        while(response_etag != header_etag):
            sleep(random.random())  # sleep between 0 and 1 second.
            header = table_service.get_entity_table(
                client_table, client_table, client_product)
            new_count = header['Count'] + 1
            entity_product = create_product_entity(
                client_table, client_product, new_count, client_image_table)
            header_etag = header['etag']
            try:    
                response_etag = table_service1.merge_entity(client_table, entity_product,
                                                            if_match=header_etag)
            except:
                logging.info("race condition detected")

尝试使用以下代码在 while 循环中实现您的参数和逻辑:

val = 0 # Initial value as zero
success = False
    while True:
        val  = val + 1 #incrementing
        CheckValidations = input(‘Check’) #add validations
        if CheckValidations == "abc123":
            success = True # this will allow the loop
            break
        if val > 5:
            break
        print("Please Try Again")
if success == True:
print("Welcome!")

还要从 Azure Python SDK 文档中检查以下 .py 文件,其中我们有一个明确的功能,用于更新、合并和插入 table 存储

中的实体

azure-sdk-for-python/sample_update_upsert_merge_entities.py at main · Azure/azure-sdk-for-python (github.com)

参考Microsoft documentation检查我们如何为创建实体传递准确的参数。

有关 table 示例的更多信息,请查看 Azure 示例

storage-table-python-getting-started/table_basic_samples.py at master · Azure-Samples/storage-table-python-getting-started (github.com)