0
votes

I wrote Python code to upload files from a local directory to a blob container in Azure Storage Explorer, and am now trying to do this automatically (i.e. if there are files in the directory, they are automatically uploaded). Is there any way I can do this?

import os
from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContentSettings


accountName = "accountName"
ContainerSAS = "SAS_Key"
containerName = "containerName"

# Create reference to container using account name and SAS key
try:
    sas_service = BlockBlobService(account_name=accountName, 
    sas_token=ContainerSAS)
except Exception as e:
    print("Error during SAS service creation. Details: {0}".format(e))
print("Created SAS service with account {0}".format(accountName))

# Upload files to container from path
# directory_path = "< path to your directory >"
directory_path = "F:/dat_files_test"
for filename in os.listdir(directory_path):
    print(filename)
    blobName = filename
    localFile = directory_path + "/" + filename

    try:
        sas_service.create_blob_from_path(
        containerName,
        blobName,
        localFile,
        content_settings=ContentSettings(content_type='DPS/dat')
        )
    except Exception as e:
        print("Error during blob uploading. Details: {0}".format(e))
print("All files uploaded")
1
You would need something always running and listening on your local computer for files. What you do with those files is an implementation detail. In other words, this isn't a cloud computing or Azure problemOneCricketeer
@cricket_007 thanks.. do you know any such software that constantly checks for files? or any suggestions for how I could do implement such a program on python?Mridula Gunturi
When you said local files, you mean you file are on a VM (OnPrem/Cloud) or FTP or ???Thomas
@Thomas No, they’re in a local directory, as in a folder in my C drive. I need the program to run automatically whenever I start my computer.Mridula Gunturi
I gurss you can find some watcher file service like this one: stackoverflow.com/questions/182197/… ???Thomas

1 Answers

0
votes

If I understand the problem, you want to constantly keep checking if files are present in a folder and if so, do something with the files (like send to Azure).

If that is the case, then Scheduler should help you.