python脚本编写( 锁定) /读取Azure中的文件



iam python编程和azure。

我需要编写一个将由2个进程执行的脚本。

这两个过程将运行相同的python脚本。我知道Azure有storageaccounts可以将一些文件放入其中,我发现了这一点:https://learn.microsoft.com/en-us/python/api/azure-storage-file/azure.storage.file.fileservice.fileservice.fileservice?view = azure-python

和:https://github.com/azure/azure-storage-python

这是一些伪代码,可以说明我需要实现的目标:

function useStorageFile
   if(fileFromStorage == null)
      createFileInStorage lockFileInStorage;
      executeDockerCommand;
      writeResultOFCommandInStorageFile;
   else
      if(fileFromStorage != null)
        X:if(fileFromStorage.status !== 'locked')
           readResultFromFile
        else
           wait 1s;
           continue X;

是否可以将文件锁定/解锁Azure?例如,如何在Python中实现这一目标?谢谢。

编辑我设法用python脚本在blob存储中编写一个文件。现在的问题是:如何在第一个过程中编写命令结果时锁定文件,并在Blob储物锁(如果存在的如果存在...(时,请通过第二个过程读取文件。第一个过程?这是python脚本IAM,使用:

import os, uuid, sys
from azure.storage.blob import BlockBlobService, PublicAccess
def run_sample():
    try:
        # Create the BlockBlockService that is used to call the Blob service for the storage account
        block_blob_service = BlockBlobService(account_name='xxxxxx', account_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
        # Create a container called 'quickstartblobs'.
        container_name ='quickstartblobs'
        block_blob_service.create_container(container_name)
        # Set the permission so the blobs are public.
        block_blob_service.set_container_acl(container_name, public_access=PublicAccess.Container)
        # Create a file in Documents to test the upload and download.
        local_path=os.path.abspath(os.path.curdir)
        local_file_name ='youss.txt'
        full_path_to_file =os.path.join(local_path, local_file_name)
        # Write text to the file.
        file = open(full_path_to_file,  'w')
        file.write("Hello, World!")
        file.close()
        print("Temp file = " + full_path_to_file)
        print("nUploading to Blob storage as blob" + local_file_name)
        # Upload the created file, use local_file_name for the blob name
        block_blob_service.create_blob_from_path(container_name, local_file_name, full_path_to_file)
        # List the blobs in the container
        print("nList blobs in the container")
        generator = block_blob_service.list_blobs(container_name)
        for blob in generator:
            print("t Blob name: " + blob.name)
        # Download the blob(s).
        # Add '_DOWNLOADED' as prefix to '.txt' so you can see both files in Documents.
        full_path_to_file2 = os.path.join(local_path, str.replace(local_file_name ,'.txt', '_DOWNLOADED.txt'))
        print("nDownloading blob to " + full_path_to_file2)
        block_blob_service.get_blob_to_path(container_name, local_file_name, full_path_to_file2)
        sys.stdout.write("Sample finished running. When you hit <any key>, the sample will be deleted and the sample "
                         "application will exit.")
        sys.stdout.flush()
        input()
        # Clean up resources. This includes the container and the temp files
        block_blob_service.delete_container(container_name)
        os.remove(full_path_to_file)
        os.remove(full_path_to_file2)
    except Exception as e:
        print(e)

# Main method.
if __name__ == '__main__':
    run_sample()

在编写命令结果时,我该如何锁定文件 第一个过程,并在第二个过程中阅读 Blob储物锁(如果存在...(由第一个发布 过程?

Azure Blob存储具有一个名为Lease的功能,您可以使用。从本质上讲,Leasing进程获得了对资源的独家锁定(在您的情况下(,并且只有一个进程可以在斑点上获取租赁。一旦在斑点上获得租赁后,任何其他过程都无法修改或删除斑点。

因此,您需要做的就是尝试在撰写书写之前先在斑点上获取租约。如果BLOB已经租赁,您将收回错误(HTTP状态代码412,预处理错误错误(。假设您没有错误,则可以继续更新文件。更新文件后,您可以手动释放锁(断路租赁或释放租赁(,也可以让租赁自动过期。假设您有错误,则应定期等待并定期获取Blob的租赁状态(例如每5秒(。一旦发现斑点不再租用,就可以阅读斑点的内容。

最新更新