concurnecy
Browse files- app.py +1 -1
- upload_to_s3.py +78 -0
app.py
CHANGED
|
@@ -17,7 +17,7 @@ from concurrent.futures import ThreadPoolExecutor
|
|
| 17 |
import random
|
| 18 |
import string
|
| 19 |
|
| 20 |
-
from
|
| 21 |
load_dotenv()
|
| 22 |
|
| 23 |
# Access the variables
|
|
|
|
| 17 |
import random
|
| 18 |
import string
|
| 19 |
|
| 20 |
+
from upload_to_s3 import upload_multiple_files
|
| 21 |
load_dotenv()
|
| 22 |
|
| 23 |
# Access the variables
|
upload_to_s3.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import boto3
|
| 3 |
+
import os
|
| 4 |
+
from botocore.exceptions import ClientError
|
| 5 |
+
import time
|
| 6 |
+
from boto3.s3.transfer import TransferConfig# Configuration
|
| 7 |
+
REGION = "us-east-1"
|
| 8 |
+
BUCKET = "shortsai-us" # Replace with your bucket name
|
| 9 |
+
S3_KEY = "test/IMG_8280.MOV" # S3 path for the file
|
| 10 |
+
LOCAL_FILE_PATH = "/Users/georgia.bucea/products/ShortsAI/21_aug/mcp_video-3028_singular_display.MOV" # Replace with your local file path
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Access the variables
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 18 |
+
|
| 19 |
+
def upload_multiple_files(file_list, bucket):
|
| 20 |
+
|
| 21 |
+
uploaded_files = []
|
| 22 |
+
|
| 23 |
+
def upload_to_s3(file_path, bucket, s3_key):
|
| 24 |
+
"""Upload a local file to an S3 bucket."""
|
| 25 |
+
|
| 26 |
+
start_time = time.time()
|
| 27 |
+
try:
|
| 28 |
+
|
| 29 |
+
config = TransferConfig(
|
| 30 |
+
multipart_threshold=16*1024*1024, # 8MB threshold
|
| 31 |
+
max_concurrency=30, # Parallel upload threads
|
| 32 |
+
multipart_chunksize=8*1024*1024, # Chunk size
|
| 33 |
+
use_threads=True,
|
| 34 |
+
)
|
| 35 |
+
# Initialize S3 client
|
| 36 |
+
s3 = boto3.client("s3",endpoint_url='https://s3-accelerate.amazonaws.com', region_name=REGION )
|
| 37 |
+
|
| 38 |
+
# Verify file exists
|
| 39 |
+
if not os.path.exists(file_path):
|
| 40 |
+
raise FileNotFoundError(f"File not found: {file_path}")
|
| 41 |
+
|
| 42 |
+
# # Upload file
|
| 43 |
+
s3.upload_file(file_path, bucket, s3_key,ExtraArgs={'ACL': 'bucket-owner-full-control'}, Config=config)
|
| 44 |
+
print(f"Successfully uploaded {file_path} to s3://{bucket}/{s3_key}")
|
| 45 |
+
|
| 46 |
+
uploaded_files.append(s3_key)
|
| 47 |
+
end_time = time.time()
|
| 48 |
+
# Calculate elapsed time
|
| 49 |
+
elapsed_time = end_time - start_time
|
| 50 |
+
|
| 51 |
+
print(f"In {elapsed_time} time ")
|
| 52 |
+
return True
|
| 53 |
+
|
| 54 |
+
except FileNotFoundError as e:
|
| 55 |
+
print(f"Error: {e}")
|
| 56 |
+
return False
|
| 57 |
+
except ClientError as e:
|
| 58 |
+
print(f"AWS Client Error: {e}")
|
| 59 |
+
return False
|
| 60 |
+
except Exception as e:
|
| 61 |
+
print(f"Unexpected error: {e}")
|
| 62 |
+
return False
|
| 63 |
+
|
| 64 |
+
with ThreadPoolExecutor(max_workers=10) as executor:
|
| 65 |
+
executor.map(lambda f: upload_to_s3(f[0], bucket, f[1]), file_list)
|
| 66 |
+
return uploaded_files
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
# upload_to_s3(LOCAL_FILE_PATH,BUCKET,S3_KEY)
|
| 71 |
+
bucket = os.getenv("BUCKET")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
file_list = [
|
| 75 |
+
("/Users/georgia.bucea/products/ShortsAI/21_aug/mcp_video-3028_singular_display.MOV", "test/21_aug/mcp_video-3028_singular_display.MOV"),
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
uploaded = upload_multiple_files(file_list, bucket)
|