99import logging
1010import sys
1111import os
12+ import copy
1213
1314logger = logging .getLogger (__name__ )
1415fs_coding = sys .getfilesystemencoding ()
1516
17+ maplist = {'CacheControl' :'Cache-Control' ,
18+ 'ContentDisposition' :'Content-Disposition' ,
19+ 'ContentEncoding' :'Content-Encoding' ,
20+ 'Expires' :'Expires' ,
21+ 'Metadata' :'x-cos-meta- *' ,
22+ 'ACL' :'x-cos-acl' ,
23+ 'GrantFullControl' :'x-cos-grant-full-control' ,
24+ 'GrantWrite' :'x-cos-grant-write' ,
25+ 'GrantRead' :'x-cos-grant-read' ,
26+ 'StorageClass' :'x-cos-storage-class' ,
27+ }
1628
1729def to_unicode (s ):
1830 if isinstance (s , unicode ):
@@ -43,35 +55,40 @@ def getTagText(root, tag):
4355 if node .nodeType in (node .TEXT_NODE , node .CDATA_SECTION_NODE ):
4456 rc = rc + node .data
4557
58+ def mapped (headers ):
59+ _headers = copy .copy (headers )
60+ for i in headers .keys ():
61+ if i in maplist :
62+ del _headers [i ]
63+ _headers [maplist [i ]] = headers [i ]
64+ return _headers
4665
4766class CosConfig (object ):
4867
49- def __init__ (self , appid , region , bucket , access_id , access_key , part_size = 1 , max_thread = 5 , * args , ** kwargs ):
68+ def __init__ (self , appid , region , access_id , access_key , part_size = 1 , max_thread = 5 , * args , ** kwargs ):
5069 self ._appid = appid
5170 self ._region = region
52- self ._bucket = bucket
5371 self ._access_id = access_id
5472 self ._access_key = access_key
5573 self ._part_size = min (10 , part_size )
5674 self ._max_thread = min (10 , max_thread )
57- logger .info ("config parameter-> appid: {appid}, region: {region}, bucket: {bucket}, part_size: {part_size}, max_thread: {max_thread}" .format (
75+ logger .info ("config parameter-> appid: {appid}, region: {region}, part_size: {part_size}, max_thread: {max_thread}" .format (
5876 appid = appid ,
5977 region = region ,
60- bucket = bucket ,
6178 part_size = part_size ,
6279 max_thread = max_thread ))
6380
64- def uri (self , path = None ):
81+ def uri (self , bucket , path = None ):
6582 if path :
6683 url = u"http://{bucket}-{uid}.{region}.myqcloud.com/{path}" .format (
67- bucket = self . _bucket ,
84+ bucket = to_unicode ( bucket ) ,
6885 uid = self ._appid ,
6986 region = self ._region ,
7087 path = to_unicode (path )
7188 )
7289 else :
7390 url = u"http://{bucket}-{uid}.{region}.myqcloud.com" .format (
74- bucket = self . _bucket ,
91+ bucket = to_unicode ( bucket ) ,
7592 uid = self ._appid ,
7693 region = self ._region
7794 )
@@ -96,32 +113,36 @@ def __init__(self, conf, session=None):
96113 self ._session = session
97114
98115
99- def put_object (self , Body , Key , ** kwargs ):
100- url = self ._conf .uri (path = Key )
116+ def put_object (self , Bucket , Body , Key , ** kwargs ):
117+ headers = mapped (kwargs )
118+ url = self ._conf .uri (bucket = Bucket , path = Key )
101119 for j in range (self ._retry ):
102120 rt = self ._session .put (url = url ,
103- auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ), data = Body , headers = kwargs [ ' headers' ] )
121+ auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ), data = Body , headers = headers )
104122 if rt .status_code == 200 :
105123 break
106124 return rt
107125
108- def get_object (self , Key , ** kwargs ):
109- url = self ._conf .uri (path = Key )
126+ def get_object (self , Bucket , Key , ** kwargs ):
127+ headers = mapped (kwargs )
128+ url = self ._conf .uri (bucket = Bucket , path = Key )
110129 for j in range (self ._retry ):
111- rt = self ._session .get (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = kwargs [ ' headers' ] )
130+ rt = self ._session .get (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = headers )
112131 if rt .status_code == 200 :
113132 break
114133 return rt
115134
116- def delete_object (self , Key , ** kwargs ):
117- url = self ._conf .uri (path = Key )
135+ def delete_object (self , Bucket , Key , ** kwargs ):
136+ headers = mapped (kwargs )
137+ url = self ._conf .uri (bucket = Bucket , path = Key )
118138 for j in range (self ._retry ):
119- rt = self ._session .delete (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = kwargs [ ' headers' ] )
139+ rt = self ._session .delete (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = headers )
120140 if rt .status_code == 204 :
121141 break
122142 return rt
123143
124144
145+
125146class BucketInterface (object ):
126147
127148 def __init__ (self , conf , session = None ):
@@ -135,19 +156,21 @@ def __init__(self, conf, session=None):
135156 else :
136157 self ._session = session
137158
138- def put_bucket (self , ** kwargs ):
139- url = self ._conf .uri (path = '' )
159+ def put_bucket (self , Bucket , ** kwargs ):
160+ headers = mapped (kwargs )
161+ url = self ._conf .uri (bucket = Bucket )
140162 for j in range (self ._retry ):
141163 rt = self ._session .put (url = url ,
142- auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ), headers = kwargs [ ' headers' ] )
164+ auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ), headers = headers )
143165 if rt .status_code == 200 :
144166 break
145167 return rt
146168
147- def delete_bucket (self , Key , ** kwargs ):
148- url = self ._conf .uri (path = '' )
169+ def delete_bucket (self , Bucket , ** kwargs ):
170+ headers = mapped (kwargs )
171+ url = self ._conf .uri (bucket = Bucket )
149172 for j in range (self ._retry ):
150- rt = self ._session .delete (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = kwargs [ ' headers' ] )
173+ rt = self ._session .delete (url = url , auth = CosS3Auth (self ._conf ._access_id , self ._conf ._access_key ),headers = headers )
151174 if rt .status_code == 204 :
152175 break
153176 return rt
0 commit comments