From 5e9482ba1adb599bc8a7004cab29f85a8806dfde Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Tue, 17 Sep 2019 15:37:28 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=B6,=E5=8F=AF=E4=BB=A5=E6=B7=BB=E5=8A=A0=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E5=8F=82=E6=95=B0=E5=BC=BA=E5=88=B6=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?key=E5=92=8Chash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/exceptions.py | 11 +++++++++++ qiniu/services/storage/uploader.py | 9 ++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 qiniu/exceptions.py diff --git a/qiniu/exceptions.py b/qiniu/exceptions.py new file mode 100644 index 00000000..d2be115e --- /dev/null +++ b/qiniu/exceptions.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +# Xiang Wang @ 2019-09-17 15:25:42 + + +class QiniuException(Exception): + pass + + +class UploadException(QiniuException): + pass diff --git a/qiniu/services/storage/uploader.py b/qiniu/services/storage/uploader.py index 03729790..5772d1b8 100644 --- a/qiniu/services/storage/uploader.py +++ b/qiniu/services/storage/uploader.py @@ -5,7 +5,9 @@ import time from qiniu import config, Auth -from qiniu.utils import urlsafe_base64_encode, crc32, file_crc32, _file_iter, rfc_from_timestamp +from qiniu import config +from qiniu.exceptions import UploadException +from qiniu.utils import urlsafe_base64_encode, crc32, file_crc32, _file_iter, rfc_from_timestamp, etag from qiniu import http from .upload_progress_recorder import UploadProgressRecorder @@ -47,6 +49,7 @@ def put_data( def put_file(up_token, key, file_path, params=None, mime_type='application/octet-stream', check_crc=False, progress_handler=None, upload_progress_recorder=None, keep_last_modified=False, hostscache_dir=None, + raise_exception=False, part_size=None, version=None, bucket_name=None): """上传文件到七牛 @@ -63,6 +66,7 @@ def put_file(up_token, key, file_path, params=None, version 分片上传版本 目前支持v1/v2版本 默认v1 part_size 分片上传v2必传字段 默认大小为4MB 分片大小范围为1 MB - 1 GB bucket_name 分片上传v2字段 空间名称 + raise_exception: 上传后自动校验key和hash, 如果不一致就报错 Returns: 一个dict变量,类似 {"hash": "", "key": ""} @@ -84,6 +88,9 @@ def put_file(up_token, key, file_path, params=None, ret, info = _form_put(up_token, key, input_stream, params, mime_type, crc, hostscache_dir, progress_handler, file_name, modify_time=modify_time, keep_last_modified=keep_last_modified) + if raise_exception is True: + if (ret["key"] != key) or (ret["hash"] != etag(file_path)): + raise UploadException("数据校验不正确") return ret, info From 406f3c10ec3e895813062f803b2631b1e1a661b6 Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Fri, 6 Mar 2020 16:13:20 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E9=9F=B3=E9=A2=91=E5=85=83=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/__init__.py | 1 + qiniu/http.py | 12 +++++++ qiniu/services/media/__init__.py | 0 qiniu/services/media/audio.py | 60 ++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 qiniu/services/media/__init__.py create mode 100644 qiniu/services/media/audio.py diff --git a/qiniu/__init__.py b/qiniu/__init__.py index 16c52eb8..2ed512d9 100644 --- a/qiniu/__init__.py +++ b/qiniu/__init__.py @@ -17,6 +17,7 @@ from .zone import Zone from .region import Region +from .services.media.audio import AudioManager from .services.storage.bucket import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, \ build_batch_stat, build_batch_delete, build_batch_restoreAr from .services.storage.uploader import put_data, put_file, put_stream diff --git a/qiniu/http.py b/qiniu/http.py index c28028fb..400b3ab7 100644 --- a/qiniu/http.py +++ b/qiniu/http.py @@ -91,6 +91,18 @@ def _get(url, params, auth, headers=None): return __return_wrapper(r) +def _session_get(url, params, auth): + if _session is None: + _init() + try: + r = _session.get( + url, params=params, auth=auth, + timeout=config.get_default('connection_timeout')) + except Exception as e: + return None, ResponseInfo(None, e) + return __return_wrapper(r) + + class _TokenAuth(AuthBase): def __init__(self, token): self.token = token diff --git a/qiniu/services/media/__init__.py b/qiniu/services/media/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/qiniu/services/media/audio.py b/qiniu/services/media/audio.py new file mode 100644 index 00000000..ccd47244 --- /dev/null +++ b/qiniu/services/media/audio.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +# Xiang Wang @ 2020-03-06 16:02:02 + + +from qiniu import http + + +class AudioManager(object): + """音频处理""" + + def __init__(self, auth): + self.auth = auth + + def avinfo(self, url): + """获取一个文件的音视频元信息: + 接口地址: https://developer.qiniu.com/dora/api/1247/audio-and-video-metadata-information-avinfo + + Args: + url: 音视频的url链接 + + Returns: + 一个dict变量, 类似: + { + "streams": [ + { + "index": 0, + "codec_name": "h264", + "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", + "codec_type": "video", + "codec_time_base": "1/30", + "codec_tag_string": "avc1", + "codec_tag": "0x31637661", + "width": 1152, + "height": 864, + ... + }, + { + "index": 1, + "codec_name": "aac", + "codec_long_name": "Advanced Audio Coding", + "codec_type": "audio", + "codec_time_base": "1/44100", + "codec_tag_string": "mp4a", + "codec_tag": "0x6134706d", + ... + } + ], + "format": { + "nb_streams": 2, + "format_name": "mov,mp4,m4a,3gp,3g2,mj2", + "format_long_name": "QuickTime/MPEG-4/Motion JPEG 2000 format", + "start_time": "0.000000", + "duration": "6413.359589", # 注意,duration是字符串 + ... + } + } + 一个ResponseInfo对象 + """ + return http._session_get(url + "?avinfo", {}, self.auth) From e5482830d8015dbac952f567c6b20bd0babdf7ed Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Fri, 6 Mar 2020 16:27:50 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=BB=99setup.py=E6=B7=BB=E5=8A=A0media?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 34badc36..d02643b2 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ packages = [ 'qiniu', 'qiniu.services', + 'qiniu.services.media', 'qiniu.services.storage', 'qiniu.services.processing', 'qiniu.services.compute', From 95f19f78ce32da4a81cfadbcdee6a32a9f1b812d Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Fri, 6 Mar 2020 17:13:16 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix=20bug:=20=E4=BD=BF=E7=94=A8get=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=97=B6=EF=BC=8Cauth=E9=9C=80=E8=A6=81=E8=BD=AC?= =?UTF-8?q?=E5=8C=96=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/http.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiniu/http.py b/qiniu/http.py index 400b3ab7..51cef806 100644 --- a/qiniu/http.py +++ b/qiniu/http.py @@ -96,7 +96,7 @@ def _session_get(url, params, auth): _init() try: r = _session.get( - url, params=params, auth=auth, + url, params=params, auth=qiniu.auth.RequestsAuth(auth) if auth is not None else None, timeout=config.get_default('connection_timeout')) except Exception as e: return None, ResponseInfo(None, e)