|
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'] |
|
|
|
|
|
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key) |
|
|
|
|
|
client = CosS3Client(config) |
|
|
|
return client |
|
|
|
|
|
|
|
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 |
|
) |
|
|
|
|
|
|
|
def generate_tx_presigned_url(img_name='1398635107044175872.png', |
|
bucket_name='fashion-person-1254150807'): |
|
|
|
client = config_tx() |
|
|
|
|
|
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__': |
|
|
|
|
|
|
|
print(datetime.now()) |
|
print(datetime.now()) |
|
|