import os
import sys
import oss2

# 本地文件夹路径目录
basedir = 'D:\\'
# 阿里云存储目录，要保证和下面的project_name相同
projectList = 'document'
# 本地文件夹
dirList = [basedir]


class AliyunOss(object):

    def __init__(self):
        self.access_key_id ='LTAI5tESB5tEYccrrUjkG7c5'  # 从阿里云查询到的 AccessKey 的ID
        self.access_key_secret = "l9CZU6pkWDdQVGbHxwZGdx9mu9cv2U"  # 从阿里云查询到的 AccessKey 的Secret
        self.auth = oss2.Auth(self.access_key_id, self.access_key_secret)
        self.bucket_name = "leaseraycos"  # 阿里云上创建好的Bucket的名称
        self.endpoint = "oss-cn-shanghai.aliyuncs.com"  # 阿里云从Bucket中查询到的endpoint
        self.bucket = oss2.Bucket(self.auth, self.endpoint, self.bucket_name)

    def put_object_from_file(self, name, file):
        """
        :param name: 在阿里云Bucket中要保存的文件名
        :param file: 本地图片的文件名
        :return:
        """
        self.bucket.put_object_from_file(name, file)
        return "https://{}.{}/{}".format(self.bucket_name, self.endpoint, name)


def uploadFile(file):
    # remoteName为oss中路径+文件名,file为本地上传文件名称
    remoteName = projectList + file.replace(basedir, '').replace('\\', '/')
    print('uploading..', file, 'remoteName', remoteName)
    result = AliyunOss().put_object_from_file(remoteName, file)
    # 文件上传成功http状态输出200
    # print('http status: {0}'.format(result.status))
    print(result)

