-
Notifications
You must be signed in to change notification settings - Fork 4
/
oss.py
73 lines (58 loc) · 2.06 KB
/
oss.py
1
# -*- coding: UTF-8 -*-# Author: chenqihuiquery = "{query}"import timeimport oss2import jsonimport osfrom AppKit import NSPasteboard, NSPasteboardTypePNG, NSFilenamesPboardTypeaccess_key_id = os.getenv('access_key_id')access_key_secret = os.getenv('access_key_secret')bucket_name = os.getenv('bucket_name')endpoint = os.getenv('endpoint')def get_paste_img_file(): """ 将剪切板数据保存到本地文件并返回文件路径 """ pb = NSPasteboard.generalPasteboard() # 获取当前系统剪切板数据 data_type = pb.types() # 获取剪切c板数据的格式类型 # 根据剪切板数据类型进行处理 if NSPasteboardTypePNG in data_type: # PNG处理 data = pb.dataForType_(NSPasteboardTypePNG) filename = '%s.png' % int(time.time()) filepath = '/tmp/%s' % filename # 保存文件的路径 ret = data.writeToFile_atomically_(filepath, False) # 将剪切板数据保存为文件 if ret: # 判断文件写入是否成功 return filepath elif NSFilenamesPboardType in data_type: # file in machine return pb.propertyListForType_(NSFilenamesPboardType)[0]def upload_file(): auth = oss2.Auth(access_key_id, access_key_secret) bucket = oss2.Bucket(auth, endpoint, bucket_name) file_name = get_paste_img_file() key_name = file_name[file_name.rfind('/'):] date = time.strftime("%Y-%m-%d", time.localtime()) key = date + key_name result = bucket.put_object_from_file(key, file_name) url = result.resp.response.url data = { 'items' : [ {'title' : 'url', 'arg': url, "icon": { 'type': 'png', 'path': 'icon.png' } }, {'title': 'md', 'arg': '![](%s)' % url, 'icon': { 'type': 'png', 'path': 'icon.png' } } ] } url_result = json.dumps(data) print(url_result)if __name__ == '__main__': upload_file()