Create s3_migration.py
Browse files- s3_migration.py +42 -0
s3_migration.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import boto3
|
| 3 |
+
from multiprocessing import Pool, cpu_count
|
| 4 |
+
|
| 5 |
+
# Configuration
|
| 6 |
+
LOCAL_FOLDER = 'PRT' # The local folder with files
|
| 7 |
+
BUCKET_NAME = 'your-s3-bucket-name' # Your S3 bucket name
|
| 8 |
+
REGION = 'your-region' # Your AWS region, e.g., 'us-east-1'
|
| 9 |
+
|
| 10 |
+
# Initialize S3 client
|
| 11 |
+
s3_client = boto3.client('s3', region_name=REGION)
|
| 12 |
+
|
| 13 |
+
def upload_file(file_path):
|
| 14 |
+
"""
|
| 15 |
+
Upload a file to S3.
|
| 16 |
+
:param file_path: Path of the file to upload
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
# Define the key for the file in S3 (optional: you can keep the same name or change it)
|
| 20 |
+
s3_key = os.path.relpath(file_path, LOCAL_FOLDER)
|
| 21 |
+
print(f"Uploading {file_path} to s3://{BUCKET_NAME}/{s3_key}")
|
| 22 |
+
s3_client.upload_file(file_path, BUCKET_NAME, s3_key)
|
| 23 |
+
print(f"Upload of {file_path} completed successfully.")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error uploading {file_path}: {e}")
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
# Get all files in the LOCAL_FOLDER
|
| 29 |
+
files_to_upload = [
|
| 30 |
+
os.path.join(LOCAL_FOLDER, f) for f in os.listdir(LOCAL_FOLDER)
|
| 31 |
+
if os.path.isfile(os.path.join(LOCAL_FOLDER, f))
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Determine the number of processes to use
|
| 35 |
+
num_processes = min(cpu_count(), len(files_to_upload))
|
| 36 |
+
|
| 37 |
+
# Use multiprocessing Pool to upload files in parallel
|
| 38 |
+
with Pool(processes=num_processes) as pool:
|
| 39 |
+
pool.map(upload_file, files_to_upload)
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
main()
|