# -*- coding: UTF-8 -*-
if __name__ == '__main__':
    import os
    os.chdir("..")

from tools.loglog import logger
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import json

def query_file_content(key):
    """
    :param key: OSS的键；key = 'study/article/14138566'
    :return: 文章的json
    """
    auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
    bucket = oss2.Bucket(auth, 'oss-cn-hangzhou.aliyuncs.com', 'qingti-private')
    try:
        object_stream = bucket.get_object(key)
        content = b''.join(object_stream)
       
        text_content = content.decode('utf-8')
        json_content = json.loads(text_content)
        return json_content
    except oss2.exceptions.NoSuchKey as e:
        return 0
    except Exception as e:
        logger.error(f"{type(e).__name__}: {e}")

if __name__ == '__main__':
    print(query_file_content('study/article/10613145')["id"])