-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadDirectory.py
More file actions
70 lines (57 loc) · 2.07 KB
/
uploadDirectory.py
File metadata and controls
70 lines (57 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import boto3
import time
import threading
from boto3.s3.transfer import TransferConfig
start_time = time.time()
S3_BUCKET = ""
DIR_PATH = r""
ACCESS_ID = ""
ACCESS_KEY=""
maxThreads = 100
sema = threading.Semaphore(value=maxThreads)
config = TransferConfig(use_threads=False)
s3_client = boto3.client('s3',
aws_access_key_id=ACCESS_ID,
aws_secret_access_key= ACCESS_KEY)
rootFolder = os.path.basename(os.path.normpath(DIR_PATH))
'''
https://thispointer.com/python-how-to-get-list-of-files-in-directory-and-sub-directories/
This will give the list of all the files I need to upload. S3 effectively
has a flat file structure (folders are just files with 'folder/' prepended to them) and I can simply
upload them using the file path to access the file and a modified version of the filepath to create the
object key.
'''
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
allFiles.append(fullPath)
return allFiles
def uploadFiles(files):
threads = list()
for file in files:
thread = threading.Thread(target = uploadFileS3, args=(file,))
threads.append(thread)
thread.start()
'''
Documentation:
https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html
'''
def uploadFileS3(filePath):
sema.acquire()
objectKey = rootFolder + filePath[len(filePath) - (len(filePath)-len(DIR_PATH)):].replace('\\', '/')
s3_client.upload_file(filePath, S3_BUCKET, objectKey, Config = config)
sema.release()
files = getListOfFiles(DIR_PATH)
uploadFiles(files)
print("--- %s seconds ---" % (time.time() - start_time))