Skip to content

Commit c98489b

Browse files
authored
Merge pull request #38 from Yulegu-bj/master
Head请求添加X-Ufile-restore头信息
2 parents 937d3d1 + b5d272f commit c98489b

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

examples/example_opMeta.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# !/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
from ufile import filemanager
4+
5+
bucket = '' # 空间名称
6+
key = '' # 文件名称
7+
public_key = '' # 账户公钥
8+
private_key = '' # 账户私钥
9+
metakey = 'mimetype' # 自定义元数据键
10+
metavalue = 'text/plain' # 自定义元数据值
11+
12+
# 设置上传host后缀,外网可用后缀形如 .cn-bj.ufileos.com(cn-bj为北京地区,其他地区具体后缀可见控制台:对象存储-单地域空间管理-存储空间域名)
13+
# 默认值为'.cn-bj.ufileos.com',如果上传文件的bucket所在地域不在北京,请务必设置此项
14+
upload_suffix = 'YOUR_UPLOAD_SUFFIX'
15+
16+
file = filemanager.FileManager(public_key, private_key, upload_suffix)
17+
18+
# 设置文件元数据
19+
ret, resp = file.opMeta(bucket, key, metakey, metavalue)
20+
assert resp.status_code == 200, resp.error
21+
print('setfilemetakey success')

ufile/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
UCloud UFile SDK for python
55
"""
66

7-
__version__ = '3.2.8'
7+
__version__ = '3.2.9'

ufile/filemanager.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from .compact import b, s, u, url_parse, quote
1111
from .config import BLOCKSIZE
1212
from .httprequest import _put_stream, _put_file, _post_file, ResponseInfo, _uploadhit_file, _download_file, \
13-
_delete_file, _getfilelist, _head_file, _restore_file, _classswitch_file, _copy_file, _rename_file, _listobjects
13+
_delete_file, _getfilelist, _head_file, _restore_file, _classswitch_file, _copy_file, _rename_file, _listobjects, \
14+
_op_meta
1415
from .logger import logger
1516
from .util import _check_dict, ufile_put_url, ufile_post_url, file_etag, ufile_uploadhit_url, ufile_getfilelist_url, \
1617
mimetype_from_file, ufile_restore_url, ufile_classswitch_url, ufile_copy_url, ufile_rename_url, \
17-
ufile_listobjects_url
18+
ufile_listobjects_url, ufile_op_meta_url
1819

1920

2021
class FileManager(BaseUFile):
@@ -535,3 +536,35 @@ def listobjects(self, bucket, prefix=None, marker=None, maxkeys=None, delimiter=
535536
logger.info(info_message)
536537
url = ufile_listobjects_url(bucket, upload_suffix=self.__upload_suffix)
537538
return _listobjects(url, header, param)
539+
540+
def opMeta(self, bucket, key, metak, metav, op="set", header=None):
541+
"""
542+
获取文件元数据
543+
544+
:param bucket: string 类型,空间名称
545+
:param key: string 类型,文件在空间中的名称
546+
:param metak: string 类型,元数据的key
547+
:param metav: string 类型,元数据的value
548+
:param op: string 类型,操作类型,目前只支持set
549+
:param header: dict类型,http 请求header,键值对类型分别为string,比如{'User-Agent': 'Google Chrome'}
550+
:return: ret: 如果http状态码为[200, 204, 206]之一则返回None,否则如果服务器返回json信息则返回dict类型,键值对类型分别为string, unicode string类型,否则返回空的dict
551+
:return: ResponseInfo: 响应的具体信息,UCloud UFile 服务器返回信息或者网络链接异常
552+
"""
553+
if header is None:
554+
header = dict()
555+
else:
556+
_check_dict(header)
557+
558+
if 'User-Agent' not in header:
559+
header['User-Agent'] = config.get_default('user_agent')
560+
header['Content-Type'] = 'application/json'
561+
562+
auth = self.authorization('post', bucket, key, header, action="opmeta")
563+
header['Authorization'] = auth
564+
data = {
565+
'op': op,
566+
'metak': metak,
567+
'metav': metav
568+
}
569+
url = ufile_op_meta_url(bucket, key, upload_suffix=self.__upload_suffix)
570+
return _op_meta(url, json.dumps(data), header)

ufile/httprequest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,21 @@ def _get_multi_upload_part(url, header):
343343
return __return_wraper(response)
344344

345345

346+
def _op_meta(url, body, header):
347+
"""
348+
UCloud UFile 文件元数据操作请求
349+
350+
:param url: String类型,文件元数据操作请求的url
351+
:param body: dict类型,键值对类型分别为string类型,HTTP请求的body信息
352+
:param header: dict类型,键值对类型分别为string类型,HTTP请求头信息
353+
"""
354+
try:
355+
response = requests.post(url, headers=header, data=body, timeout=config.get_default('connection_timeout'))
356+
except RequestException as e:
357+
return None, ResponseInfo(None, e)
358+
return __return_wraper(response)
359+
360+
346361
class ResponseInfo(object):
347362
"""
348363
UCloud UFile 服务器返回信息,解析UCloud UFile服务器返回信息以及网络连接问题
@@ -382,6 +397,7 @@ def __init__(self, response, exception=None, content_consumed=False):
382397
self.ret_code = None
383398
self.content = None
384399
self.md5 = None
400+
self.x_ufile_restore = None
385401
else:
386402
self.status_code = response.status_code
387403
self.x_session_id = response.headers.get('X-SessionId')
@@ -390,6 +406,7 @@ def __init__(self, response, exception=None, content_consumed=False):
390406
self.content_length = response.headers.get('Content-Length')
391407
content_length = response.headers.get('content-range')
392408
self.content = None if content_consumed else response.content
409+
self.x_ufile_restore = response.headers.get('X-Ufile-restore')
393410
if content_length is not None:
394411
byteslist = re.split('[- /]', content_length)
395412
self.content_range = (int(byteslist[1]), int(byteslist[2]))

ufile/util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,17 @@ def ufile_listparts_url(bucket, upload_suffix, upload_id, max_parts=None, part_n
542542
return url
543543

544544

545+
def ufile_op_meta_url(bucket, key, upload_suffix=None):
546+
"""
547+
文件元数据的url
548+
549+
:param bucket: string类型, 待创建的空间名称
550+
:param key: string类型, 在空间中的文件名
551+
:return: string类型, 获取文件元数据的url
552+
"""
553+
return 'http://{0}{1}/{2}?opmeta'.format(bucket, upload_suffix or config.get_default('upload_suffix'), key)
554+
555+
545556
def deprecated(message):
546557
def deprecated_decorator(func):
547558
def deprecated_func(*args, **kwargs):

0 commit comments

Comments
 (0)