66import base64
77import os
88import sys
9+ import time
910import copy
1011import json
1112import xml .dom .minidom
2324from .cos_exception import CosClientError
2425from .cos_exception import CosServiceError
2526from .version import __version__
26-
27+ from . select_event_stream import EventStream
2728logger = logging .getLogger (__name__ )
2829
2930
3031class CosConfig (object ):
3132 """config类,保存用户相关信息"""
3233 def __init__ (self , Appid = None , Region = None , SecretId = None , SecretKey = None , Token = None , Scheme = None , Timeout = None ,
3334 Access_id = None , Access_key = None , Secret_id = None , Secret_key = None , Endpoint = None , IP = None , Port = None ,
34- Anonymous = None , UA = None , Proxies = None , Domain = None , ServiceDomain = None ):
35+ Anonymous = None , UA = None , Proxies = None , Domain = None , ServiceDomain = None , PoolConnections = 10 , PoolMaxSize = 10 ):
3536 """初始化,保存用户的信息
3637
3738 :param Appid(string): 用户APPID.
@@ -53,6 +54,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
5354 :param Proxies(dict): 使用代理来访问COS
5455 :param Domain(string): 使用自定义的域名来访问COS
5556 :param ServiceDomain(string): 使用自定义的域名来访问cos service
57+ :param PoolConnections(int): 连接池个数
58+ :param PoolMaxSize(int): 连接池中最大连接数
5659 """
5760 self ._appid = to_unicode (Appid )
5861 self ._token = to_unicode (Token )
@@ -66,6 +69,8 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
6669 self ._proxies = Proxies
6770 self ._domain = Domain
6871 self ._service_domain = ServiceDomain
72+ self ._pool_connections = PoolConnections
73+ self ._pool_maxsize = PoolMaxSize
6974
7075 if self ._domain is None :
7176 self ._endpoint = format_endpoint (Endpoint , Region )
@@ -175,6 +180,8 @@ def __init__(self, conf, retry=1, session=None):
175180 self ._retry = retry # 重试的次数,分片上传时可适当增大
176181 if session is None :
177182 self ._session = requests .session ()
183+ self ._session .mount ('http://' , requests .adapters .HTTPAdapter (pool_connections = self ._conf ._pool_connections , pool_maxsize = self ._conf ._pool_maxsize ))
184+ self ._session .mount ('https://' , requests .adapters .HTTPAdapter (pool_connections = self ._conf ._pool_connections , pool_maxsize = self ._conf ._pool_maxsize ))
178185 else :
179186 self ._session = session
180187
@@ -235,6 +242,8 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
235242 kwargs ['verify' ] = False
236243 for j in range (self ._retry + 1 ):
237244 try :
245+ if j != 0 :
246+ time .sleep (j )
238247 if method == 'POST' :
239248 res = self ._session .post (url , timeout = timeout , proxies = self ._conf ._proxies , ** kwargs )
240249 elif method == 'GET' :
@@ -948,7 +957,7 @@ def restore_object(self, Bucket, Key, RestoreRequest={}, **kwargs):
948957
949958 :param Bucket(string): 存储桶名称.
950959 :param Key(string): COS路径.
951- :param RestoreRequest: 取回object的属性设置
960+ :param RestoreRequest(dict) : 取回object的属性设置
952961 :param kwargs(dict): 设置请求headers.
953962 :return: None.
954963 """
@@ -972,6 +981,46 @@ def restore_object(self, Bucket, Key, RestoreRequest={}, **kwargs):
972981 params = params )
973982 return None
974983
984+ def select_object_content (self , Bucket , Key , Expression , ExpressionType , InputSerialization , OutputSerialization , RequestProgress = None , ** kwargs ):
985+ """从指定文对象中检索内容
986+
987+ :param Bucket(string): 存储桶名称.
988+ :param Key(string): 检索的路径.
989+ :param Expression(string): 查询语句
990+ :param ExpressionType(string): 查询语句的类型
991+ :param RequestProgress(dict): 查询进度设置
992+ :param InputSerialization(dict): 输入格式设置
993+ :param OutputSerialization(dict): 输出格式设置
994+ :param kwargs(dict): 设置请求headers.
995+ :return(dict): 检索内容.
996+ """
997+ params = {'select' : '' , 'select-type' : 2 }
998+ headers = mapped (kwargs )
999+ url = self ._conf .uri (bucket = Bucket , path = Key )
1000+ logger .info ("select object content, url=:{url} ,headers=:{headers}" .format (
1001+ url = url ,
1002+ headers = headers ))
1003+ SelectRequest = {
1004+ 'Expression' : Expression ,
1005+ 'ExpressionType' : ExpressionType ,
1006+ 'InputSerialization' : InputSerialization ,
1007+ 'OutputSerialization' : OutputSerialization
1008+ }
1009+ if RequestProgress is not None :
1010+ SelectRequest ['RequestProgress' ] = RequestProgress
1011+ xml_config = format_xml (data = SelectRequest , root = 'SelectRequest' )
1012+ rt = self .send_request (
1013+ method = 'POST' ,
1014+ url = url ,
1015+ stream = True ,
1016+ bucket = Bucket ,
1017+ data = xml_config ,
1018+ auth = CosS3Auth (self ._conf , Key , params = params ),
1019+ headers = headers ,
1020+ params = params )
1021+ data = {'Payload' : EventStream (rt )}
1022+ return data
1023+
9751024 # s3 bucket interface begin
9761025 def create_bucket (self , Bucket , ** kwargs ):
9771026 """创建一个bucket
@@ -2687,9 +2736,9 @@ def list_buckets(self, **kwargs):
26872736 )
26882737 """
26892738 headers = mapped (kwargs )
2690- url = 'https ://service.cos.myqcloud.com/'
2739+ url = '{scheme} ://service.cos.myqcloud.com/' . format ( scheme = self . _conf . _scheme )
26912740 if self ._conf ._service_domain is not None :
2692- url = 'https ://{domain}/' .format (domain = self ._conf ._service_domain )
2741+ url = '{scheme} ://{domain}/' .format (scheme = self . _conf . _scheme , domain = self ._conf ._service_domain )
26932742 rt = self .send_request (
26942743 method = 'GET' ,
26952744 url = url ,
0 commit comments