| from dotenv import load_dotenv |
| import boto3 |
| import os |
| from botocore.exceptions import ClientError |
| import time |
| from boto3.s3.transfer import TransferConfig |
| REGION = "us-east-1" |
| BUCKET = "shortsai-us" |
| S3_KEY = "test/IMG_8280.MOV" |
| LOCAL_FILE_PATH = "/Users/georgia.bucea/products/ShortsAI/21_aug/mcp_video-3028_singular_display.MOV" |
|
|
| import os |
|
|
| def list_files_in_folder(folder_path: str) -> list: |
| """ |
| Reads all files in the specified folder (non-recursive, excludes subdirectories). |
| |
| :param folder_path: Path to the folder. |
| :return: List of file names. |
| """ |
| try: |
| |
| if not os.path.isdir(folder_path): |
| raise ValueError(f"Directory does not exist: {folder_path}") |
| |
| |
| files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))] |
| return files |
| except Exception as e: |
| print(f"Error reading folder: {e}") |
| return [] |
|
|
|
|
|
|
| load_dotenv() |
|
|
| |
|
|
|
|
| from concurrent.futures import ThreadPoolExecutor |
|
|
| def upload_multiple_files(file_list, bucket): |
|
|
| uploaded_files = [] |
| |
| def upload_to_s3(file_path, bucket, s3_key): |
| """Upload a local file to an S3 bucket.""" |
|
|
| start_time = time.time() |
| try: |
|
|
| config = TransferConfig( |
| multipart_threshold=16*1024*1024, |
| max_concurrency=30, |
| multipart_chunksize=8*1024*1024, |
| use_threads=True, |
| ) |
| |
| s3 = boto3.client("s3",endpoint_url='https://s3-accelerate.amazonaws.com', region_name=REGION ) |
|
|
| |
| if not os.path.exists(file_path): |
| raise FileNotFoundError(f"File not found: {file_path}") |
|
|
| |
| s3.upload_file(file_path, bucket, s3_key,ExtraArgs={'ACL': 'bucket-owner-full-control'}, Config=config) |
| print(f"Successfully uploaded {file_path} to s3://{bucket}/{s3_key}") |
|
|
| uploaded_files.append(s3_key) |
| end_time = time.time() |
| |
| elapsed_time = end_time - start_time |
|
|
| print(f"In {elapsed_time} time ") |
| return True |
|
|
| except FileNotFoundError as e: |
| print(f"Error: {e}") |
| return False |
| except ClientError as e: |
| print(f"AWS Client Error: {e}") |
| return False |
| except Exception as e: |
| print(f"Unexpected error: {e}") |
| return False |
|
|
| with ThreadPoolExecutor(max_workers=30) as executor: |
| executor.map(lambda f: upload_to_s3(f[0], bucket, f[1]), file_list) |
| return uploaded_files |
|
|
|
|
| if __name__ == "__main__": |
| |
| bucket = os.getenv("BUCKET") |
|
|
| folder="/Users/georgia.bucea/products/ShortsAI/16_aug" |
|
|
| files=list_files_in_folder("/Users/georgia.bucea/products/ShortsAI/16_aug") |
| files=[(folder+ "/"+os.path.basename(path), "testall16/"+os.path.basename(path)) for path in files] |
| print(files) |
|
|
| |
| |
| |
|
|
| uploaded = upload_multiple_files(files, bucket) |
|
|