分享时间 | 2019-02-16 00:09 |
最后更新 | 2020-02-28 15:28 |
修订版本 | 1 |
用户许可 | -未设置- |
Quicker版本 | 1.5.0 |
动作大小 | 3.7 KB |
需配合python 文件使用
保存下列代码为oss.py
import sys
from shutil import copyfile
import oss2
from pyperclip import copy # 用来操作粘贴板
from random import choice
map = ["a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"]
def url_s(m):
url = ""
for i in range(m):
url = url + str(choice(map))
return url
# 获取远程文件名
# def get_remote_file_name(local_name):
# name = uuid.uuid4().__str__().replace("-", "").upper()
# local_name = str(local_name).rsplit(".")
# return "pics/%s.%s" % (name, local_name[-1])
# 随机生成短链
def get_shourt_file_name(local_name, m):
name = url_s(m)
local_name = str(local_name).rsplit(".")
return "pics/%s.%s" % (name, local_name[-1])
BUCKET_NAME = "lyx-tc"
PRE = "http://**.**.**.**:88/img/"
PRE_A = "https://pics.starfishs.cn/"
PRE_B = "https://lyx-tc.oss-cn-shanghai.aliyuncs.com/"
length = 5
PIC_STYLE = "!1"
ENDPOINT = "oss-cn-shanghai.aliyuncs.com"
ACCESS_KEY_ID = "LTA*****zYEiB51g7"
ACCESS_KEY_SECRET = "XMclFPxI9D*****y6rZvw38DIRQE"
addr = "D:/star/400-interest/md/"
print('开始上传...')
src_file = sys.argv[-1] # 获取文件路径
auth = oss2.Auth(ACCESS_KEY_ID, ACCESS_KEY_SECRET)
bucket = oss2.Bucket(auth, ENDPOINT, BUCKET_NAME)
remote_file_name = get_shourt_file_name(src_file, int(length))
bucket.put_object_from_file(remote_file_name, src_file) # 上传文件
result_str = "![](%s%s%s)" % (PRE_B, remote_file_name, PIC_STYLE)
copy(result_str) # 将结果复制到粘贴板方便直接使用
print('上传完成')
print(result_str)
f = open(addr + 'log.txt', 'a')
f.write(src_file + ':' + result_str + '\n')
f.close()
print('写入日志')
if (sys.argv[1] == 'gd'):
copyfile(src_file, addr + remote_file_name)
print("移动至" + addr + remote_file_name)
python3 +
pip install oss2 # 阿里云oss的pythonSDk
pip install pyperclip # 操控剪切板的库
购买过程略
创建bucket
记录下该bucket 的外网访问地址下的EndPoint(地域节点),Bucket 域名(用其分别替换我的代码中的 ENDPOINT,PRE_B)
另外获取访问密钥。替换代码中的值
ACCESS_KEY_ID = "LTAIOWgjgyjKzYEiB51g7"
ACCESS_KEY_SECRET="khjygjgyjkgk"
有自己域名的同学,可以把bucket域名设置为自己的域名(需在阿里云控制台设置),修改PRE_A,并替换PRE_B即可。
设置一个文件夹本地保存图片
addr=D:/pic/
修订版本 | 更新时间 | 更新说明 |
---|---|---|
1 | 2020-02-28 15:28 | 更新了代码 |
0 | 2019-02-16 00:09 |