import os
from hashlib import md5
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider


os.environ['OSS_ACCESS_KEY_ID'] = 'LTAI5tEYvefc4U3fyU5du225'
os.environ['OSS_ACCESS_KEY_SECRET'] = 'Z1gZtAGe8NwRtXgPzVgRMkRez4Ex4K'
OSS_BUCKET_ENDPOINT = 'oss-accelerate.aliyuncs.com'
OSS_BUCKET_NAME = 'aigc-admin'
OSS_BUCKET_PATH = 'crawler'
OSS_BUCKET_REGION = 'ap-southeast-1'

def local_file_upload(local_file_path: str):
    """
    上传本地文件
    """
    
    auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
    # 创建Bucket实例，指定存储空间的名称和Region信息。
    bucket = oss2.Bucket(auth, OSS_BUCKET_ENDPOINT, OSS_BUCKET_NAME, region=OSS_BUCKET_REGION)

    # 本地文件的完整路径
    file_suffix = local_file_path.split('.')[-1]
    objectName = f'crawler/video/{md5(local_file_path.encode("utf-8")).hexdigest()}.{file_suffix}'

    # 使用put_object_from_file方法将本地文件上传至OSS
    bucket.put_object_from_file(objectName, local_file_path)
    return f'https://res.cybertogether.net/{objectName}'
