Skip to content

Commit 8156f18

Browse files
authored
Merge pull request #103 from dt3310321/s3
support Inventory and Bucket Tagging
2 parents 9243bde + ea8ceb3 commit 8156f18

File tree

5 files changed

+352
-52
lines changed

5 files changed

+352
-52
lines changed

qcloud_cos/cos_client.py

Lines changed: 264 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ def send_request(self, method, url, bucket, timeout=30, **kwargs):
216216
kwargs['headers'] = format_values(kwargs['headers'])
217217
if 'data' in kwargs:
218218
kwargs['data'] = to_bytes(kwargs['data'])
219+
if self._conf._ip is not None and self._conf._scheme == 'https':
220+
kwargs['verify'] = False
219221
for j in range(self._retry + 1):
220222
try:
221223
if method == 'POST':
@@ -1966,7 +1968,7 @@ def put_bucket_logging(self, Bucket, BucketLoggingStatus={}, **kwargs):
19661968
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
19671969
client = CosS3Client(config)
19681970
# 设置bucket logging服务
1969-
logging_bucket = 'logging-beijing-1252448703'
1971+
logging_bucket = 'logging-beijing-1250000000'
19701972
logging_config = {
19711973
'LoggingEnabled': {
19721974
'TargetBucket': logging_bucket,
@@ -1995,7 +1997,6 @@ def put_bucket_logging(self, Bucket, BucketLoggingStatus={}, **kwargs):
19951997
auth=CosS3Auth(self._conf, params=params),
19961998
headers=headers,
19971999
params=params)
1998-
grant_rt = self.put_bucket_acl(Bucket=Bucket, GrantFullControl=LOGGING_UIN)
19992000
return None
20002001

20012002
def get_bucket_logging(self, Bucket, **kwargs):
@@ -2110,7 +2111,7 @@ def put_bucket_domain(self, Bucket, DomainConfiguration={}, **kwargs):
21102111
"""设置bucket的自定义域名
21112112
21122113
:param Bucket(string): 存储桶名称.
2113-
:param ReplicationConfiguration(dict): 设置Bucket的自定义域名规则.
2114+
:param DomainConfiguration(dict): 设置Bucket的自定义域名规则.
21142115
:param kwargs(dict): 设置请求headers.
21152116
:return: None.
21162117
@@ -2200,15 +2201,15 @@ def delete_bucket_domain(self, Bucket, **kwargs):
22002201
22012202
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
22022203
client = CosS3Client(config)
2203-
# 获取bucket自定义域名配置
2204+
# 删除ucket自定义域名配置
22042205
response = client.delete_bucket_domain(
22052206
Bucket='bucket'
22062207
)
22072208
"""
22082209
headers = mapped(kwargs)
22092210
params = {'domain': ''}
22102211
url = self._conf.uri(bucket=Bucket)
2211-
logger.info("get bucket domain, url=:{url} ,headers=:{headers}".format(
2212+
logger.info("delete bucket domain, url=:{url} ,headers=:{headers}".format(
22122213
url=url,
22132214
headers=headers))
22142215
rt = self.send_request(
@@ -2221,29 +2222,19 @@ def delete_bucket_domain(self, Bucket, **kwargs):
22212222
return None
22222223

22232224
def put_bucket_origin(self, Bucket, OriginConfiguration={}, **kwargs):
2224-
"""设置bucket的回源
2225+
"""设置bucket的回源规则
22252226
22262227
:param Bucket(string): 存储桶名称.
2227-
:param ReplicationConfiguration(dict): 设置Bucket的回源规则.
2228+
:param OriginConfiguration(dict): 设置Bucket的回源规则.
22282229
:param kwargs(dict): 设置请求headers.
22292230
:return: None.
22302231
22312232
.. code-block:: python
22322233
22332234
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
22342235
client = CosS3Client(config)
2235-
# 设置bucket自定义域名配置
2236-
origin_config = {
2237-
'OriginRule': [
2238-
{
2239-
'OriginType': 'Redirect',
2240-
'OriginInfo': {
2241-
'HostName': 'www.abc.com',
2242-
'Protocol': 'HTTP'
2243-
}
2244-
},
2245-
]
2246-
}
2236+
# 设置bucket回源规则
2237+
origin_config = {}
22472238
response = client.put_bucket_origin(
22482239
Bucket='bucket',
22492240
OriginConfiguration=origin_config
@@ -2274,13 +2265,13 @@ def get_bucket_origin(self, Bucket, **kwargs):
22742265
22752266
:param Bucket(string): 存储桶名称.
22762267
:param kwargs(dict): 设置请求headers.
2277-
:return(dict): Bucket对应的自定义域名配置.
2268+
:return(dict): Bucket对应的回源规则.
22782269
22792270
.. code-block:: python
22802271
22812272
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
22822273
client = CosS3Client(config)
2283-
# 获取bucket自定义域名配置
2274+
# 获取bucket回源规则
22842275
response = client.get_bucket_origin(
22852276
Bucket='bucket'
22862277
)
@@ -2313,15 +2304,265 @@ def delete_bucket_origin(self, Bucket, **kwargs):
23132304
23142305
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
23152306
client = CosS3Client(config)
2316-
# 获取bucket自定义域名配置
2307+
# 删除bucket回源规则
23172308
response = client.delete_bucket_origin(
23182309
Bucket='bucket'
23192310
)
23202311
"""
23212312
headers = mapped(kwargs)
23222313
params = {'origin': ''}
23232314
url = self._conf.uri(bucket=Bucket)
2324-
logger.info("get bucket origin, url=:{url} ,headers=:{headers}".format(
2315+
logger.info("delete bucket origin, url=:{url} ,headers=:{headers}".format(
2316+
url=url,
2317+
headers=headers))
2318+
rt = self.send_request(
2319+
method='DELETE',
2320+
url=url,
2321+
bucket=Bucket,
2322+
auth=CosS3Auth(self._conf, params=params),
2323+
headers=headers,
2324+
params=params)
2325+
return None
2326+
2327+
def put_bucket_inventory(self, Bucket, Id, InventoryConfiguration={}, **kwargs):
2328+
"""设置bucket的清单规则
2329+
2330+
:param Bucket(string): 存储桶名称.
2331+
:param Id(string): 清单规则名称.
2332+
:param InventoryConfiguration(dict): Bucket的清单规则.
2333+
:param kwargs(dict): 设置请求headers.
2334+
:return: None.
2335+
2336+
.. code-block:: python
2337+
2338+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2339+
client = CosS3Client(config)
2340+
# 设置bucket清单规则
2341+
inventory_config = {
2342+
'Destination': {
2343+
'COSBucketDestination': {
2344+
'AccountId': '100000000001',
2345+
'Bucket': 'qcs::cos:ap-guangzhou::examplebucket-1250000000',
2346+
'Format': 'CSV',
2347+
'Prefix': 'list1',
2348+
'Encryption': {
2349+
'SSECOS': {}
2350+
}
2351+
},
2352+
'IsEnabled': 'True',
2353+
'Filter': {
2354+
'Prefix': 'filterPrefix'
2355+
},
2356+
'IncludedObjectVersions':'All',
2357+
'OptionalFields': {
2358+
'Field': [
2359+
'Size',
2360+
'LastModifiedDate',
2361+
'ETag',
2362+
'StorageClass',
2363+
'IsMultipartUploaded',
2364+
'ReplicationStatus'
2365+
]
2366+
},
2367+
'Schedule': {
2368+
'Frequency': 'Daily'
2369+
}
2370+
}
2371+
response = client.put_bucket_inventory(
2372+
Bucket='bucket',
2373+
Id='list1',
2374+
InventoryConfiguration=inventory_config
2375+
)
2376+
"""
2377+
lst = ['<Field>', '</Field>'] # 类型为list的标签
2378+
InventoryConfiguration['Id'] = Id
2379+
xml_config = format_xml(data=InventoryConfiguration, root='InventoryConfiguration', lst=lst)
2380+
headers = mapped(kwargs)
2381+
headers['Content-MD5'] = get_md5(xml_config)
2382+
headers['Content-Type'] = 'application/xml'
2383+
params = {'inventory': '', 'id': Id}
2384+
url = self._conf.uri(bucket=Bucket)
2385+
logger.info("put bucket inventory, url=:{url} ,headers=:{headers}".format(
2386+
url=url,
2387+
headers=headers))
2388+
rt = self.send_request(
2389+
method='PUT',
2390+
url=url,
2391+
bucket=Bucket,
2392+
data=xml_config,
2393+
auth=CosS3Auth(self._conf, params=params),
2394+
headers=headers,
2395+
params=params)
2396+
return None
2397+
2398+
def get_bucket_inventory(self, Bucket, Id, **kwargs):
2399+
"""获取bucket清单规则
2400+
2401+
:param Bucket(string): 存储桶名称.
2402+
:param Id(string): 清单规则名称.
2403+
:param kwargs(dict): 设置请求headers.
2404+
:return(dict): Bucket对应的清单规则.
2405+
2406+
.. code-block:: python
2407+
2408+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2409+
client = CosS3Client(config)
2410+
# 获取bucket清单规则
2411+
response = client.get_bucket_inventory(
2412+
Bucket='bucket'
2413+
)
2414+
"""
2415+
headers = mapped(kwargs)
2416+
params = {'inventory': '', 'id': Id}
2417+
url = self._conf.uri(bucket=Bucket)
2418+
logger.info("get bucket inventory, url=:{url} ,headers=:{headers}".format(
2419+
url=url,
2420+
headers=headers))
2421+
rt = self.send_request(
2422+
method='GET',
2423+
url=url,
2424+
bucket=Bucket,
2425+
auth=CosS3Auth(self._conf, params=params),
2426+
headers=headers,
2427+
params=params)
2428+
data = xml_to_dict(rt.content)
2429+
format_dict(data['OptionalFields'], ['Field'])
2430+
return data
2431+
2432+
def delete_bucket_inventory(self, Bucket, Id, **kwargs):
2433+
"""删除bucket 回源配置
2434+
2435+
:param Bucket(string): 存储桶名称.
2436+
:param Id(string): 清单规则名称.
2437+
:param kwargs(dict): 设置请求headers.
2438+
:return(dict): None.
2439+
2440+
.. code-block:: python
2441+
2442+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2443+
client = CosS3Client(config)
2444+
# 删除bucket清单规则
2445+
response = client.delete_bucket_origin(
2446+
Bucket='bucket'
2447+
)
2448+
"""
2449+
headers = mapped(kwargs)
2450+
params = {'inventory': '', 'id': Id}
2451+
url = self._conf.uri(bucket=Bucket)
2452+
logger.info("delete bucket inventory, url=:{url} ,headers=:{headers}".format(
2453+
url=url,
2454+
headers=headers))
2455+
rt = self.send_request(
2456+
method='DELETE',
2457+
url=url,
2458+
bucket=Bucket,
2459+
auth=CosS3Auth(self._conf, params=params),
2460+
headers=headers,
2461+
params=params)
2462+
return None
2463+
2464+
def put_bucket_tagging(self, Bucket, Tagging={}, **kwargs):
2465+
"""设置bucket的标签
2466+
2467+
:param Bucket(string): 存储桶名称.
2468+
:param Tagging(dict): Bucket的标签集合
2469+
:param kwargs(dict): 设置请求headers.
2470+
:return: None.
2471+
2472+
.. code-block:: python
2473+
2474+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2475+
client = CosS3Client(config)
2476+
# 设置bucket标签
2477+
tagging_set = {
2478+
'TagSet': {
2479+
'Tag': [
2480+
{
2481+
'Key': 'string',
2482+
'Value': 'string'
2483+
}
2484+
]
2485+
}
2486+
}
2487+
response = client.put_bucket_tagging(
2488+
Bucket='bucket',
2489+
Tagging=tagging_set
2490+
)
2491+
"""
2492+
lst = ['<Tag>', '</Tag>'] # 类型为list的标签
2493+
xml_config = format_xml(data=Tagging, root='Tagging', lst=lst)
2494+
headers = mapped(kwargs)
2495+
headers['Content-MD5'] = get_md5(xml_config)
2496+
headers['Content-Type'] = 'application/xml'
2497+
params = {'tagging': ''}
2498+
url = self._conf.uri(bucket=Bucket)
2499+
logger.info("put bucket tagging, url=:{url} ,headers=:{headers}".format(
2500+
url=url,
2501+
headers=headers))
2502+
rt = self.send_request(
2503+
method='PUT',
2504+
url=url,
2505+
bucket=Bucket,
2506+
data=xml_config,
2507+
auth=CosS3Auth(self._conf, params=params),
2508+
headers=headers,
2509+
params=params)
2510+
return None
2511+
2512+
def get_bucket_tagging(self, Bucket, **kwargs):
2513+
"""获取bucket标签
2514+
2515+
:param Bucket(string): 存储桶名称.
2516+
:param kwargs(dict): 设置请求headers.
2517+
:return(dict): Bucket对应的标签.
2518+
2519+
.. code-block:: python
2520+
2521+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2522+
client = CosS3Client(config)
2523+
# 获取bucket标签
2524+
response = client.get_bucket_tagging(
2525+
Bucket='bucket'
2526+
)
2527+
"""
2528+
headers = mapped(kwargs)
2529+
params = {'tagging': ''}
2530+
url = self._conf.uri(bucket=Bucket)
2531+
logger.info("get bucket tagging, url=:{url} ,headers=:{headers}".format(
2532+
url=url,
2533+
headers=headers))
2534+
rt = self.send_request(
2535+
method='GET',
2536+
url=url,
2537+
bucket=Bucket,
2538+
auth=CosS3Auth(self._conf, params=params),
2539+
headers=headers,
2540+
params=params)
2541+
data = xml_to_dict(rt.content)
2542+
if 'TagSet' in data:
2543+
format_dict(data['TagSet'], ['Tag'])
2544+
return data
2545+
2546+
def delete_bucket_tagging(self, Bucket, **kwargs):
2547+
"""删除bucket 回源配置
2548+
2549+
:param Bucket(string): 存储桶名称.
2550+
:param kwargs(dict): 设置请求headers.
2551+
:return(dict): None.
2552+
2553+
.. code-block:: python
2554+
2555+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2556+
client = CosS3Client(config)
2557+
# 删除bucket标签
2558+
response = client.delete_bucket_tagging(
2559+
Bucket='bucket'
2560+
)
2561+
"""
2562+
headers = mapped(kwargs)
2563+
params = {'tagging': ''}
2564+
url = self._conf.uri(bucket=Bucket)
2565+
logger.info("delete bucket tagging, url=:{url} ,headers=:{headers}".format(
23252566
url=url,
23262567
headers=headers))
23272568
rt = self.send_request(

qcloud_cos/cos_comm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
SINGLE_UPLOAD_LENGTH = 5*1024*1024*1024 # 单次上传文件最大为5GB
2020
DEFAULT_CHUNK_SIZE = 1024*1024 # 计算MD5值时,文件单次读取的块大小为1MB
21-
LOGGING_UIN = 'id="qcs::cam::uin/100001001014:uin/100001001014"'
2221
# kwargs中params到http headers的映射
2322
maplist = {
2423
'ContentLength': 'Content-Length',
@@ -377,7 +376,7 @@ def format_dict(data, key_lst):
377376
return data
378377
for key in key_lst:
379378
# 将dict转为list,保持一致
380-
if key in data and isinstance(data[key], dict):
379+
if key in data and (isinstance(data[key], dict) or isinstance(data[key], str)):
381380
lst = []
382381
lst.append(data[key])
383382
data[key] = lst

qcloud_cos/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = '5.1.7.5'
2+
__version__ = '5.1.7.6'

0 commit comments

Comments
 (0)