Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

上传文件时,可以添加校验参数强制校验key和hash 音频元信息的接口 #365

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions qiniu/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions qiniu/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=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)
return __return_wrapper(r)


class _TokenAuth(AuthBase):
def __init__(self, token):
self.token = token
Expand Down
Empty file.
60 changes: 60 additions & 0 deletions qiniu/services/media/audio.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 8 additions & 1 deletion qiniu/services/storage/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""上传文件到七牛

Expand All @@ -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": "<Hash string>", "key": "<Key string>"}
Expand All @@ -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


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
packages = [
'qiniu',
'qiniu.services',
'qiniu.services.media',
'qiniu.services.storage',
'qiniu.services.processing',
'qiniu.services.compute',
Expand Down