oss上传

oss上传 公开 已发布

星翔 分享于 2020-02-28 15:19 | 0 | 10
如何安装动作?

适用于
分类(旧)
编程相关 脚本 网络服务



更多信息
分享时间 2020-02-28 15:19
最后更新 2020-02-28 15:19
修订版本 0
用户许可 -未设置-
Quicker版本 1.5.0
动作大小 3.3 KB

分享到

「上传选中文件到阿里云oss」

简介

python 代码如下 保存后修改动作中的文件名
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://****.oss-cn-shanghai.aliyuncs.com/"
length = 5
PIC_STYLE = "!1"
ENDPOINT = "oss-cn-shanghai.aliyuncs.com"
ACCESS_KEY_ID = "LTAI*****g7"
ACCESS_KEY_SECRET = "XMclFP********rZvw38DIRQE"
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)
import time

最近更新

修订版本 更新时间 更新说明
0 2020-02-28 15:19

最近讨论

暂无讨论