Search code examples
python-3.xazureazure-storage

Monitor status of files in cloud endpoint (storage account) vs server endpoint


I've written some scripts using the Python and Go oriented documentation which can upload/download a file etc and now I'm thinking about managing files between a machine and my Azure storage account.

I'm now thinking of either creating/utilising something akin to 'git status' which you can use to see the differences between a local git repository and a remote git repository. My initial thought was to use some kind of queue system, but I can't seem to find documentation on doing this for files with the Azure Storage SDK.

Any help would be greatly appreciated!


Solution

  • There are several methods to check what you are currently looking for, a simple python script can give you this capability, I just wrote and tested one that compares files and folders existing in the Azure File Share VS the local folders on your server, then prints what is not yet in the Azure file share:

    import os
    import azure.storage.common
    from azure.storage.common import CloudStorageAccount
    from azure.storage.file import FileService
    file_service = FileService(account_name='storage_acct_name', account_key='keyhere')
    
    
    local_files = os.listdir(r'C:\Users\someuser\Desktop\test')
    print("Local Files: ")
    print(local_files)
    
    print('Azure Files in the Cloud:')
    files_incloud= list()
    
    generator = file_service.list_directories_and_files('filesharename')
    for file_or_dir in generator:
            files_incloud.append(file_or_dir.name)
            print(file_or_dir.name)
    
    def Diff(local_files,files_incloud):
        return(list(set(local_files) - set(files_incloud)))
    
    print("Files NOT in the Cloud FILESHARE are: ")
    print(Diff(local_files,files_incloud))
    

    in my testing scenario, I had two files locally : text1.txt and text2.txt , and in the Azure cloud share I had: text2.txt only, result is as below:

        Local Files:
        ['text1.txt', 'text2.txt']
        Azure Files in the Cloud:
        xxxxxx.pptx
        xxxx.pdf
        text2.txt
        Files NOT in the Cloud FILESHARE are:
        ['text1.txt']
    

    New Addition for string matching:

    result = (Diff(local_files,files_incloud))
    print (result)
    print("\n")
    print("Matching Strings:")
    print("\n")
    for item in result:
            if "text" in item:
                    print item
    

    Result:

    Matching Strings:
    
    
    text1.txt
    

    I also shared it on this repo: https://github.com/adamsmith0016/Azure-storage