Created
January 26, 2024 14:12
-
-
Save renini/84fd834033f79d8942aba98f7de6f9bd to your computer and use it in GitHub Desktop.
upload_mp4_files_to_s3_test2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import boto3 | |
import mysql.connector | |
from tqdm import tqdm | |
def upload_mp4_files_to_s3(bucket_name: str, local_directory: str, access_key_id: str, secret_access_key: str, db_host: str, db_user: str, db_password: str, db_database: str): | |
session = boto3.Session( | |
aws_access_key_id=access_key_id, | |
aws_secret_access_key=secret_access_key | |
) | |
s3 = session.resource('s3', endpoint_url='http://MINIO_SERVER_IP:9000') | |
cnx = mysql.connector.connect(user=db_user, password=db_password, host=db_host, database=db_database) | |
cursor = cnx.cursor() | |
for root, dirs, files in os.walk(local_directory): | |
for file in files: | |
if file.endswith('.mp4'): | |
file_path = os.path.join(root, file) | |
s3_path = os.path.join(root, file).replace(local_directory, '') | |
with open(os.path.join(root, 'index.json')) as f: | |
data = json.load(f) | |
description = data[file]['description'] | |
with tqdm(total=os.path.getsize(file_path), unit='B', unit_scale=True, desc=file) as progress_bar: | |
s3.meta.client.upload_file(file_path, bucket_name, s3_path, Callback=lambda bytes_transferred: progress_bar.update(bytes_transferred)) | |
# Insert the filename and description into MySQL database | |
query = "INSERT INTO videos (name, description) VALUES (%s, %s) ON DUPLICATE KEY UPDATE description = %s" | |
values = (file, description, description) | |
cursor.execute(query, values) | |
cnx.commit() | |
print(f"Inserted '{file}' and '{description}' into MySQL database") | |
# Example usage | |
bucket_name = 'my-bucket' | |
local_directory = '/path/to/local/directory' | |
access_key_id = 'YOUR_ACCESS_KEY' | |
secret_access_key = 'YOUR_SECRET_KEY' | |
db_host = 'localhost' | |
db_user = 'yourusername' | |
db_password = 'yourpassword' | |
db_database = 'mydatabase' | |
upload_mp4_files_to_s3(bucket_name, local_directory, access_key_id, secret_access_key, db_host, db_user, db_password, db_database) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment