Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A tutorial on how to delete resources in batch by python sdk

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "python sdk batch deletion of resources how to operate tutorial", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn the "python sdk batch deletion of resources how to operate tutorial" it!

Here's a simple note.

From qiniu import build_batch_delete, Auth, BucketManager# need to fill in your Access Key and Secret Key. In your personal center, you have access_key = 'Access_Key'secret_key =' Secret_Key'# to build authentication object Q = Auth (access_key, secret_key) # initialize BucketManagerbucket = BucketManager (Q) # here is your space name bucket_name = "test" # example The file name array keys = ['test.mp4','test1.mp4',' 'test2.mp4'',' 'test3.mp4''] ops = build_batch_delete (bucket_name, keys) ret, info = bucket.batch (ops) print (info) is all the code deleted in batches. The following is a detailed explanation

Enter from the build_batch_delete function and into the build_batch_delete method under the bucket.py file

Bucket.py

Def build_batch_delete (bucket, keys): # bucket here is the bucket_name space name above, keys timely file list return _ one_key_batch ('delete', bucket, keys) def _ one_key_batch (operation, bucket, keys): # here gives a default parameter delete delete operation, return [_ build_op (operation, entry (bucket, key) for key in keys]

Let's take a look at what is done in the entry method

Utils.py

Def entry (bucket Key): "" calculate the data format in Qiniu API: entry specification reference https://developer.qiniu.com/kodo/api/1276/data-format Args: bucket: space name to be operated key: file name to be operated Returns: data format in accordance with Qiniu API specification "" if key is None: return urlsafe_base64_encode " ('{0} '.format (bucket)) else: return urlsafe_base64_encode (' {0}: {1} '.format (bucket) Key) if the key does not exist, the data is the space name. The existence of key does a string formatting, for example: the first key is test.mp4, so the data is "test:test.mp4" and then pass "test:test.mp4" into urlsafe_base64_encode. Let's see what urlsafe_base64_encode does from base64 import urlsafe_b64encodedef urlsafe_base64_encode (data): "" urlsafe's base64 encoding: urlsafe base64 encoding of the provided data. Specification reference: https://developer.qiniu.com/kodo/manual/1231/appendix#1 Args: data: the data to be encoded is generally a string Returns: the encoded string "" ret = urlsafe_b64encode (b (data)) return s (ret) returns data of type bytes, which is passed to the s method and decoded to the string type again.

Compat.py

Def b (data): if isinstance (data, str): return data.encode ('utf-8') return data finally data is passed into b method to encode def s (data): if isinstance (data, bytes): data = data.decode (' utf-8') return data

Finally, the entry function returns' ZGRkZDpkZGRfMzMubXA0' type data. Let's take a look at what the _ build_op method does.

Bucket.py

Def _ build_op (* args): the entry and exit here is ("delete", 'ZGRkZDpkZGRfMzMubXA0') return' / '.join (args) # join operation followed by "delete/ZGRkZDpkZGRfMzMubXA0"

At this point, the build_batch_delete method is finished, and combined with the above example, the method returns a list ["delete/ZGRkZDpkZGRfMzMubXA0", "delete/ZGRkZDpkZGRfMzMubXA0"]

Then look at what's done in bucket.batch (ops).

Def batch (self, operations): "" batch operation: perform multiple resource management operations in a single request. For more information, please see http://developer.qiniu.com/docs/v6/api/reference/rs/batch.html Args: operations: resource management operation array. You can use Returns: a dict variable. The returned result is similar to: [{"code":, "data":}, {"code":}, {"code": "data": {"error": ""},...] an ResponseInfo object "" url ='{0} / batch'.format (config.get_default ('default_rs_host')) return self.__post (url, dict (op=operations))

Config.py

RS_HOST = 'http://rs.qiniu.com' # management operation HostRSF_HOST =' http://rsf.qbox.me' # enumeration operation HostAPI_HOST = 'http://api.qiniu.com' # data processing operation HostUC_HOST =' https://uc.qbox.me' # get spatial information Host_BLOCK_SIZE = 1024 * 1024 * 4 # breakpoint continuation chunk size. This parameter is the API specification. Modification of _ config = {'default_zone': zone.Zone (),' default_rs_host': RS_HOST, 'default_rsf_host': RSF_HOST,' default_api_host': API_HOST, 'default_uc_host': UC_HOST,' connection_timeout': 30, # link timeout of 30s' connection_retries': 3 is not supported. # the number of link retries is 3 'connection_pool': 10, # the number of link pools is 10' default_upload_threshold': 2 * _ BLOCK_SIZE # the critical default value of put_file upload method}

Remove the path from the configuration file for stitching

Get URL = http://rs.qiniu.com/batch

Dict (op=operations) gets {'op': [' delete/ZGRkZDpkZGRfMzMubXA0', 'delete/ZGRkZDpkZGRfMzMubXA0']}

Then the post request is called to send the deletion request and data.

The final return value needs to be mentioned.

Http.py

Def _ _ return_wrapper (resp): if resp.status_code! = 200or resp.headers.get ('XmurReqid') is None: return None, ResponseInfo (resp) resp.encoding =' utf-8' ret = resp.json () if resp.text! =''else {} if ret is None: # json null ret = {} return ret, ResponseInfo (resp)

The response body is also dealt with separately in this file, so the final return is a meta-ancestor, which can be accepted with two parameters.

The above is the Qiniuyun delete operation to do a source code analysis, there is something wrong, please point out.

At this point, I believe that everyone on the "python sdk batch delete resources how to operate tutorial" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report