import os
import tos
import json

TOS_AK = "AKLTMmRiMmU3YmY5ZjZjNDZkMTlhMmQxY2JkYTllYTQzNDI"
TOS_SK = "WkdWak4yUTRNakl3WVdOa05HUXdaR0V4TlRBM1l6YzJZMll5WkRnMFlUTQ=="
TOS_ENDPOINT = "tos-cn-beijing.volces.com"
TOS_REGION = "cn-beijing"
TOS_BUCKET_NAME = "douchacha-web"


def put_video(object_key,file_name):
    try:
        client = tos.TosClientV2(TOS_AK, TOS_SK, TOS_ENDPOINT, TOS_REGION)
        client.put_object_from_file(TOS_BUCKET_NAME, object_key, file_name)
    except tos.exceptions.TosClientError as e:
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))


def put_string_to_tos( object_key, content, retry_times=3):
    client = tos.TosClientV2(TOS_AK, TOS_SK, TOS_ENDPOINT, TOS_REGION, max_retry_count=3)
    for _ in range(retry_times):
        try:
            json_str = json.dumps(content, ensure_ascii=False)
            client.put_object(TOS_BUCKET_NAME, object_key, content=json_str.encode('utf-8'))

            return True
        except Exception as e:
            print(e)

            continue
    return False

