0

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")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 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 problem – OneCricketeer Apr 28 '18 at 00:24
  • @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 Apr 28 '18 at 06:19
  • When you said local files, you mean you file are on a VM (OnPrem/Cloud) or FTP or ??? – Thomas Apr 29 '18 at 21:04
  • @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 Apr 30 '18 at 08:46
  • I gurss you can find some watcher file service like this one: https://stackoverflow.com/questions/182197/how-do-i-watch-a-file-for-changes ??? – Thomas Apr 30 '18 at 11:05

1 Answers1

0

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.

Community
  • 1
  • 1
GuruCharan94
  • 852
  • 9
  • 13
  • Thanks- I tried it, it does work when I run the program manually but doesn’t seem to run at startup... any idea how I could do that? – Mridula Gunturi Apr 30 '18 at 08:58
  • This link might help you. https://stackoverflow.com/questions/4438020/how-to-start-a-python-file-while-windows-starts – GuruCharan94 May 01 '18 at 01:50