File size: 2,911 Bytes
117b368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import yaml
from datetime import datetime
from qcloud_cos import CosConfig, CosS3Client


# 配置腾讯云客户端
def config_tx(yaml_path='./config/txcloud/tx.yaml'):
    # 读取腾讯云的配置
    f = open(yaml_path, 'r', encoding='utf-8')
    res = yaml.load(f, Loader=yaml.FullLoader)

    # 配置腾讯云的访问信息
    secret_id = res['SecretId']
    secret_key = res['SecretKey']
    region = res['region']

    # 创建COS配置对象
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)

    # 创建COS客户端对象
    client = CosS3Client(config)

    return client


# 上传PNG图片到腾讯云
def save_tx(file_data,
            file_name='1398635107044175872.png',
            bucket_name='fashion-person-1254150807'):
    # 创建腾讯云客户端
    client = config_tx()

    # 发送请求上传数据
    response = client.put_object(
        Bucket=bucket_name,
        Body=file_data,  # 二进制数据
        Key=file_name
    )


# 从腾讯云取出图片URL
def generate_tx_presigned_url(img_name='1398635107044175872.png',
                              bucket_name='fashion-person-1254150807'):
    # 创建腾讯云客户端
    client = config_tx()

    # 生成预签名URL
    try:
        img_url = client.get_presigned_url(
            Method='GET',
            Bucket=bucket_name,
            Key=img_name,
            Expired=315360000
        )
        return img_url
    except Exception as e:
        print("生成预签名URL失败:", e)
        return None


# 从腾讯云下载图片
def download_tx(file_name='1398635107044175872.png',
                local_file_path='../data/temp/temp.png',
                bucket_name='fashion-person-1254150807'):
    # 创建腾讯云客户端
    client = config_tx()

    # 从指定桶下载指定图片
    try:
        response = client.get_object(
            Bucket=bucket_name,
            Key=file_name
        )

        with open(local_file_path, 'wb') as file:
            file.write(response['Body'].get_raw_stream().read())

        print(f"成功下载文件到 {local_file_path}")
    except Exception as e:
        print(f"下载图片失败: {e}")


# 检查腾讯云存储桶中是否存在指定名称的图片(忽略文件扩展名)
def check_image_name_in_tx(image_name,
                           bucket_name='fashion-guangzhou-dataset'):
    # 创建腾讯云客户端
    client = config_tx()

    # 获取存储桶中的所有对象
    response = client.list_objects(Bucket=bucket_name)

    # 检查是否有与给定图片名称相同的结果
    for content in response.get('Contents', []):
        if content['Key'] == image_name:
            return True

    return False


if __name__ == '__main__':
    # save_tx(png_to_binary())
    # print(generate_tx_presigned_url())
    # download_tx()
    print(datetime.now())
    print(datetime.now())