赞
踩
Typora保存图片时会保存在本地,所以不方便;
有两种实现方式:
那么这里选择的是第二种方式;
第一种弊端:得一直运行PicGO这个软件很麻烦,那么当然从简
app_key
和app_secret
注意保留下来;没有的自行创建key公共读
访问地址
python
环境以及PIP
包依赖管理工具(下载包的玩意)
启动开发工具复制以下代码,修改以下参数:
访问地址
呀#! /usr/bin/python # -*- coding: UTF-8 -*- import logging import uuid import oss2 import os class AliyunOSS: def __init__(self, app_key, app_secret, oss_url, bucket_name): auth = oss2.Auth(app_key, app_secret) self.bucket = oss2.Bucket(auth, oss_url, bucket_name) self.url_prefix = 'http://' + bucket_name+ '.' + oss_url logging.info('======= oss init ok') def make_unique_id(self): """ 生成唯一的id :return: """ name = str(uuid.uuid4()) name = uuid.uuid5(uuid.NAMESPACE_DNS, name) name = str(name) return name def upload_image_async(self, upload_dto, need_logging=False): """ 同步方式上传图片 :param upload_dto: UploadFileDto 对象 :return: """ headers = { 'Content-Type': upload_dto.content_type, 'x-oss-meta-width': str(upload_dto.width), 'x-oss-meta-height': str(upload_dto.height), } # 生成唯一的filename name = self.make_unique_id() result = self.bucket.put_object(name, upload_dto.file_data, headers=headers) if need_logging: logging.info('http status: {0}'.format(result.status)) logging.info('request_id: {0}'.format(result.request_id)) logging.info('ETag: {0}'.format(result.etag)) logging.info('date: {0}'.format(result.headers['date'])) return name, self.get_full_url(name) def get_image_meta(self, name): """ 返回 meta 信息. 不会全部返回阿里云的信息, 只会返回一部分 :param name: :return: """ oss_meta = self.bucket.head_object(name).headers meta = { 'width': oss_meta['x-oss-meta-width'], 'height': oss_meta['x-oss-meta-height'], 'content_type': oss_meta['content-type'] } return meta def upload_file_async(self, file_data, content_type, need_logging=False): """ 同步方式上传文件 :param upload_dto: UploadFileDto 对象 :return: """ headers = { 'Content-Type': content_type, } # 生成唯一的filename name = self.make_unique_id() result = self.bucket.put_object(name, file_data, headers=headers) if need_logging: logging.info('http status: {0}'.format(result.status)) logging.info('request_id: {0}'.format(result.request_id)) logging.info('ETag: {0}'.format(result.etag)) logging.info('date: {0}'.format(result.headers['date'])) return name, self.get_full_url(name) def get_full_url(self, name): """ 返回图片的网络路径 :param name: :return: """ return self.url_prefix + '/' + name def upload_local_file_sync(self, file_path, content_type, need_logging=False): """ 同步方式上传文件 """ headers = { 'Content-Type': content_type } # 生成唯一的filename file_ext = os.path.splitext(file_path)[1] name = catalog+self.make_unique_id() + file_ext.lower() # 必须以二进制的方式打开文件,因为需要知道文件包含的字节数。 with open(file_path, 'rb') as fileobj: fileobj.seek(0) # 0 表示从开始,直到文件结束。 # Tell方法用于返回当前位置。 current = fileobj.tell() result = self.bucket.put_object(name, fileobj, headers=headers) if need_logging: logging.info('http status: {0}'.format(result.status)) logging.info('request_id: {0}'.format(result.request_id)) logging.info('ETag: {0}'.format(result.etag)) logging.info('date: {0}'.format(result.headers['date'])) return name, self.get_full_url(name) if __name__ == '__main__': import sys if len(sys.argv) < 2: print("调用错误, 图片格式不对") sys.exit(1) app_key = '' app_secret = '' oss_url = '' # 节点名称 bucket_name = '' # 上传至md/目录 catalog = 'md/' a_oss = AliyunOSS(app_key, app_secret, oss_url, bucket_name) # 返回值。 url_list = [] for i in range(1, len(sys.argv)): path = sys.argv[i] name, url = a_oss.upload_local_file_sync(path, 'image/png', True) url_list.append(url) print("Upload Success:") for url in url_list: print(url)
依次点击文件
——>偏好设置
根据图上所示配置信息;
命令输入:python 脚本的绝对路径
;如python D:\upload_oss.py
CTRL+V
把图片粘贴上,自动上传到OSS中python D:\upload_oss.py D:\1.png
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。