优化包结构!
This commit is contained in:
parent
28e4fbcfcc
commit
fd3b4bc320
3 changed files with 74 additions and 144 deletions
74
test/upload.py
Normal file
74
test/upload.py
Normal file
|
@ -0,0 +1,74 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import json
|
||||
import os
|
||||
|
||||
import oss2
|
||||
import sys
|
||||
|
||||
# 以下代码展示了文件上传的高级用法,如断点续传、分片上传等。
|
||||
|
||||
# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
|
||||
# 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
|
||||
#
|
||||
# 以杭州区域为例,Endpoint可以是:
|
||||
# http://oss-cn-hangzhou.aliyuncs.com
|
||||
# https://oss-cn-hangzhou.aliyuncs.com
|
||||
# 分别以HTTP、HTTPS协议访问。
|
||||
|
||||
# print(os.getcwd())
|
||||
class Upload():
|
||||
def __init__(self,path) :
|
||||
config={}
|
||||
try:
|
||||
with open(path, 'r') as file:
|
||||
config = json.load(file)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
# print(str(config))
|
||||
self.access_key_id = config['OSS_ACCESS_KEY_ID']
|
||||
# print(access_key_id)
|
||||
self.access_key_secret = config['OSS_ACCESS_KEY_SECRET']
|
||||
# print(access_key_secret)
|
||||
self.bucket_name = config['OSS_BUCKET']
|
||||
self.endpoint = config['OSS_ENDPOINT']
|
||||
self.upload_path = config['UPLOAD_PATH'] if config['UPLOAD_PATH'][-1] == '/' else config['UPLOAD_PATH'] + '/'
|
||||
# 在域名后面添加 /
|
||||
self.upload_domain = config['UPLOAD_DOMAIN'] if config['UPLOAD_DOMAIN'][-1] == '/' else config['UPLOAD_DOMAIN'] + '/'
|
||||
# print(upload_domain)
|
||||
|
||||
|
||||
def percentage(self,consumed_bytes, total_bytes):
|
||||
"""进度条回调函数,计算当前完成的百分比
|
||||
|
||||
:param consumed_bytes: 已经上传/下载的数据量
|
||||
:param total_bytes: 总数据量
|
||||
"""
|
||||
if total_bytes:
|
||||
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
|
||||
print('\r{0}% '.format(rate))
|
||||
# sys.stdout.flush()
|
||||
|
||||
def upload(self,filepath):
|
||||
print(filepath)
|
||||
# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
|
||||
bucket = oss2.Bucket(oss2.Auth(self.access_key_id, self.access_key_secret), self.endpoint, self.bucket_name)
|
||||
# 必须以二进制的方式打开文件。
|
||||
# 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
|
||||
with open(filepath, 'rb') as fileobj:
|
||||
# 填写Object完整路径。Object完整路径中不能包含Bucket名称。
|
||||
upload_name = self.upload_path + filepath.split('/')[-1]
|
||||
bucket.put_object(upload_name, fileobj, progress_callback= self.percentage)
|
||||
|
||||
return self.upload_domain + upload_name
|
||||
|
||||
|
||||
|
||||
# 确认上面的参数都填写正确了
|
||||
# try:
|
||||
# for param in (access_key_id, access_key_secret, bucket_name, endpoint, upload_path, upload_domain):
|
||||
# assert '' != param, '请配置上传参数!'
|
||||
# except Exception as e:
|
||||
# print(e)
|
||||
|
||||
# print(upload('/home/liyp/Pictures/壁纸/web-illust_71411811_20181116_203823.png'))
|
|
@ -1,52 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import json
|
||||
import oss2
|
||||
|
||||
# 以下代码展示了文件上传的高级用法,如断点续传、分片上传等。
|
||||
|
||||
# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
|
||||
# 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
|
||||
#
|
||||
# 以杭州区域为例,Endpoint可以是:
|
||||
# http://oss-cn-hangzhou.aliyuncs.com
|
||||
# https://oss-cn-hangzhou.aliyuncs.com
|
||||
# 分别以HTTP、HTTPS协议访问。
|
||||
|
||||
config = {}
|
||||
with open('../QtPicUpload/config.json', 'r') as file:
|
||||
config = json.load(file)
|
||||
# print(str(config))
|
||||
access_key_id = config['OSS_ACCESS_KEY_ID']
|
||||
# print(access_key_id)
|
||||
access_key_secret = config['OSS_ACCESS_KEY_SECRET']
|
||||
# print(access_key_secret)
|
||||
bucket_name = config['OSS_BUCKET']
|
||||
endpoint = config['OSS_ENDPOINT']
|
||||
upload_path = config['UPLOAD_PATH'] if config['UPLOAD_PATH'][-1] == '/' else config['UPLOAD_PATH'] + '/'
|
||||
# 在域名后面添加 /
|
||||
upload_domain = config['UPLOAD_DOMAIN'] if config['UPLOAD_DOMAIN'][-1] == '/' else config['UPLOAD_DOMAIN'] + '/'
|
||||
# print(upload_domain)
|
||||
|
||||
|
||||
# 确认上面的参数都填写正确了
|
||||
try:
|
||||
for param in (access_key_id, access_key_secret, bucket_name, endpoint, upload_path, upload_domain):
|
||||
assert '' != param, '请配置上传参数!'
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
|
||||
def upload(filepath):
|
||||
print(filepath)
|
||||
# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
|
||||
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
|
||||
# 必须以二进制的方式打开文件。
|
||||
# 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
|
||||
with open(filepath, 'rb') as fileobj:
|
||||
# 填写Object完整路径。Object完整路径中不能包含Bucket名称。
|
||||
upload_name = upload_path + filepath.split('/')[-1]
|
||||
bucket.put_object(upload_name, fileobj)
|
||||
|
||||
return upload_domain + upload_name
|
||||
|
||||
# print(upload('/home/liyp/Pictures/壁纸/web-illust_71411811_20181116_203823.png'))
|
|
@ -1,92 +0,0 @@
|
|||
import sys
|
||||
import time
|
||||
import os
|
||||
import qt_material
|
||||
from PyQt5.QtCore import QSize
|
||||
from PyQt5.QtWidgets import *
|
||||
from PyQt5 import QtCore
|
||||
from PyQt5.QtGui import QPixmap
|
||||
|
||||
|
||||
class UploadTip(QWidget):
|
||||
def __init__(self):
|
||||
super(UploadTip, self).__init__()
|
||||
self.pic_show = QLabel()
|
||||
# self.resize(300,500)
|
||||
self.pic_show.setMaximumSize(300, 400)
|
||||
self.pic_show.setPixmap(
|
||||
QPixmap('../images/icon.png'))
|
||||
# .scaled(self.pic_show.size(), aspectRatioMode=QtCore.Qt.KeepAspectRatio)
|
||||
|
||||
self.pic_tip = QLabel('Ctrl + V 粘贴图片')
|
||||
self.pic_tip.setAlignment(QtCore.Qt.AlignCenter)
|
||||
|
||||
self.tip_layout = QVBoxLayout()
|
||||
self.tip_layout.addStretch()
|
||||
self.tip_layout.addWidget(self.pic_show)
|
||||
self.tip_layout.addStretch()
|
||||
|
||||
self.tip_layout.addWidget(self.pic_tip)
|
||||
self.tip_layout.addStretch()
|
||||
|
||||
# layout.setSpacing(0)
|
||||
# 美化风格
|
||||
# self.setStyleSheet(qt_material.apply_stylesheet(self,''))
|
||||
self.createActions()
|
||||
self.setLayout(self.tip_layout)
|
||||
|
||||
def createActions(self):
|
||||
pastAction = QAction(self)
|
||||
pastAction.setShortcut("Ctrl+V")
|
||||
pastAction.triggered.connect(self.pasteData)
|
||||
self.addAction((pastAction)) # Activate QAction
|
||||
|
||||
def setImage(self, path):
|
||||
|
||||
image = QPixmap(path)
|
||||
if image.width() > image.height():
|
||||
scale = self.pic_show.width() / image.width()
|
||||
# print('比例:', scale)
|
||||
|
||||
# width=scale*clipboard.pixmap().width()*scale
|
||||
height = image.height() * scale
|
||||
# print('转换后高度:', height)
|
||||
self.pic_show.setPixmap(image.scaled(QSize(self.pic_show.width(), int(height)))) # 用于粘贴图片
|
||||
else:
|
||||
scale = self.pic_show.height() / image.height()
|
||||
# print('比例:', scale)
|
||||
width = image.width() * scale
|
||||
# print('转换后宽度:', width)
|
||||
self.pic_show.setPixmap(image.scaled(QSize(int(width), self.pic_show.height())))
|
||||
self.pic_show.setAlignment(QtCore.Qt.AlignCenter)
|
||||
|
||||
# self.pic_show.repaint()
|
||||
# pass
|
||||
|
||||
def pasteData(self):
|
||||
clipboard = QApplication.clipboard()
|
||||
mimeData = clipboard.mimeData()
|
||||
if mimeData.hasImage():
|
||||
|
||||
# 根据时间设置图片文件名
|
||||
file_name = time.strftime('%Y-%m-%d-%H%M%S', time.localtime()) + '.png'
|
||||
# 将图片保存到指定位置
|
||||
clipboard.pixmap().save(file_name, 'PNG')
|
||||
|
||||
restore_path = '../'
|
||||
|
||||
self.setImage(restore_path + file_name)
|
||||
os.remove(restore_path + file_name)
|
||||
|
||||
elif mimeData.hasText():
|
||||
path = clipboard.text()
|
||||
self.setImage(path)
|
||||
print("pasted from clipboard")
|
||||
|
||||
# if __name__ == '__main__':
|
||||
# app = QApplication(sys.argv)
|
||||
# qt_material.apply_stylesheet(app, theme='light_teal.xml')
|
||||
# main = UploadTip()
|
||||
# app.setWindowIcon(QIcon('./images/icon.svg'))
|
||||
# main.show()
|
||||
# sys.exit(app.exec_())
|
Loading…
Reference in a new issue