完整项目代码见GitHub
本项目教程见CSDN博客
pip install oss2
import oss2
import time
class RunOSS:
def __init__(self, AccessKeyId=None, AccessKeySecret=None, Endpoint=None, BuckerName=None, filename=None):
self.AccessKeyId = AccessKeyId
self.AccessKeySecret = AccessKeySecret
self.Endpoint = Endpoint # Endpoint(香港, 其他地区根据实际情况修改): 'http://oss-cn-hongkong.aliyuncs.com'
self.BuckerName = BuckerName
self.filename = filename or str(int(time.time()))
def getBucket(self):
auth = oss2.Auth(self.AccessKeyId, self.AccessKeySecret)
bucket = oss2.Bucket(auth, self.Endpoint, self.BuckerName)
return bucket
def uploadFIle(self, objectfile, isFile=True):
bucket = self.getBucket()
if isinstance(objectfile, str):
objectname = self.filename+'.'+objectfile.split('.')[-1]
else:
objectname = self.filename
try:
if isFile:
with open(objectfile, mode='rb') as f:
ret = bucket.put_object(objectname, f)
else:
ret = bucket.put_object(objectname, objectfile)
link = "http://{}.oss-cn-hongkong.aliyuncs.com/".format(self.BuckerName) + objectname
return {'status': ret.status, 'link': link}
except Exception as e:
return e
...
import json
from flask import Flask, request, jsonify, Response
from aliyunOSSRun import RunOSS
# Flask对象
app = Flask(__name__)
def response_headers(content):
resp = Response(content)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
@app.route('/oss_upload/', methods=['POST'])
def upload_oss_file():
if request.files.get('objectfile'):
objectfile = request.files['objectfile']
filename = str(request.args.get('filename', ""))
ret = RunOSS(filename=filename).uploadFIle(objectfile=objectfile, isFile=False)
print(ret[key] for key in ret)
return jsonify(ret)
else:
content = json.dumps({"error_code": "1001"})
resp = response_headers(content)
return resp
if __name__ == '__main__':
app.run(debug=False, port=5000, host='0.0.0.0') # 启动服务
可以在阿里云服务器上后台开启该任务, 用于接收OSS上传任务
requests.exceptions.ConnectionError: ('Connection aborted.', BrokenPipeError(32, 'Broken pipe'))
使用Flask+gevent异步WEB架构解决
代码修改如下:
import json
from flask import Flask, request, jsonify, Response
from gevent import pywsgi
from gevent import monkey
from aliyunOSSRun import RunOSS
monkey.patch_all()
# Flask对象
app = Flask(__name__)
...
if __name__ == '__main__':
port = 5000
server = pywsgi.WSGIServer(('0.0.0.0', port), app)
server.serve_forever()
import sys
import time
import requests
import json
class WindowsOSSUpload:
def __init__(self, filename=None, ip=None, port=None):
self.ip = ip
self.port = port
self.url = None
self.filename = filename
def winUpload(self):
timestr = str(int(time.time()))
objectfile = sys.argv[1]
file = objectfile.split("\\")[-1]
filename = ".".join(file.split(".")[:-1]) + "_" + timestr + "." + file.split(".")[-1]
self.filename = self.filename or filename
self.url = "http://{}:{}/oss_upload?filename={}".format(self.ip, self.port, self.filename)
files = {'objectfile': (filename, open(objectfile, 'rb'), 'application/octet-stream')}
res = requests.post(url=self.url, files=files)
rest = json.loads(res.text)
print(rest['link'])
input() # 用于cmd窗口停留
if __name__ == '__main__':
ip = "xxx.xxx.xxx.xxx"
port = "xxxx"
WindowsOSSUpload(ip=ip, port=port).winUpload()
其中
sys.argv[1]
用于读取cmd中传递的参数, 该处是获取待上传文件的路径
最后加input(), 防止cmd窗口自动关闭
Win + R
打开命令行, 输入regedit
打开注册表HKEY_CLASSES_ROOT\*\shell
目录, 新建name
项, 命名即为右键显示的任务名称name
项目录, 新建command
项command
项目录, 新建一个字符串值
D:\Anaconda3\envs\pyenv35\python D:\OSSRun\winOSSUpload.py %1