File size: 2,976 Bytes
149fbcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import oss2

auth = oss2.Auth('LTAI5tAGoBnm5eYsnZ5E1zMr', 'KsLQWLRSEVCfoUed3DVInGOou3TAQh')
endpoint = 'https://oss-ap-southeast-1.aliyuncs.com'
bucket = oss2.Bucket(auth, endpoint, 'ai-business-algo-pai')
# file_path = os.path.join('tts_model', model_name)
# bucket.put_object(file_path, audio_buffer.getvalue(), headers=headers)
# print(bucket.sign_url('GET', file_path, 3600 * 24 * 30))

import os
import tarfile
from io import BytesIO
from uuid import uuid1

def upload_tar_gz_file(
    file_path,       # 本地 .tar.gz 文件路径
    osspath="tts_model",  # OSS存储路径
    expire_time=30    # 签名URL有效期(天)
):
    """
    直接上传现有的 .tar.gz 文件到 OSS
    :param file_path: 本地 .tar.gz 文件路径 (如 "pretrained_models.tar.gz")
    :param osspath: OSS存储目录 (默认 "pretrained_models")
    :param expire_time: 签名URL有效期(天)
    :return: 签名URL
    """
    # if not os.path.isfile(file_path):
    #     raise ValueError("File not found: " + file_path)

    # 从文件名提取基础名称(保留 .tar.gz 后缀)
    print("file_path:", file_path)
    filename = os.path.basename(file_path)
    oss_filepath = os.path.join(osspath, filename)

    # 读取文件二进制内容
    with open(file_path, "rb") as f:
        file_content = f.read()

    # 设置 OSS 文件头
    headers = {
        "Content-Type": "application/gzip",
        "Content-Disposition": f'attachment; filename="{filename}"'
    }

    # 上传到 OSS
    session = oss2.Session()
    session.session.verify = False  # 控制SSL验证

    bucket.put_object(oss_filepath, file_content, headers=headers)

    # 生成签名 URL
    signed_url = bucket.sign_url("GET", oss_filepath, 3600 * 24 * expire_time)
    return signed_url

if __name__ =="__main__":
    from oss2 import Bucket, Auth
    import sys

    # 初始化 OSS
    # auth = Auth("your_access_key_id", "your_access_key_secret")
    # bucket = Bucket(auth, "https://oss-cn-hangzhou.aliyuncs.com", "your-bucket-name")
    session = oss2.Session()
    session.session.verify = False  # 控制SSL验证
    
    # # 使用自定义会话创建Bucket
    # bucket = oss2.Bucket(
    #     auth, 
    #     'http://oss-ap-southeast-1.aliyuncs.com',  # 注意使用http而非https
    #     'ai-business-algo-pai',
    #     session=session
    # )
    auth = oss2.Auth('LTAI5tAGoBnm5eYsnZ5E1zMr', 'KsLQWLRSEVCfoUed3DVInGOou3TAQh')
    endpoint = 'http://oss-ap-southeast-1.aliyuncs.com'
    bucket = oss2.Bucket(auth, endpoint, 'ai-business-algo-pai', session=session)

    # 上传文件夹
    url = upload_tar_gz_file(
        file_path=sys.argv[1],
        osspath="tts_model",
        # compression_level=9,  # 最高压缩
        # expire_time=365,      # 1年有效期
        # remove_prefix="/mnt/by079416/fengping/CosyVoice2"  # 去除本地路径前缀
    )

    print("Model uploaded. Download URL:", url)
    # import sys
    # upload_model_folder(sys.argv[1])