You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xml_utils.py 90KB

1 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064
  1. # -*- coding: utf-8 -*-
  2. """
  3. oss2.xml_utils
  4. ~~~~~~~~~~~~~~
  5. XML处理相关。
  6. 主要包括两类接口:
  7. - parse_开头的函数:用来解析服务器端返回的XML
  8. - to_开头的函数:用来生成发往服务器端的XML
  9. """
  10. import xml.etree.ElementTree as ElementTree
  11. from .models import (SimplifiedObjectInfo,
  12. SimplifiedBucketInfo,
  13. PartInfo,
  14. MultipartUploadInfo,
  15. LifecycleRule,
  16. LifecycleExpiration,
  17. CorsRule,
  18. LiveChannelInfoTarget,
  19. LiveChannelInfo,
  20. LiveRecord,
  21. LiveChannelVideoStat,
  22. LiveChannelAudioStat,
  23. Owner,
  24. AccessControlList,
  25. AbortMultipartUpload,
  26. StorageTransition,
  27. Tagging,
  28. TaggingRule,
  29. ServerSideEncryptionRule,
  30. ObjectVersionInfo,
  31. DeleteMarkerInfo,
  32. BatchDeleteObjectVersionResult,
  33. RoutingRule,
  34. Condition,
  35. ConditionInlcudeHeader,
  36. Redirect,
  37. RedirectMirrorHeaders,
  38. MirrorHeadersSet,
  39. REDIRECT_TYPE_MIRROR,
  40. REDIRECT_TYPE_EXTERNAL,
  41. REDIRECT_TYPE_INTERNAL,
  42. REDIRECT_TYPE_ALICDN,
  43. NoncurrentVersionStorageTransition,
  44. NoncurrentVersionExpiration,
  45. AsyncFetchTaskConfiguration,
  46. InventoryConfiguration,
  47. InventoryFilter,
  48. InventorySchedule,
  49. InventoryDestination,
  50. InventoryBucketDestination,
  51. InventoryServerSideEncryptionKMS,
  52. InventoryServerSideEncryptionOSS,
  53. LocationTransferType,
  54. BucketReplicationProgress,
  55. CnameInfo,
  56. CertificateInfo,
  57. ReplicationRule,
  58. MetaQueryFile,
  59. AggregationsInfo,
  60. OSSTaggingInfo,
  61. OSSUserMetaInfo,
  62. AggregationGroupInfo,
  63. AccessMonitorInfo,
  64. LifecycleFilter,
  65. FilterNot,
  66. FilterNotTag,
  67. BucketStyleInfo,
  68. RegionInfo,
  69. CallbackPolicyInfo)
  70. from .select_params import (SelectJsonTypes, SelectParameters)
  71. from .compat import urlunquote, to_unicode, to_string
  72. from .utils import iso8601_to_unixtime, date_to_iso8601, iso8601_to_date
  73. from . import utils
  74. import base64
  75. from .exceptions import SelectOperationClientError
  76. def _find_tag(parent, path):
  77. child = parent.find(path)
  78. if child is None:
  79. raise RuntimeError("parse xml: " + path + " could not be found under " + parent.tag)
  80. if child.text is None:
  81. return ''
  82. return to_string(child.text)
  83. def _find_tag_with_default(parent, path, default_value):
  84. child = parent.find(path)
  85. if child is None:
  86. return default_value
  87. if child.text is None:
  88. return ''
  89. return to_string(child.text)
  90. def _find_bool(parent, path):
  91. text = _find_tag(parent, path)
  92. if text == 'true':
  93. return True
  94. elif text == 'false':
  95. return False
  96. else:
  97. raise RuntimeError("parse xml: value of " + path + " is not a boolean under " + parent.tag)
  98. def _find_int(parent, path):
  99. return int(_find_tag(parent, path))
  100. def _find_object(parent, path, url_encoded):
  101. name = _find_tag(parent, path)
  102. if url_encoded:
  103. return urlunquote(name)
  104. else:
  105. return name
  106. def _find_all_tags(parent, tag):
  107. return [to_string(node.text) or '' for node in parent.findall(tag)]
  108. def _is_url_encoding(root):
  109. node = root.find('EncodingType')
  110. if node is not None and to_string(node.text) == 'url':
  111. return True
  112. else:
  113. return False
  114. def _node_to_string(root):
  115. return ElementTree.tostring(root, encoding='utf-8')
  116. def _add_node_list(parent, tag, entries):
  117. for e in entries:
  118. _add_text_child(parent, tag, e)
  119. def _add_text_child(parent, tag, text):
  120. ElementTree.SubElement(parent, tag).text = to_unicode(text)
  121. def _add_node_child(parent, tag):
  122. return ElementTree.SubElement(parent, tag)
  123. def parse_list_objects(result, body):
  124. root = ElementTree.fromstring(body)
  125. url_encoded = _is_url_encoding(root)
  126. result.is_truncated = _find_bool(root, 'IsTruncated')
  127. if result.is_truncated:
  128. result.next_marker = _find_object(root, 'NextMarker', url_encoded)
  129. for contents_node in root.findall('Contents'):
  130. owner = None
  131. if contents_node.find("Owner") is not None:
  132. owner = Owner(_find_tag(contents_node, 'Owner/DisplayName'), _find_tag(contents_node, 'Owner/ID'))
  133. result.object_list.append(SimplifiedObjectInfo(
  134. _find_object(contents_node, 'Key', url_encoded),
  135. iso8601_to_unixtime(_find_tag(contents_node, 'LastModified')),
  136. _find_tag(contents_node, 'ETag').strip('"'),
  137. _find_tag(contents_node, 'Type'),
  138. int(_find_tag(contents_node, 'Size')),
  139. _find_tag(contents_node, 'StorageClass'),
  140. owner,
  141. _find_tag_with_default(contents_node, 'RestoreInfo', None)
  142. ))
  143. for prefix_node in root.findall('CommonPrefixes'):
  144. result.prefix_list.append(_find_object(prefix_node, 'Prefix', url_encoded))
  145. return result
  146. def parse_list_objects_v2(result, body):
  147. root = ElementTree.fromstring(body)
  148. url_encoded = _is_url_encoding(root)
  149. result.is_truncated = _find_bool(root, 'IsTruncated')
  150. if result.is_truncated:
  151. result.next_continuation_token = _find_object(root, 'NextContinuationToken', url_encoded)
  152. for contents_node in root.findall('Contents'):
  153. owner = None
  154. if contents_node.find("Owner") is not None:
  155. owner = Owner(_find_tag(contents_node, 'Owner/DisplayName'), _find_tag(contents_node, 'Owner/ID'))
  156. result.object_list.append(SimplifiedObjectInfo(
  157. _find_object(contents_node, 'Key', url_encoded),
  158. iso8601_to_unixtime(_find_tag(contents_node, 'LastModified')),
  159. _find_tag(contents_node, 'ETag').strip('"'),
  160. _find_tag(contents_node, 'Type'),
  161. int(_find_tag(contents_node, 'Size')),
  162. _find_tag(contents_node, 'StorageClass'),
  163. owner,
  164. _find_tag_with_default(contents_node, 'RestoreInfo', None)
  165. ))
  166. for prefix_node in root.findall('CommonPrefixes'):
  167. result.prefix_list.append(_find_object(prefix_node, 'Prefix', url_encoded))
  168. return result
  169. def parse_list_buckets(result, body):
  170. root = ElementTree.fromstring(body)
  171. if root.find('IsTruncated') is None:
  172. result.is_truncated = False
  173. else:
  174. result.is_truncated = _find_bool(root, 'IsTruncated')
  175. if result.is_truncated:
  176. result.next_marker = _find_tag(root, 'NextMarker')
  177. if root.find('Owner') is not None:
  178. result.owner = Owner(_find_tag_with_default(root, 'Owner/DisplayName', None), _find_tag_with_default(root, 'Owner/ID', None))
  179. for bucket_node in root.findall('Buckets/Bucket'):
  180. result.buckets.append(SimplifiedBucketInfo(
  181. _find_tag(bucket_node, 'Name'),
  182. _find_tag(bucket_node, 'Location'),
  183. iso8601_to_unixtime(_find_tag(bucket_node, 'CreationDate')),
  184. _find_tag(bucket_node, 'ExtranetEndpoint'),
  185. _find_tag(bucket_node, 'IntranetEndpoint'),
  186. _find_tag(bucket_node, 'StorageClass'),
  187. _find_tag_with_default(bucket_node, 'Region', None),
  188. _find_tag_with_default(bucket_node, 'ResourceGroupId', None),
  189. ))
  190. return result
  191. def parse_init_multipart_upload(result, body):
  192. root = ElementTree.fromstring(body)
  193. result.upload_id = _find_tag(root, 'UploadId')
  194. return result
  195. def parse_list_multipart_uploads(result, body):
  196. root = ElementTree.fromstring(body)
  197. url_encoded = _is_url_encoding(root)
  198. result.is_truncated = _find_bool(root, 'IsTruncated')
  199. result.next_key_marker = _find_object(root, 'NextKeyMarker', url_encoded)
  200. result.next_upload_id_marker = _find_tag(root, 'NextUploadIdMarker')
  201. for upload_node in root.findall('Upload'):
  202. result.upload_list.append(MultipartUploadInfo(
  203. _find_object(upload_node, 'Key', url_encoded),
  204. _find_tag(upload_node, 'UploadId'),
  205. iso8601_to_unixtime(_find_tag(upload_node, 'Initiated'))
  206. ))
  207. for prefix_node in root.findall('CommonPrefixes'):
  208. result.prefix_list.append(_find_object(prefix_node, 'Prefix', url_encoded))
  209. return result
  210. def parse_list_parts(result, body):
  211. root = ElementTree.fromstring(body)
  212. result.is_truncated = _find_bool(root, 'IsTruncated')
  213. result.next_marker = _find_tag(root, 'NextPartNumberMarker')
  214. for part_node in root.findall('Part'):
  215. result.parts.append(PartInfo(
  216. _find_int(part_node, 'PartNumber'),
  217. _find_tag(part_node, 'ETag').strip('"'),
  218. size=_find_int(part_node, 'Size'),
  219. last_modified=iso8601_to_unixtime(_find_tag(part_node, 'LastModified'))
  220. ))
  221. return result
  222. def parse_batch_delete_objects(result, body):
  223. if not body:
  224. return result
  225. root = ElementTree.fromstring(body)
  226. url_encoded = _is_url_encoding(root)
  227. for deleted_node in root.findall('Deleted'):
  228. key = _find_object(deleted_node, 'Key', url_encoded)
  229. result.deleted_keys.append(key)
  230. versionid_node = deleted_node.find('VersionId')
  231. versionid = None
  232. if versionid_node is not None:
  233. versionid = _find_tag(deleted_node, 'VersionId')
  234. delete_marker_node = deleted_node.find('DeleteMarker')
  235. delete_marker = False
  236. if delete_marker_node is not None:
  237. delete_marker = _find_bool(deleted_node, 'DeleteMarker')
  238. marker_versionid_node = deleted_node.find('DeleteMarkerVersionId')
  239. delete_marker_versionid = ''
  240. if marker_versionid_node is not None:
  241. delete_marker_versionid = _find_tag(deleted_node, 'DeleteMarkerVersionId')
  242. result.delete_versions.append(BatchDeleteObjectVersionResult(key, versionid, delete_marker, delete_marker_versionid))
  243. return result
  244. def parse_get_bucket_acl(result, body):
  245. root = ElementTree.fromstring(body)
  246. result.acl = _find_tag(root, 'AccessControlList/Grant')
  247. return result
  248. def parse_get_object_acl(result, body):
  249. root = ElementTree.fromstring(body)
  250. result.acl = _find_tag(root, 'AccessControlList/Grant')
  251. return result
  252. def parse_get_bucket_location(result, body):
  253. result.location = to_string(ElementTree.fromstring(body).text)
  254. return result
  255. def parse_get_bucket_logging(result, body):
  256. root = ElementTree.fromstring(body)
  257. if root.find('LoggingEnabled/TargetBucket') is not None:
  258. result.target_bucket = _find_tag(root, 'LoggingEnabled/TargetBucket')
  259. if root.find('LoggingEnabled/TargetPrefix') is not None:
  260. result.target_prefix = _find_tag(root, 'LoggingEnabled/TargetPrefix')
  261. return result
  262. def parse_get_bucket_stat(result, body):
  263. root = ElementTree.fromstring(body)
  264. result.storage_size_in_bytes = _find_int(root, 'Storage')
  265. result.object_count = _find_int(root, 'ObjectCount')
  266. result.multi_part_upload_count = int(_find_tag_with_default(root, 'MultipartUploadCount', 0))
  267. result.live_channel_count = int(_find_tag_with_default(root, 'LiveChannelCount', 0))
  268. result.last_modified_time = int(_find_tag_with_default(root, 'LastModifiedTime', 0))
  269. result.standard_storage = int(_find_tag_with_default(root, 'StandardStorage', 0))
  270. result.standard_object_count = int(_find_tag_with_default(root, 'StandardObjectCount', 0))
  271. result.infrequent_access_storage = int(_find_tag_with_default(root, 'InfrequentAccessStorage', 0))
  272. result.infrequent_access_real_storage = int(_find_tag_with_default(root, 'InfrequentAccessRealStorage', 0))
  273. result.infrequent_access_object_count = int(_find_tag_with_default(root, 'InfrequentAccessObjectCount', 0))
  274. result.archive_storage = int(_find_tag_with_default(root, 'ArchiveStorage', 0))
  275. result.archive_real_storage = int(_find_tag_with_default(root, 'ArchiveRealStorage', 0))
  276. result.archive_object_count = int(_find_tag_with_default(root, 'ArchiveObjectCount', 0))
  277. result.cold_archive_storage = int(_find_tag_with_default(root, 'ColdArchiveStorage', 0))
  278. result.cold_archive_real_storage = int(_find_tag_with_default(root, 'ColdArchiveRealStorage', 0))
  279. result.cold_archive_object_count = int(_find_tag_with_default(root, 'ColdArchiveObjectCount', 0))
  280. return result
  281. def parse_get_bucket_info(result, body):
  282. root = ElementTree.fromstring(body)
  283. result.name = _find_tag(root, 'Bucket/Name')
  284. result.creation_date = _find_tag(root, 'Bucket/CreationDate')
  285. result.storage_class = _find_tag(root, 'Bucket/StorageClass')
  286. result.extranet_endpoint = _find_tag(root, 'Bucket/ExtranetEndpoint')
  287. result.intranet_endpoint = _find_tag(root, 'Bucket/IntranetEndpoint')
  288. result.location = _find_tag(root, 'Bucket/Location')
  289. result.owner = Owner(_find_tag(root, 'Bucket/Owner/DisplayName'), _find_tag(root, 'Bucket/Owner/ID'))
  290. result.acl = AccessControlList(_find_tag(root, 'Bucket/AccessControlList/Grant'))
  291. result.comment = _find_tag_with_default(root, 'Bucket/Comment', None)
  292. result.versioning_status = _find_tag_with_default(root, 'Bucket/Versioning', None)
  293. result.data_redundancy_type = _find_tag_with_default(root, 'Bucket/DataRedundancyType', None)
  294. result.access_monitor = _find_tag_with_default(root, 'Bucket/AccessMonitor', None)
  295. result.transfer_acceleration = _find_tag_with_default(root, 'Bucket/TransferAcceleration', None)
  296. result.cross_region_replication = _find_tag_with_default(root, 'Bucket/CrossRegionReplication', None)
  297. result.resource_group_id = _find_tag_with_default(root, 'Bucket/ResourceGroupId', None)
  298. server_side_encryption = root.find("Bucket/ServerSideEncryptionRule")
  299. if server_side_encryption is None:
  300. result.bucket_encryption_rule = None
  301. else:
  302. result.bucket_encryption_rule = _parse_bucket_encryption_info(server_side_encryption)
  303. return result
  304. def _parse_bucket_encryption_info(node):
  305. rule = ServerSideEncryptionRule()
  306. rule.sse_algorithm = _find_tag(node,"SSEAlgorithm")
  307. if rule.sse_algorithm == "None":
  308. rule.kms_master_keyid = None
  309. rule.sse_algorithm = None
  310. return rule
  311. kmsnode = node.find("KMSMasterKeyID")
  312. if kmsnode is None or kmsnode.text is None:
  313. rule.kms_master_keyid = None
  314. else:
  315. rule.kms_master_keyid = to_string(kmsnode.text)
  316. kms_data_encryption_node = node.find("KMSDataEncryption")
  317. if kms_data_encryption_node is None or kms_data_encryption_node.text is None:
  318. rule.kms_data_encryption = None
  319. else:
  320. rule.kms_data_encryption = to_string(kms_data_encryption_node.text)
  321. return rule
  322. def parse_get_bucket_referer(result, body):
  323. root = ElementTree.fromstring(body)
  324. result.allow_empty_referer = _find_bool(root, 'AllowEmptyReferer')
  325. result.referers = _find_all_tags(root, 'RefererList/Referer')
  326. if root.find("AllowTruncateQueryString") is not None:
  327. result.allow_truncate_query_string = _find_bool(root, 'AllowTruncateQueryString')
  328. if root.find("RefererBlacklist/Referer") is not None:
  329. result.black_referers = _find_all_tags(root, 'RefererBlacklist/Referer')
  330. return result
  331. def parse_condition_include_header(include_header_node):
  332. key = _find_tag(include_header_node, 'Key')
  333. equals = _find_tag(include_header_node, 'Equals')
  334. include_header = ConditionInlcudeHeader(key, equals)
  335. return include_header
  336. def parse_routing_rule_condition(condition_node):
  337. if condition_node.find('KeyPrefixEquals') is not None:
  338. key_prefix_equals = _find_tag(condition_node, 'KeyPrefixEquals')
  339. if condition_node.find('HttpErrorCodeReturnedEquals') is not None:
  340. http_err_code_return_equals = _find_int(condition_node, 'HttpErrorCodeReturnedEquals');
  341. include_header_list = []
  342. if condition_node.find('IncludeHeader') is not None:
  343. for include_header_node in condition_node.findall('IncludeHeader'):
  344. include_header = parse_condition_include_header(include_header_node)
  345. include_header_list.append(include_header)
  346. condition = Condition(key_prefix_equals, http_err_code_return_equals, include_header_list)
  347. return condition
  348. def parse_mirror_headers(mirror_headers_node):
  349. if mirror_headers_node is None:
  350. return None
  351. pass_all = None
  352. if mirror_headers_node.find('PassAll') is not None:
  353. pass_all = _find_bool(mirror_headers_node, 'PassAll')
  354. pass_list = _find_all_tags(mirror_headers_node, 'Pass')
  355. remove_list = _find_all_tags(mirror_headers_node, 'Remove')
  356. set_list = []
  357. for set_node in mirror_headers_node.findall('Set'):
  358. key = _find_tag(set_node, 'Key')
  359. value = _find_tag(set_node, 'Value')
  360. mirror_headers_set = MirrorHeadersSet(key, value)
  361. set_list.append(mirror_headers_set)
  362. redirect_mirror_headers = RedirectMirrorHeaders(pass_all, pass_list, remove_list, set_list)
  363. return redirect_mirror_headers
  364. def parse_routing_rule_redirect(redirect_node):
  365. redirect_type = None
  366. pass_query_string = None
  367. replace_key_with = None
  368. replace_key_prefix_with = None
  369. proto = None
  370. host_name = None
  371. http_redirect_code = None
  372. mirror_url = None
  373. mirror_url_slave = None
  374. mirror_url_probe = None
  375. mirror_pass_query_string = None
  376. mirror_check_md5 = None
  377. mirror_follow_redirect = None
  378. mirror_headers = None
  379. # common args
  380. redirect_type = _find_tag(redirect_node, 'RedirectType')
  381. if redirect_node.find('PassQueryString') is not None:
  382. pass_query_string = _find_bool(redirect_node, 'PassQueryString')
  383. # External, AliCDN
  384. if redirect_type in [REDIRECT_TYPE_EXTERNAL, REDIRECT_TYPE_ALICDN]:
  385. if redirect_node.find('Protocol') is not None:
  386. proto = _find_tag(redirect_node, 'Protocol')
  387. if redirect_node.find('HostName') is not None:
  388. host_name = _find_tag(redirect_node, 'HostName')
  389. if redirect_node.find('HttpRedirectCode') is not None:
  390. http_redirect_code = _find_int(redirect_node, 'HttpRedirectCode')
  391. # External, AliCDN, Internal
  392. if redirect_type in [REDIRECT_TYPE_EXTERNAL, REDIRECT_TYPE_ALICDN, REDIRECT_TYPE_INTERNAL]:
  393. if redirect_node.find('ReplaceKeyWith') is not None:
  394. replace_key_with = _find_tag(redirect_node, 'ReplaceKeyWith')
  395. if redirect_node.find('ReplaceKeyPrefixWith') is not None:
  396. replace_key_prefix_with = _find_tag(redirect_node, 'ReplaceKeyPrefixWith')
  397. # Mirror
  398. elif redirect_type == REDIRECT_TYPE_MIRROR:
  399. if redirect_node.find('MirrorURL') is not None:
  400. mirror_url = _find_tag(redirect_node, 'MirrorURL')
  401. if redirect_node.find('MirrorURLSlave') is not None:
  402. mirror_url_slave = _find_tag(redirect_node, 'MirrorURLSlave')
  403. if redirect_node.find('MirrorURLProbe') is not None:
  404. mirror_url_probe = _find_tag(redirect_node, 'MirrorURLProbe')
  405. if redirect_node.find('MirrorPassQueryString') is not None:
  406. mirror_pass_query_string = _find_bool(redirect_node, 'MirrorPassQueryString')
  407. if redirect_node.find('MirrorCheckMd5') is not None:
  408. mirror_check_md5 = _find_bool(redirect_node, 'MirrorCheckMd5')
  409. if redirect_node.find('MirrorFollowRedirect') is not None:
  410. mirror_follow_redirect = _find_bool(redirect_node, 'MirrorFollowRedirect')
  411. mirror_headers = parse_mirror_headers(redirect_node.find('MirrorHeaders'))
  412. redirect = Redirect(redirect_type=redirect_type, proto=proto, host_name=host_name, replace_key_with=replace_key_with,
  413. replace_key_prefix_with=replace_key_prefix_with, http_redirect_code=http_redirect_code,
  414. pass_query_string=pass_query_string, mirror_url=mirror_url,mirror_url_slave=mirror_url_slave,
  415. mirror_url_probe=mirror_url_probe, mirror_pass_query_string=mirror_pass_query_string,
  416. mirror_follow_redirect=mirror_follow_redirect, mirror_check_md5=mirror_check_md5,
  417. mirror_headers=mirror_headers)
  418. return redirect
  419. def parse_get_bucket_website(result, body):
  420. root = ElementTree.fromstring(body)
  421. result.index_file = _find_tag(root, 'IndexDocument/Suffix')
  422. result.error_file = _find_tag(root, 'ErrorDocument/Key')
  423. if root.find('RoutingRules') is None:
  424. return result
  425. routing_rules_node = root.find('RoutingRules')
  426. for rule_node in routing_rules_node.findall('RoutingRule'):
  427. rule_num = _find_int(rule_node, 'RuleNumber')
  428. condition = parse_routing_rule_condition(rule_node.find('Condition'))
  429. redirect = parse_routing_rule_redirect(rule_node.find('Redirect'))
  430. rule = RoutingRule(rule_num, condition, redirect);
  431. result.rules.append(rule)
  432. return result
  433. def parse_create_live_channel(result, body):
  434. root = ElementTree.fromstring(body)
  435. result.play_url = _find_tag(root, 'PlayUrls/Url')
  436. result.publish_url = _find_tag(root, 'PublishUrls/Url')
  437. return result
  438. def parse_get_live_channel(result, body):
  439. root = ElementTree.fromstring(body)
  440. result.status = _find_tag(root, 'Status')
  441. result.description = _find_tag(root, 'Description')
  442. target = LiveChannelInfoTarget()
  443. target.type = _find_tag(root, 'Target/Type')
  444. target.frag_duration = _find_tag(root, 'Target/FragDuration')
  445. target.frag_count = _find_tag(root, 'Target/FragCount')
  446. target.playlist_name = _find_tag(root, 'Target/PlaylistName')
  447. result.target = target
  448. return result
  449. def parse_list_live_channel(result, body):
  450. root = ElementTree.fromstring(body)
  451. result.prefix = _find_tag(root, 'Prefix')
  452. result.marker = _find_tag(root, 'Marker')
  453. result.max_keys = _find_int(root, 'MaxKeys')
  454. result.is_truncated = _find_bool(root, 'IsTruncated')
  455. if result.is_truncated:
  456. result.next_marker = _find_tag(root, 'NextMarker')
  457. channels = root.findall('LiveChannel')
  458. for channel in channels:
  459. tmp = LiveChannelInfo()
  460. tmp.name = _find_tag(channel, 'Name')
  461. tmp.description = _find_tag(channel, 'Description')
  462. tmp.status = _find_tag(channel, 'Status')
  463. tmp.last_modified = iso8601_to_unixtime(_find_tag(channel, 'LastModified'))
  464. tmp.play_url = _find_tag(channel, 'PlayUrls/Url')
  465. tmp.publish_url = _find_tag(channel, 'PublishUrls/Url')
  466. result.channels.append(tmp)
  467. return result
  468. def parse_stat_video(video_node, video):
  469. video.width = _find_int(video_node, 'Width')
  470. video.height = _find_int(video_node, 'Height')
  471. video.frame_rate = _find_int(video_node, 'FrameRate')
  472. video.bandwidth = _find_int(video_node, 'Bandwidth')
  473. video.codec = _find_tag(video_node, 'Codec')
  474. def parse_stat_audio(audio_node, audio):
  475. audio.bandwidth = _find_int(audio_node, 'Bandwidth')
  476. audio.sample_rate = _find_int(audio_node, 'SampleRate')
  477. audio.codec = _find_tag(audio_node, 'Codec')
  478. def parse_live_channel_stat(result, body):
  479. root = ElementTree.fromstring(body)
  480. result.status = _find_tag(root, 'Status')
  481. if root.find('RemoteAddr') is not None:
  482. result.remote_addr = _find_tag(root, 'RemoteAddr')
  483. if root.find('ConnectedTime') is not None:
  484. result.connected_time = iso8601_to_unixtime(_find_tag(root, 'ConnectedTime'))
  485. video_node = root.find('Video')
  486. audio_node = root.find('Audio')
  487. if video_node is not None:
  488. result.video = LiveChannelVideoStat()
  489. parse_stat_video(video_node, result.video)
  490. if audio_node is not None:
  491. result.audio = LiveChannelAudioStat()
  492. parse_stat_audio(audio_node, result.audio)
  493. return result
  494. def parse_live_channel_history(result, body):
  495. root = ElementTree.fromstring(body)
  496. records = root.findall('LiveRecord')
  497. for record in records:
  498. tmp = LiveRecord()
  499. tmp.start_time = iso8601_to_unixtime(_find_tag(record, 'StartTime'))
  500. tmp.end_time = iso8601_to_unixtime(_find_tag(record, 'EndTime'))
  501. tmp.remote_addr = _find_tag(record, 'RemoteAddr')
  502. result.records.append(tmp)
  503. return result
  504. def parse_lifecycle_expiration(expiration_node):
  505. if expiration_node is None:
  506. return None
  507. expiration = LifecycleExpiration()
  508. if expiration_node.find('Days') is not None:
  509. expiration.days = _find_int(expiration_node, 'Days')
  510. elif expiration_node.find('Date') is not None:
  511. expiration.date = iso8601_to_date(_find_tag(expiration_node, 'Date'))
  512. elif expiration_node.find('CreatedBeforeDate') is not None:
  513. expiration.created_before_date = iso8601_to_date(_find_tag(expiration_node, 'CreatedBeforeDate'))
  514. elif expiration_node.find('ExpiredObjectDeleteMarker') is not None:
  515. expiration.expired_detete_marker = _find_bool(expiration_node, 'ExpiredObjectDeleteMarker')
  516. return expiration
  517. def parse_lifecycle_abort_multipart_upload(abort_multipart_upload_node):
  518. if abort_multipart_upload_node is None:
  519. return None
  520. abort_multipart_upload = AbortMultipartUpload()
  521. if abort_multipart_upload_node.find('Days') is not None:
  522. abort_multipart_upload.days = _find_int(abort_multipart_upload_node, 'Days')
  523. elif abort_multipart_upload_node.find('CreatedBeforeDate') is not None:
  524. abort_multipart_upload.created_before_date = iso8601_to_date(_find_tag(abort_multipart_upload_node,
  525. 'CreatedBeforeDate'))
  526. return abort_multipart_upload
  527. def parse_lifecycle_storage_transitions(storage_transition_nodes):
  528. storage_transitions = []
  529. for storage_transition_node in storage_transition_nodes:
  530. storage_class = _find_tag(storage_transition_node, 'StorageClass')
  531. storage_transition = StorageTransition(storage_class=storage_class)
  532. if storage_transition_node.find('Days') is not None:
  533. storage_transition.days = _find_int(storage_transition_node, 'Days')
  534. elif storage_transition_node.find('CreatedBeforeDate') is not None:
  535. storage_transition.created_before_date = iso8601_to_date(_find_tag(storage_transition_node,
  536. 'CreatedBeforeDate'))
  537. if storage_transition_node.find('IsAccessTime') is not None:
  538. storage_transition.is_access_time = _find_bool(storage_transition_node, 'IsAccessTime')
  539. if storage_transition_node.find('ReturnToStdWhenVisit') is not None:
  540. storage_transition.return_to_std_when_visit = _find_bool(storage_transition_node, 'ReturnToStdWhenVisit')
  541. if storage_transition_node.find('AllowSmallFile') is not None:
  542. storage_transition.allow_small_file = _find_bool(storage_transition_node, 'AllowSmallFile')
  543. storage_transitions.append(storage_transition)
  544. return storage_transitions
  545. def parse_lifecycle_object_taggings(lifecycle_tagging_nodes):
  546. if lifecycle_tagging_nodes is None or \
  547. len(lifecycle_tagging_nodes) == 0:
  548. return None
  549. tagging_rule = TaggingRule()
  550. for tag_node in lifecycle_tagging_nodes:
  551. key = _find_tag(tag_node, 'Key')
  552. value = _find_tag(tag_node, 'Value')
  553. tagging_rule.add(key, value)
  554. return Tagging(tagging_rule)
  555. def parse_lifecycle_version_expiration(version_expiration_node):
  556. if version_expiration_node is None:
  557. return None
  558. noncurrent_days = _find_int(version_expiration_node, 'NoncurrentDays')
  559. expiration = NoncurrentVersionExpiration(noncurrent_days)
  560. return expiration
  561. def parse_lifecycle_verison_storage_transitions(version_storage_transition_nodes):
  562. version_storage_transitions = []
  563. for transition_node in version_storage_transition_nodes:
  564. storage_class = _find_tag(transition_node, 'StorageClass')
  565. non_crurrent_days = _find_int(transition_node, 'NoncurrentDays')
  566. version_storage_transition = NoncurrentVersionStorageTransition(non_crurrent_days, storage_class)
  567. if transition_node.find('IsAccessTime') is not None:
  568. version_storage_transition.is_access_time = _find_bool(transition_node, 'IsAccessTime')
  569. if transition_node.find('ReturnToStdWhenVisit') is not None:
  570. version_storage_transition.return_to_std_when_visit = _find_bool(transition_node, 'ReturnToStdWhenVisit')
  571. if transition_node.find('AllowSmallFile') is not None:
  572. version_storage_transition.allow_small_file = _find_bool(transition_node, 'AllowSmallFile')
  573. version_storage_transitions.append(version_storage_transition)
  574. return version_storage_transitions
  575. def parse_get_bucket_lifecycle(result, body):
  576. root = ElementTree.fromstring(body)
  577. url_encoded = _is_url_encoding(root)
  578. for rule_node in root.findall('Rule'):
  579. expiration = parse_lifecycle_expiration(rule_node.find('Expiration'))
  580. abort_multipart_upload = parse_lifecycle_abort_multipart_upload(rule_node.find('AbortMultipartUpload'))
  581. storage_transitions = parse_lifecycle_storage_transitions(rule_node.findall('Transition'))
  582. tagging = parse_lifecycle_object_taggings(rule_node.findall('Tag'))
  583. noncurrent_version_expiration = parse_lifecycle_version_expiration(rule_node.find('NoncurrentVersionExpiration'))
  584. noncurrent_version_sotrage_transitions = parse_lifecycle_verison_storage_transitions(rule_node.findall('NoncurrentVersionTransition'))
  585. lifecycle_filter = parse_lifecycle_filter_not(rule_node.findall('Filter/Not'))
  586. rule = LifecycleRule(
  587. _find_tag(rule_node, 'ID'),
  588. _find_tag(rule_node, 'Prefix'),
  589. status=_find_tag(rule_node, 'Status'),
  590. expiration=expiration,
  591. abort_multipart_upload=abort_multipart_upload,
  592. storage_transitions=storage_transitions,
  593. tagging=tagging,
  594. noncurrent_version_expiration = noncurrent_version_expiration,
  595. noncurrent_version_sotrage_transitions = noncurrent_version_sotrage_transitions,
  596. atime_base=int(_find_tag_with_default(rule_node, 'AtimeBase', 0)),
  597. filter=lifecycle_filter
  598. )
  599. result.rules.append(rule)
  600. return result
  601. def parse_get_bucket_cors(result, body):
  602. root = ElementTree.fromstring(body)
  603. for rule_node in root.findall('CORSRule'):
  604. rule = CorsRule()
  605. rule.allowed_origins = _find_all_tags(rule_node, 'AllowedOrigin')
  606. rule.allowed_methods = _find_all_tags(rule_node, 'AllowedMethod')
  607. rule.allowed_headers = _find_all_tags(rule_node, 'AllowedHeader')
  608. rule.expose_headers = _find_all_tags(rule_node, 'ExposeHeader')
  609. max_age_node = rule_node.find('MaxAgeSeconds')
  610. if max_age_node is not None:
  611. rule.max_age_seconds = int(max_age_node.text)
  612. result.rules.append(rule)
  613. return result
  614. def to_complete_upload_request(parts):
  615. root = ElementTree.Element('CompleteMultipartUpload')
  616. for p in parts:
  617. part_node = ElementTree.SubElement(root, "Part")
  618. _add_text_child(part_node, 'PartNumber', str(p.part_number))
  619. _add_text_child(part_node, 'ETag', '"{0}"'.format(p.etag))
  620. return _node_to_string(root)
  621. def to_batch_delete_objects_request(keys, quiet):
  622. root_node = ElementTree.Element('Delete')
  623. _add_text_child(root_node, 'Quiet', str(quiet).lower())
  624. for key in keys:
  625. object_node = ElementTree.SubElement(root_node, 'Object')
  626. _add_text_child(object_node, 'Key', key)
  627. return _node_to_string(root_node)
  628. def to_batch_delete_objects_version_request(objectVersions, quiet):
  629. root_node = ElementTree.Element('Delete')
  630. _add_text_child(root_node, 'Quiet', str(quiet).lower())
  631. objectVersionList = objectVersions.object_version_list
  632. for ver in objectVersionList:
  633. object_node = ElementTree.SubElement(root_node, 'Object')
  634. _add_text_child(object_node, 'Key', ver.key)
  635. if ver.versionid != '':
  636. _add_text_child(object_node, 'VersionId', ver.versionid)
  637. return _node_to_string(root_node)
  638. def to_put_bucket_config(bucket_config):
  639. root = ElementTree.Element('CreateBucketConfiguration')
  640. _add_text_child(root, 'StorageClass', bucket_config.storage_class)
  641. if bucket_config.data_redundancy_type is not None:
  642. _add_text_child(root, 'DataRedundancyType', bucket_config.data_redundancy_type)
  643. return _node_to_string(root)
  644. def to_put_bucket_logging(bucket_logging):
  645. root = ElementTree.Element('BucketLoggingStatus')
  646. if bucket_logging.target_bucket:
  647. logging_node = ElementTree.SubElement(root, 'LoggingEnabled')
  648. _add_text_child(logging_node, 'TargetBucket', bucket_logging.target_bucket)
  649. _add_text_child(logging_node, 'TargetPrefix', bucket_logging.target_prefix)
  650. return _node_to_string(root)
  651. def to_put_bucket_referer(bucket_referer):
  652. root = ElementTree.Element('RefererConfiguration')
  653. _add_text_child(root, 'AllowEmptyReferer', str(bucket_referer.allow_empty_referer).lower())
  654. list_node = ElementTree.SubElement(root, 'RefererList')
  655. for r in bucket_referer.referers:
  656. _add_text_child(list_node, 'Referer', r)
  657. if bucket_referer.allow_truncate_query_string is not None:
  658. _add_text_child(root, 'AllowTruncateQueryString', str(bucket_referer.allow_truncate_query_string).lower())
  659. if bucket_referer.black_referers:
  660. black_referer_node = ElementTree.SubElement(root, 'RefererBlacklist')
  661. for r in bucket_referer.black_referers:
  662. _add_text_child(black_referer_node, 'Referer', r)
  663. return _node_to_string(root)
  664. def to_put_bucket_website(bucket_website):
  665. root = ElementTree.Element('WebsiteConfiguration')
  666. index_node = ElementTree.SubElement(root, 'IndexDocument')
  667. _add_text_child(index_node, 'Suffix', bucket_website.index_file)
  668. error_node = ElementTree.SubElement(root, 'ErrorDocument')
  669. _add_text_child(error_node, 'Key', bucket_website.error_file)
  670. if len(bucket_website.rules) == 0:
  671. return _node_to_string(root)
  672. rules_node = ElementTree.SubElement(root, "RoutingRules")
  673. for rule in bucket_website.rules:
  674. rule_node = ElementTree.SubElement(rules_node, 'RoutingRule')
  675. _add_text_child(rule_node, 'RuleNumber', str(rule.rule_num))
  676. condition_node = ElementTree.SubElement(rule_node, 'Condition')
  677. if rule.condition.key_prefix_equals is not None:
  678. _add_text_child(condition_node, 'KeyPrefixEquals', rule.condition.key_prefix_equals)
  679. if rule.condition.http_err_code_return_equals is not None:
  680. _add_text_child(condition_node, 'HttpErrorCodeReturnedEquals',
  681. str(rule.condition.http_err_code_return_equals))
  682. for header in rule.condition.include_header_list:
  683. include_header_node = ElementTree.SubElement(condition_node, 'IncludeHeader')
  684. _add_text_child(include_header_node, 'Key', header.key)
  685. _add_text_child(include_header_node, 'Equals', header.equals)
  686. if rule.redirect is not None:
  687. redirect_node = ElementTree.SubElement(rule_node, 'Redirect')
  688. # common
  689. _add_text_child(redirect_node, 'RedirectType', rule.redirect.redirect_type)
  690. if rule.redirect.pass_query_string is not None:
  691. _add_text_child(redirect_node, 'PassQueryString', str(rule.redirect.pass_query_string))
  692. # External, AliCDN
  693. if rule.redirect.redirect_type in [REDIRECT_TYPE_EXTERNAL, REDIRECT_TYPE_ALICDN]:
  694. if rule.redirect.proto is not None:
  695. _add_text_child(redirect_node, 'Protocol', rule.redirect.proto)
  696. if rule.redirect.host_name is not None:
  697. _add_text_child(redirect_node, 'HostName', rule.redirect.host_name)
  698. if rule.redirect.http_redirect_code is not None:
  699. _add_text_child(redirect_node, 'HttpRedirectCode', str(rule.redirect.http_redirect_code))
  700. # External, AliCDN, Internal
  701. if rule.redirect.redirect_type in [REDIRECT_TYPE_EXTERNAL, REDIRECT_TYPE_ALICDN, REDIRECT_TYPE_INTERNAL]:
  702. if rule.redirect.replace_key_with is not None:
  703. _add_text_child(redirect_node, 'ReplaceKeyWith', rule.redirect.replace_key_with)
  704. if rule.redirect.replace_key_prefix_with is not None:
  705. _add_text_child(redirect_node, 'ReplaceKeyPrefixWith', rule.redirect.replace_key_prefix_with)
  706. # Mirror
  707. elif rule.redirect.redirect_type == REDIRECT_TYPE_MIRROR:
  708. if rule.redirect.mirror_url is not None:
  709. _add_text_child(redirect_node, 'MirrorURL', rule.redirect.mirror_url)
  710. if rule.redirect.mirror_url_slave is not None:
  711. _add_text_child(redirect_node, 'MirrorURLSlave', rule.redirect.mirror_url_slave)
  712. if rule.redirect.mirror_url_probe is not None:
  713. _add_text_child(redirect_node, 'MirrorURLProbe', rule.redirect.mirror_url_probe)
  714. if rule.redirect.mirror_pass_query_string is not None:
  715. _add_text_child(redirect_node, 'MirrorPassQueryString', str(rule.redirect.mirror_pass_query_string))
  716. if rule.redirect.mirror_follow_redirect is not None:
  717. _add_text_child(redirect_node, 'MirrorFollowRedirect', str(rule.redirect.mirror_follow_redirect))
  718. if rule.redirect.mirror_check_md5 is not None:
  719. _add_text_child(redirect_node, 'MirrorCheckMd5', str(rule.redirect.mirror_check_md5))
  720. if rule.redirect.mirror_headers is not None:
  721. mirror_headers_node = ElementTree.SubElement(redirect_node, 'MirrorHeaders')
  722. if rule.redirect.mirror_headers.pass_all is not None:
  723. _add_text_child(mirror_headers_node, 'PassAll', str(rule.redirect.mirror_headers.pass_all))
  724. for pass_param in rule.redirect.mirror_headers.pass_list:
  725. _add_text_child(mirror_headers_node, 'Pass', pass_param)
  726. for remove_param in rule.redirect.mirror_headers.remove_list:
  727. _add_text_child(mirror_headers_node, 'Remove', remove_param)
  728. for set_param in rule.redirect.mirror_headers.set_list:
  729. set_node = ElementTree.SubElement(mirror_headers_node, 'Set')
  730. _add_text_child(set_node, 'Key', set_param.key)
  731. _add_text_child(set_node, 'Value', set_param.value)
  732. return _node_to_string(root)
  733. def to_put_bucket_lifecycle(bucket_lifecycle):
  734. root = ElementTree.Element('LifecycleConfiguration')
  735. for rule in bucket_lifecycle.rules:
  736. rule_node = ElementTree.SubElement(root, 'Rule')
  737. _add_text_child(rule_node, 'ID', rule.id)
  738. _add_text_child(rule_node, 'Prefix', rule.prefix)
  739. _add_text_child(rule_node, 'Status', rule.status)
  740. expiration = rule.expiration
  741. if expiration:
  742. expiration_node = ElementTree.SubElement(rule_node, 'Expiration')
  743. if expiration.days is not None:
  744. _add_text_child(expiration_node, 'Days', str(expiration.days))
  745. elif expiration.date is not None:
  746. _add_text_child(expiration_node, 'Date', date_to_iso8601(expiration.date))
  747. elif expiration.created_before_date is not None:
  748. _add_text_child(expiration_node, 'CreatedBeforeDate', date_to_iso8601(expiration.created_before_date))
  749. elif expiration.expired_detete_marker is not None:
  750. _add_text_child(expiration_node, 'ExpiredObjectDeleteMarker', str(expiration.expired_detete_marker))
  751. abort_multipart_upload = rule.abort_multipart_upload
  752. if abort_multipart_upload:
  753. abort_multipart_upload_node = ElementTree.SubElement(rule_node, 'AbortMultipartUpload')
  754. if abort_multipart_upload.days is not None:
  755. _add_text_child(abort_multipart_upload_node, 'Days', str(abort_multipart_upload.days))
  756. elif abort_multipart_upload.created_before_date is not None:
  757. _add_text_child(abort_multipart_upload_node, 'CreatedBeforeDate',
  758. date_to_iso8601(abort_multipart_upload.created_before_date))
  759. storage_transitions = rule.storage_transitions
  760. if storage_transitions:
  761. for storage_transition in storage_transitions:
  762. storage_transition_node = ElementTree.SubElement(rule_node, 'Transition')
  763. _add_text_child(storage_transition_node, 'StorageClass', str(storage_transition.storage_class))
  764. if storage_transition.is_access_time is not None:
  765. _add_text_child(storage_transition_node, 'IsAccessTime', str(storage_transition.is_access_time).lower())
  766. if storage_transition.return_to_std_when_visit is not None:
  767. _add_text_child(storage_transition_node, 'ReturnToStdWhenVisit', str(storage_transition.return_to_std_when_visit).lower())
  768. if storage_transition.allow_small_file is not None:
  769. _add_text_child(storage_transition_node, 'AllowSmallFile', str(storage_transition.allow_small_file).lower())
  770. if storage_transition.days is not None:
  771. _add_text_child(storage_transition_node, 'Days', str(storage_transition.days))
  772. elif storage_transition.created_before_date is not None:
  773. _add_text_child(storage_transition_node, 'CreatedBeforeDate',
  774. date_to_iso8601(storage_transition.created_before_date))
  775. tagging = rule.tagging
  776. if tagging:
  777. tagging_rule = tagging.tag_set.tagging_rule
  778. for key in tagging.tag_set.tagging_rule:
  779. tag_node = ElementTree.SubElement(rule_node, 'Tag')
  780. _add_text_child(tag_node, 'Key', key)
  781. _add_text_child(tag_node, 'Value', tagging_rule[key])
  782. noncurrent_version_expiration = rule.noncurrent_version_expiration
  783. if noncurrent_version_expiration is not None:
  784. version_expiration_node = ElementTree.SubElement(rule_node, 'NoncurrentVersionExpiration')
  785. _add_text_child(version_expiration_node, 'NoncurrentDays', str(noncurrent_version_expiration.noncurrent_days))
  786. noncurrent_version_sotrage_transitions = rule.noncurrent_version_sotrage_transitions
  787. if noncurrent_version_sotrage_transitions is not None:
  788. for noncurrent_version_sotrage_transition in noncurrent_version_sotrage_transitions:
  789. version_transition_node = ElementTree.SubElement(rule_node, 'NoncurrentVersionTransition')
  790. _add_text_child(version_transition_node, 'NoncurrentDays', str(noncurrent_version_sotrage_transition.noncurrent_days))
  791. _add_text_child(version_transition_node, 'StorageClass', str(noncurrent_version_sotrage_transition.storage_class))
  792. if noncurrent_version_sotrage_transition.is_access_time is not None:
  793. _add_text_child(version_transition_node, 'IsAccessTime', str(noncurrent_version_sotrage_transition.is_access_time).lower())
  794. if noncurrent_version_sotrage_transition.return_to_std_when_visit is not None:
  795. _add_text_child(version_transition_node, 'ReturnToStdWhenVisit', str(noncurrent_version_sotrage_transition.return_to_std_when_visit).lower())
  796. if noncurrent_version_sotrage_transition.allow_small_file is not None:
  797. _add_text_child(version_transition_node, 'AllowSmallFile', str(noncurrent_version_sotrage_transition.allow_small_file).lower())
  798. if rule.filter and rule.filter.filter_not:
  799. filter_node = ElementTree.SubElement(rule_node, "Filter")
  800. for not_arg in rule.filter.filter_not:
  801. not_node = ElementTree.SubElement(filter_node, 'Not')
  802. _add_text_child(not_node, 'Prefix', not_arg.prefix)
  803. if not_arg.tag:
  804. tag_node = ElementTree.SubElement(not_node, 'Tag')
  805. _add_text_child(tag_node, 'Key', not_arg.tag.key)
  806. _add_text_child(tag_node, 'Value', not_arg.tag.value)
  807. return _node_to_string(root)
  808. def to_put_bucket_cors(bucket_cors):
  809. root = ElementTree.Element('CORSConfiguration')
  810. for rule in bucket_cors.rules:
  811. rule_node = ElementTree.SubElement(root, 'CORSRule')
  812. _add_node_list(rule_node, 'AllowedOrigin', rule.allowed_origins)
  813. _add_node_list(rule_node, 'AllowedMethod', rule.allowed_methods)
  814. _add_node_list(rule_node, 'AllowedHeader', rule.allowed_headers)
  815. _add_node_list(rule_node, 'ExposeHeader', rule.expose_headers)
  816. if rule.max_age_seconds is not None:
  817. _add_text_child(rule_node, 'MaxAgeSeconds', str(rule.max_age_seconds))
  818. return _node_to_string(root)
  819. def to_create_live_channel(live_channel):
  820. root = ElementTree.Element('LiveChannelConfiguration')
  821. _add_text_child(root, 'Description', live_channel.description)
  822. _add_text_child(root, 'Status', live_channel.status)
  823. target_node = _add_node_child(root, 'Target')
  824. _add_text_child(target_node, 'Type', live_channel.target.type)
  825. _add_text_child(target_node, 'FragDuration', str(live_channel.target.frag_duration))
  826. _add_text_child(target_node, 'FragCount', str(live_channel.target.frag_count))
  827. _add_text_child(target_node, 'PlaylistName', str(live_channel.target.playlist_name))
  828. return _node_to_string(root)
  829. def to_select_object(sql, select_params):
  830. if (select_params is not None and 'Json_Type' in select_params):
  831. return to_select_json_object(sql, select_params)
  832. else:
  833. return to_select_csv_object(sql, select_params)
  834. def to_select_csv_object(sql, select_params):
  835. root = ElementTree.Element('SelectRequest')
  836. _add_text_child(root, 'Expression', base64.b64encode(str.encode(sql)))
  837. input_ser = ElementTree.SubElement(root, 'InputSerialization')
  838. output_ser = ElementTree.SubElement(root, 'OutputSerialization')
  839. csv = ElementTree.SubElement(input_ser, 'CSV')
  840. out_csv = ElementTree.SubElement(output_ser, 'CSV')
  841. options = ElementTree.SubElement(root, 'Options')
  842. if (select_params is None):
  843. return _node_to_string(root)
  844. for key, value in select_params.items():
  845. if SelectParameters.CsvHeaderInfo == key:
  846. _add_text_child(csv, 'FileHeaderInfo', value)
  847. elif SelectParameters.CommentCharacter == key:
  848. _add_text_child(csv, SelectParameters.CommentCharacter, base64.b64encode(str.encode(value)))
  849. elif SelectParameters.RecordDelimiter == key:
  850. _add_text_child(csv, SelectParameters.RecordDelimiter, base64.b64encode(str.encode(value)))
  851. elif SelectParameters.OutputRecordDelimiter == key:
  852. _add_text_child(out_csv, SelectParameters.RecordDelimiter, base64.b64encode(str.encode(value)))
  853. elif SelectParameters.FieldDelimiter == key:
  854. _add_text_child(csv, SelectParameters.FieldDelimiter, base64.b64encode(str.encode(value)))
  855. elif SelectParameters.OutputFieldDelimiter == key:
  856. _add_text_child(out_csv, SelectParameters.FieldDelimiter, base64.b64encode(str.encode(value)))
  857. elif SelectParameters.QuoteCharacter == key:
  858. _add_text_child(csv, SelectParameters.QuoteCharacter, base64.b64encode(str.encode(value)))
  859. elif SelectParameters.SplitRange == key:
  860. _add_text_child(csv, 'Range', utils._make_split_range_string(value))
  861. elif SelectParameters.LineRange == key:
  862. _add_text_child(csv, 'Range', utils._make_line_range_string(value))
  863. elif SelectParameters.CompressionType == key:
  864. _add_text_child(input_ser, SelectParameters.CompressionType, str(value))
  865. elif SelectParameters.KeepAllColumns == key:
  866. _add_text_child(output_ser, SelectParameters.KeepAllColumns, str(value))
  867. elif SelectParameters.OutputRawData == key:
  868. _add_text_child(output_ser, SelectParameters.OutputRawData, str(value))
  869. elif SelectParameters.EnablePayloadCrc == key:
  870. _add_text_child(output_ser, SelectParameters.EnablePayloadCrc, str(value))
  871. elif SelectParameters.OutputHeader == key:
  872. _add_text_child(output_ser, SelectParameters.OutputHeader, str(value))
  873. elif SelectParameters.SkipPartialDataRecord == key:
  874. _add_text_child(options, SelectParameters.SkipPartialDataRecord, str(value))
  875. elif SelectParameters.MaxSkippedRecordsAllowed == key:
  876. _add_text_child(options, SelectParameters.MaxSkippedRecordsAllowed, str(value))
  877. elif SelectParameters.AllowQuotedRecordDelimiter == key:
  878. _add_text_child(csv, SelectParameters.AllowQuotedRecordDelimiter, str(value))
  879. else:
  880. raise SelectOperationClientError("The select_params contains unsupported key " + key, "")
  881. return _node_to_string(root)
  882. def to_select_json_object(sql, select_params):
  883. root = ElementTree.Element('SelectRequest')
  884. _add_text_child(root, 'Expression', base64.b64encode(str.encode(sql)))
  885. input_ser = ElementTree.SubElement(root, 'InputSerialization')
  886. output_ser = ElementTree.SubElement(root, 'OutputSerialization')
  887. json = ElementTree.SubElement(input_ser, 'JSON')
  888. out_json = ElementTree.SubElement(output_ser, 'JSON')
  889. options = ElementTree.SubElement(root, 'Options')
  890. is_doc = select_params[SelectParameters.Json_Type] == SelectJsonTypes.DOCUMENT
  891. _add_text_child(json, 'Type', select_params[SelectParameters.Json_Type])
  892. for key, value in select_params.items():
  893. if SelectParameters.SplitRange == key and is_doc == False:
  894. _add_text_child(json, 'Range', utils._make_split_range_string(value))
  895. elif SelectParameters.LineRange == key and is_doc == False:
  896. _add_text_child(json, 'Range', utils._make_line_range_string(value))
  897. elif SelectParameters.CompressionType == key:
  898. _add_text_child(input_ser, SelectParameters.CompressionType, value)
  899. elif SelectParameters.OutputRawData == key:
  900. _add_text_child(output_ser, SelectParameters.OutputRawData, str(value))
  901. elif SelectParameters.EnablePayloadCrc == key:
  902. _add_text_child(output_ser, SelectParameters.EnablePayloadCrc, str(value))
  903. elif SelectParameters.OutputRecordDelimiter == key:
  904. _add_text_child(out_json, SelectParameters.RecordDelimiter, base64.b64encode(str.encode(value)))
  905. elif SelectParameters.SkipPartialDataRecord == key:
  906. _add_text_child(options, SelectParameters.SkipPartialDataRecord, str(value))
  907. elif SelectParameters.MaxSkippedRecordsAllowed == key:
  908. _add_text_child(options, SelectParameters.MaxSkippedRecordsAllowed, str(value))
  909. elif SelectParameters.ParseJsonNumberAsString == key:
  910. _add_text_child(json, SelectParameters.ParseJsonNumberAsString, str(value))
  911. else:
  912. if key != SelectParameters.Json_Type:
  913. raise SelectOperationClientError("The select_params contains unsupported key " + key, "")
  914. return _node_to_string(root)
  915. def to_get_select_object_meta(meta_param):
  916. if meta_param is not None and SelectParameters.Json_Type in meta_param:
  917. if meta_param[SelectParameters.Json_Type] != SelectJsonTypes.LINES:
  918. raise SelectOperationClientError("Json_Type can only be 'LINES' for creating meta", "")
  919. else:
  920. return to_get_select_json_object_meta(meta_param)
  921. else:
  922. return to_get_select_csv_object_meta(meta_param)
  923. def to_get_select_csv_object_meta(csv_meta_param):
  924. root = ElementTree.Element('CsvMetaRequest')
  925. input_ser = ElementTree.SubElement(root, 'InputSerialization')
  926. csv = ElementTree.SubElement(input_ser, 'CSV')
  927. if (csv_meta_param is None):
  928. return _node_to_string(root)
  929. for key, value in csv_meta_param.items():
  930. if SelectParameters.RecordDelimiter == key:
  931. _add_text_child(csv, SelectParameters.RecordDelimiter, base64.b64encode(str.encode(value)))
  932. elif SelectParameters.FieldDelimiter == key:
  933. _add_text_child(csv, SelectParameters.FieldDelimiter, base64.b64encode(str.encode(value)))
  934. elif SelectParameters.QuoteCharacter == key:
  935. _add_text_child(csv, SelectParameters.QuoteCharacter, base64.b64encode(str.encode(value)))
  936. elif SelectParameters.CompressionType == key:
  937. _add_text_child(input_ser, SelectParameters.CompressionType, base64.b64encode(str.encode(value)))
  938. elif SelectParameters.OverwriteIfExists == key:
  939. _add_text_child(root, SelectParameters.OverwriteIfExists, str(value))
  940. else:
  941. raise SelectOperationClientError("The csv_meta_param contains unsupported key " + key, "")
  942. return _node_to_string(root)
  943. def to_get_select_json_object_meta(json_meta_param):
  944. root = ElementTree.Element('JsonMetaRequest')
  945. input_ser = ElementTree.SubElement(root, 'InputSerialization')
  946. json = ElementTree.SubElement(input_ser, 'JSON')
  947. _add_text_child(json, 'Type', json_meta_param[SelectParameters.Json_Type]) # Json_Type是必须的
  948. for key, value in json_meta_param.items():
  949. if SelectParameters.OverwriteIfExists == key:
  950. _add_text_child(root, SelectParameters.OverwriteIfExists, str(value))
  951. elif SelectParameters.CompressionType == key:
  952. _add_text_child(input_ser, SelectParameters.CompressionType, base64.b64encode(str.encode(value)))
  953. else:
  954. if SelectParameters.Json_Type != key:
  955. raise SelectOperationClientError("The json_meta_param contains unsupported key " + key, "")
  956. return _node_to_string(root)
  957. def to_put_tagging(object_tagging):
  958. root = ElementTree.Element("Tagging")
  959. tag_set = ElementTree.SubElement(root, "TagSet")
  960. for item in object_tagging.tag_set.tagging_rule:
  961. tag_xml = ElementTree.SubElement(tag_set, "Tag")
  962. _add_text_child(tag_xml, 'Key', item)
  963. _add_text_child(tag_xml, 'Value', object_tagging.tag_set.tagging_rule[item])
  964. return _node_to_string(root)
  965. def parse_get_tagging(result, body):
  966. root = ElementTree.fromstring(body)
  967. url_encoded = _is_url_encoding(root)
  968. tagset_node = root.find('TagSet')
  969. if tagset_node is None:
  970. return result
  971. tagging_rules = TaggingRule()
  972. for tag_node in tagset_node.findall('Tag'):
  973. key = _find_object(tag_node, 'Key', url_encoded)
  974. value = _find_object(tag_node, 'Value', url_encoded)
  975. tagging_rules.add(key, value)
  976. result.tag_set = tagging_rules
  977. return result
  978. def to_put_bucket_encryption(rule):
  979. root = ElementTree.Element("ServerSideEncryptionRule")
  980. apply_node = ElementTree.SubElement(root, "ApplyServerSideEncryptionByDefault")
  981. _add_text_child(apply_node, "SSEAlgorithm", rule.sse_algorithm)
  982. if rule.kms_master_keyid:
  983. _add_text_child(apply_node, "KMSMasterKeyID", rule.kms_master_keyid)
  984. if rule.kms_data_encryption:
  985. _add_text_child(apply_node, "KMSDataEncryption", rule.kms_data_encryption)
  986. return _node_to_string(root)
  987. def parse_get_bucket_encryption(result, body):
  988. root = ElementTree.fromstring(body)
  989. apply_node = root.find('ApplyServerSideEncryptionByDefault')
  990. result.sse_algorithm = _find_tag(apply_node, "SSEAlgorithm")
  991. kmsnode = apply_node.find('KMSMasterKeyID')
  992. if kmsnode is None or kmsnode.text is None:
  993. result.kms_master_keyid = None
  994. else:
  995. result.kms_master_keyid = to_string(kmsnode.text)
  996. kms_data_encryption_node = apply_node.find('KMSDataEncryption')
  997. if kms_data_encryption_node is None or kms_data_encryption_node.text is None:
  998. result.kms_data_encryption = None
  999. else:
  1000. result.kms_data_encryption = to_string(kms_data_encryption_node.text)
  1001. return result
  1002. def parse_list_object_versions(result, body):
  1003. root = ElementTree.fromstring(body)
  1004. url_encoded = _is_url_encoding(root)
  1005. result.is_truncated = _find_bool(root, 'IsTruncated')
  1006. if result.is_truncated:
  1007. result.next_key_marker = _find_object(root, 'NextKeyMarker', url_encoded)
  1008. result.next_versionid_marker = _find_object(root, "NextVersionIdMarker", url_encoded)
  1009. result.name = _find_tag(root, "Name")
  1010. result.prefix = _find_object(root, "Prefix", url_encoded)
  1011. result.key_marker = _find_object(root, "KeyMarker", url_encoded)
  1012. result.versionid_marker = _find_object(root, "VersionIdMarker", url_encoded)
  1013. result.max_keys = _find_int(root, "MaxKeys")
  1014. result.delimiter = _find_object(root, "Delimiter", url_encoded)
  1015. for delete_marker in root.findall("DeleteMarker"):
  1016. deleteInfo = DeleteMarkerInfo()
  1017. deleteInfo.key = _find_object(delete_marker, "Key", url_encoded)
  1018. deleteInfo.versionid = _find_tag(delete_marker, "VersionId")
  1019. deleteInfo.is_latest = _find_bool(delete_marker, "IsLatest")
  1020. deleteInfo.last_modified = iso8601_to_unixtime(_find_tag(delete_marker, "LastModified"))
  1021. deleteInfo.owner.id = _find_tag(delete_marker, "Owner/ID")
  1022. deleteInfo.owner.display_name = _find_tag(delete_marker, "Owner/DisplayName")
  1023. result.delete_marker.append(deleteInfo)
  1024. for version in root.findall("Version"):
  1025. versionInfo = ObjectVersionInfo()
  1026. versionInfo.key = _find_object(version, "Key", url_encoded)
  1027. versionInfo.versionid = _find_tag(version, "VersionId")
  1028. versionInfo.is_latest = _find_bool(version, "IsLatest")
  1029. versionInfo.last_modified = iso8601_to_unixtime(_find_tag(version, "LastModified"))
  1030. versionInfo.owner.id = _find_tag(version, "Owner/ID")
  1031. versionInfo.owner.display_name = _find_tag(version, "Owner/DisplayName")
  1032. versionInfo.type = _find_tag(version, "Type")
  1033. versionInfo.storage_class = _find_tag(version, "StorageClass")
  1034. versionInfo.size = _find_int(version, "Size")
  1035. versionInfo.etag = _find_tag(version, "ETag").strip('"')
  1036. versionInfo.restore_info = _find_tag_with_default(version, 'RestoreInfo', None)
  1037. result.versions.append(versionInfo)
  1038. for common_prefix in root.findall("CommonPrefixes"):
  1039. result.common_prefix.append(_find_object(common_prefix, "Prefix", url_encoded))
  1040. return result
  1041. def to_put_bucket_versioning(bucket_version_config):
  1042. root = ElementTree.Element('VersioningConfiguration')
  1043. _add_text_child(root, 'Status', str(bucket_version_config.status))
  1044. return _node_to_string(root)
  1045. def parse_get_bucket_versioning(result, body):
  1046. root = ElementTree.fromstring(body)
  1047. status_node = root.find("Status")
  1048. if status_node is None:
  1049. result.status = None
  1050. else:
  1051. result.status = _find_tag(root, "Status")
  1052. return result
  1053. def to_put_bucket_request_payment(payer):
  1054. root = ElementTree.Element('RequestPaymentConfiguration')
  1055. _add_text_child(root, 'Payer', payer)
  1056. return _node_to_string(root)
  1057. def parse_get_bucket_request_payment(result, body):
  1058. root = ElementTree.fromstring(body)
  1059. result.payer = _find_tag(root, 'Payer')
  1060. return result
  1061. def to_put_qos_info(qos_info):
  1062. root = ElementTree.Element("QoSConfiguration")
  1063. if qos_info.total_upload_bw is not None:
  1064. _add_text_child(root, "TotalUploadBandwidth", str(qos_info.total_upload_bw))
  1065. if qos_info.intranet_upload_bw is not None:
  1066. _add_text_child(root, "IntranetUploadBandwidth", str(qos_info.intranet_upload_bw))
  1067. if qos_info.extranet_upload_bw is not None:
  1068. _add_text_child(root, "ExtranetUploadBandwidth", str(qos_info.extranet_upload_bw))
  1069. if qos_info.total_download_bw is not None:
  1070. _add_text_child(root, "TotalDownloadBandwidth", str(qos_info.total_download_bw))
  1071. if qos_info.intranet_download_bw is not None:
  1072. _add_text_child(root, "IntranetDownloadBandwidth", str(qos_info.intranet_download_bw))
  1073. if qos_info.extranet_download_bw is not None:
  1074. _add_text_child(root, "ExtranetDownloadBandwidth", str(qos_info.extranet_download_bw))
  1075. if qos_info.total_qps is not None:
  1076. _add_text_child(root, "TotalQps", str(qos_info.total_qps))
  1077. if qos_info.intranet_qps is not None:
  1078. _add_text_child(root, "IntranetQps", str(qos_info.intranet_qps))
  1079. if qos_info.extranet_qps is not None:
  1080. _add_text_child(root, "ExtranetQps", str(qos_info.extranet_qps))
  1081. return _node_to_string(root)
  1082. def parse_get_qos_info(result, body):
  1083. """解析UserQosInfo 或者BucketQosInfo
  1084. :UserQosInfo包含成员region,其他成员同BucketQosInfo
  1085. """
  1086. root = ElementTree.fromstring(body)
  1087. if hasattr(result, 'region'):
  1088. result.region = _find_tag(root, 'Region')
  1089. result.total_upload_bw = _find_int(root, 'TotalUploadBandwidth')
  1090. result.intranet_upload_bw = _find_int(root, 'IntranetUploadBandwidth')
  1091. result.extranet_upload_bw = _find_int(root, 'ExtranetUploadBandwidth')
  1092. result.total_download_bw = _find_int(root, 'TotalDownloadBandwidth')
  1093. result.intranet_download_bw = _find_int(root, 'IntranetDownloadBandwidth')
  1094. result.extranet_download_bw = _find_int(root, 'ExtranetDownloadBandwidth')
  1095. result.total_qps = _find_int(root, 'TotalQps')
  1096. result.intranet_qps = _find_int(root, 'IntranetQps')
  1097. result.extranet_qps = _find_int(root, 'ExtranetQps')
  1098. return result
  1099. def parse_get_bucket_user_qos(result, body):
  1100. root = ElementTree.fromstring(body)
  1101. result.storage_capacity = _find_int(root, 'StorageCapacity')
  1102. return result
  1103. def to_put_bucket_user_qos(user_qos):
  1104. root = ElementTree.Element('BucketUserQos')
  1105. _add_text_child(root, 'StorageCapacity', str(user_qos.storage_capacity))
  1106. return _node_to_string(root)
  1107. def to_put_async_fetch_task(task_config):
  1108. root = ElementTree.Element('AsyncFetchTaskConfiguration')
  1109. _add_text_child(root, 'Url', task_config.url)
  1110. _add_text_child(root, 'Object', task_config.object_name)
  1111. if task_config.host is not None:
  1112. _add_text_child(root, 'Host', task_config.host)
  1113. if task_config.content_md5 is not None:
  1114. _add_text_child(root, 'ContentMD5', task_config.content_md5)
  1115. if task_config.callback is not None:
  1116. _add_text_child(root, 'Callback', task_config.callback)
  1117. if task_config.ignore_same_key is not None:
  1118. _add_text_child(root, 'IgnoreSameKey', str(task_config.ignore_same_key).lower())
  1119. return _node_to_string(root)
  1120. def parse_put_async_fetch_task_result(result, body):
  1121. root = ElementTree.fromstring(body)
  1122. result.task_id = _find_tag(root, 'TaskId')
  1123. return result
  1124. def _parse_async_fetch_task_configuration(task_info_node):
  1125. url = _find_tag(task_info_node, 'Url')
  1126. object_name = _find_tag(task_info_node, 'Object')
  1127. host = _find_tag(task_info_node, 'Host')
  1128. content_md5 = _find_tag(task_info_node, 'ContentMD5')
  1129. callback = _find_tag(task_info_node, 'Callback')
  1130. ignore_same_key = _find_bool(task_info_node, 'IgnoreSameKey')
  1131. return AsyncFetchTaskConfiguration(url, object_name, host, content_md5, callback, ignore_same_key)
  1132. def parse_get_async_fetch_task_result(result, body):
  1133. root = ElementTree.fromstring(body)
  1134. result.task_id = _find_tag(root, 'TaskId')
  1135. result.task_state = _find_tag(root, 'State')
  1136. result.error_msg = _find_tag(root, 'ErrorMsg')
  1137. result.task_config = _parse_async_fetch_task_configuration(root.find('TaskInfo'))
  1138. return result
  1139. def to_put_inventory_configuration(inventory_config):
  1140. root = ElementTree.Element("InventoryConfiguration")
  1141. _add_text_child(root, "Id", inventory_config.inventory_id)
  1142. if inventory_config.is_enabled is not None:
  1143. _add_text_child(root, "IsEnabled", str(inventory_config.is_enabled))
  1144. if inventory_config.included_object_versions is not None:
  1145. _add_text_child(root, "IncludedObjectVersions", inventory_config.included_object_versions)
  1146. if inventory_config.inventory_filter is not None:
  1147. filter_node = ElementTree.SubElement(root, 'Filter')
  1148. if inventory_config.inventory_filter.prefix is not None:
  1149. _add_text_child(filter_node, "Prefix", inventory_config.inventory_filter.prefix)
  1150. if inventory_config.inventory_filter.last_modify_begin_time_stamp is not None:
  1151. _add_text_child(filter_node, "LastModifyBeginTimeStamp", str(inventory_config.inventory_filter.last_modify_begin_time_stamp))
  1152. if inventory_config.inventory_filter.last_modify_end_time_stamp is not None:
  1153. _add_text_child(filter_node, "LastModifyEndTimeStamp", str(inventory_config.inventory_filter.last_modify_end_time_stamp))
  1154. if inventory_config.inventory_filter.lower_size_bound is not None:
  1155. _add_text_child(filter_node, "LowerSizeBound", str(inventory_config.inventory_filter.lower_size_bound))
  1156. if inventory_config.inventory_filter.upper_size_bound is not None:
  1157. _add_text_child(filter_node, "UpperSizeBound", str(inventory_config.inventory_filter.upper_size_bound))
  1158. if inventory_config.inventory_filter.storage_class is not None:
  1159. _add_text_child(filter_node, "StorageClass", inventory_config.inventory_filter.storage_class)
  1160. if inventory_config.inventory_schedule is not None and inventory_config.inventory_schedule.frequency is not None:
  1161. schedule_node = ElementTree.SubElement(root, 'Schedule')
  1162. _add_text_child(schedule_node, "Frequency", inventory_config.inventory_schedule.frequency)
  1163. if inventory_config.optional_fields is not None:
  1164. fields_node = ElementTree.SubElement(root, 'OptionalFields')
  1165. for field in inventory_config.optional_fields:
  1166. _add_text_child(fields_node, "Field", field)
  1167. if inventory_config.inventory_destination is not None and inventory_config.inventory_destination.bucket_destination is not None:
  1168. destin_node = ElementTree.SubElement(root, 'Destination')
  1169. bucket_destin_node = ElementTree.SubElement(destin_node, 'OSSBucketDestination')
  1170. bucket_destin = inventory_config.inventory_destination.bucket_destination
  1171. if bucket_destin.account_id is not None:
  1172. _add_text_child(bucket_destin_node, "AccountId", str(bucket_destin.account_id))
  1173. if bucket_destin.role_arn is not None:
  1174. _add_text_child(bucket_destin_node, "RoleArn", bucket_destin.role_arn)
  1175. if bucket_destin.bucket is not None:
  1176. _add_text_child(bucket_destin_node, "Bucket", "acs:oss:::" + bucket_destin.bucket)
  1177. if bucket_destin.inventory_format is not None:
  1178. _add_text_child(bucket_destin_node, "Format", bucket_destin.inventory_format)
  1179. if bucket_destin.prefix is not None:
  1180. _add_text_child(bucket_destin_node, "Prefix", bucket_destin.prefix)
  1181. if bucket_destin.sse_kms_encryption is not None:
  1182. encryption_node = ElementTree.SubElement(bucket_destin_node, 'Encryption')
  1183. sse_kms_node = ElementTree.SubElement(encryption_node, 'SSE-KMS')
  1184. _add_text_child(sse_kms_node, "KeyId", bucket_destin.sse_kms_encryption.key_id)
  1185. elif bucket_destin.sse_oss_encryption is not None:
  1186. encryption_node = ElementTree.SubElement(bucket_destin_node, 'Encryption')
  1187. _add_node_child(encryption_node, 'SSE-OSS')
  1188. return _node_to_string(root)
  1189. def get_Inventory_configuration_from_element(elem):
  1190. root = elem
  1191. result = InventoryConfiguration()
  1192. result.inventory_id = _find_tag(root, 'Id')
  1193. result.is_enabled = _find_bool(root, 'IsEnabled')
  1194. result.included_object_versions = _find_tag(root, 'IncludedObjectVersions')
  1195. if root.find("Filter") is not None:
  1196. prefix = None
  1197. last_modify_begin_time_stamp = None
  1198. last_modify_end_time_stamp = None
  1199. lower_size_bound = None
  1200. upper_size_bound = None
  1201. storage_class = None
  1202. if root.find("Filter/Prefix") is not None:
  1203. prefix = _find_tag(root, 'Filter/Prefix')
  1204. if root.find("Filter/LastModifyBeginTimeStamp") is not None:
  1205. last_modify_begin_time_stamp = _find_tag_with_default(root, 'Filter/LastModifyBeginTimeStamp', None)
  1206. if root.find("Filter/LastModifyEndTimeStamp") is not None:
  1207. last_modify_end_time_stamp = _find_tag_with_default(root, 'Filter/LastModifyEndTimeStamp', None)
  1208. if root.find("Filter/LowerSizeBound") is not None:
  1209. lower_size_bound = _find_tag_with_default(root, 'Filter/LowerSizeBound', None)
  1210. if root.find("Filter/UpperSizeBound") is not None:
  1211. upper_size_bound = _find_tag_with_default(root, 'Filter/UpperSizeBound', None)
  1212. if root.find("Filter/StorageClass") is not None:
  1213. storage_class = _find_tag_with_default(root, 'Filter/StorageClass', None)
  1214. result.inventory_filter = InventoryFilter(prefix=prefix, last_modify_begin_time_stamp=last_modify_begin_time_stamp,
  1215. last_modify_end_time_stamp=last_modify_end_time_stamp, lower_size_bound=lower_size_bound,
  1216. upper_size_bound=upper_size_bound, storage_class=storage_class)
  1217. if root.find("Schedule/Frequency") is not None:
  1218. result.inventory_schedule = InventorySchedule(_find_tag(root, 'Schedule/Frequency'))
  1219. result.optional_fields = _find_all_tags(root, "OptionalFields/Field")
  1220. if root.find("Destination/OSSBucketDestination") is not None:
  1221. bucket_distin_node = root.find("Destination/OSSBucketDestination")
  1222. account_id = None
  1223. role_arn = None
  1224. bucket = None
  1225. inventory_format = None
  1226. prefix = None
  1227. sse_kms_encryption = None
  1228. sse_oss_encryption = None
  1229. if bucket_distin_node.find('AccountId') is not None:
  1230. account_id = _find_tag(bucket_distin_node, 'AccountId')
  1231. if bucket_distin_node.find('RoleArn') is not None:
  1232. role_arn = _find_tag(bucket_distin_node, 'RoleArn')
  1233. if bucket_distin_node.find('Bucket') is not None:
  1234. origin_bucket = _find_tag(bucket_distin_node, 'Bucket')
  1235. if origin_bucket.startswith('acs:oss:::'):
  1236. bucket = origin_bucket.replace('acs:oss:::', '')
  1237. if bucket_distin_node.find('Format') is not None:
  1238. inventory_format = _find_tag(bucket_distin_node, 'Format')
  1239. if bucket_distin_node.find('Prefix') is not None:
  1240. prefix = _find_tag(bucket_distin_node, 'Prefix')
  1241. sse_kms_node = bucket_distin_node.find("Encryption/SSE-KMS")
  1242. if sse_kms_node is not None:
  1243. sse_kms_encryption = InventoryServerSideEncryptionKMS(_find_tag(sse_kms_node, 'KeyId'))
  1244. elif bucket_distin_node.find("Encryption/SSE-OSS") is not None:
  1245. sse_oss_encryption = InventoryServerSideEncryptionOSS()
  1246. bucket_destination = InventoryBucketDestination(account_id=account_id, role_arn=role_arn,
  1247. bucket=bucket, inventory_format=inventory_format, prefix=prefix,
  1248. sse_kms_encryption=sse_kms_encryption, sse_oss_encryption=sse_oss_encryption)
  1249. result.inventory_destination = InventoryDestination(bucket_destination)
  1250. return result
  1251. def parse_get_bucket_inventory_configuration(result, body):
  1252. root = ElementTree.fromstring(body)
  1253. inventory_config = get_Inventory_configuration_from_element(root)
  1254. result.inventory_id = inventory_config.inventory_id
  1255. result.is_enabled = inventory_config.is_enabled
  1256. result.included_object_versions = inventory_config.included_object_versions
  1257. result.inventory_filter = inventory_config.inventory_filter
  1258. result.inventory_schedule = inventory_config.inventory_schedule
  1259. result.optional_fields = inventory_config.optional_fields
  1260. result.inventory_destination = inventory_config.inventory_destination
  1261. return result
  1262. def parse_list_bucket_inventory_configurations(result, body):
  1263. root = ElementTree.fromstring(body)
  1264. for inventory_config_node in root.findall("InventoryConfiguration"):
  1265. inventory_config = get_Inventory_configuration_from_element(inventory_config_node)
  1266. result.inventory_configurations.append(inventory_config)
  1267. if root.find("ContinuationToken") is not None:
  1268. result.continuaiton_token = _find_tag(root, "ContinuationToken")
  1269. if root.find("IsTruncated") is not None:
  1270. result.is_truncated = _find_bool(root, "IsTruncated")
  1271. if root.find("NextContinuationToken") is not None:
  1272. result.next_continuation_token = _find_tag(root, "NextContinuationToken")
  1273. return result
  1274. def to_put_restore_config(restore_config):
  1275. root = ElementTree.Element('RestoreRequest')
  1276. _add_text_child(root, 'Days', str(restore_config.days))
  1277. if restore_config.job_parameters is not None:
  1278. job_parameters = restore_config.job_parameters
  1279. job_parameters_node = ElementTree.SubElement(root, "JobParameters")
  1280. if job_parameters.tier is not None:
  1281. _add_text_child(job_parameters_node, 'Tier', job_parameters.tier)
  1282. return _node_to_string(root)
  1283. def parse_get_bucket_worm_result(result, body):
  1284. root = ElementTree.fromstring(body)
  1285. result.worm_id = _find_tag(root, "WormId")
  1286. result.state = _find_tag(root, "State")
  1287. result.retention_period_days = _find_int(root, "RetentionPeriodInDays")
  1288. result.creation_date = _find_tag(root, "CreationDate")
  1289. def to_put_extend_bucket_worm(retention_period_days):
  1290. root = ElementTree.Element('ExtendWormConfiguration')
  1291. _add_text_child(root, 'RetentionPeriodInDays', str(retention_period_days))
  1292. return _node_to_string(root)
  1293. def to_put_init_bucket_worm(retention_period_days):
  1294. root = ElementTree.Element('InitiateWormConfiguration')
  1295. _add_text_child(root, 'RetentionPeriodInDays', str(retention_period_days))
  1296. return _node_to_string(root)
  1297. def to_put_bucket_replication(replication_config):
  1298. root = ElementTree.Element('ReplicationConfiguration')
  1299. rule = ElementTree.SubElement(root, 'Rule')
  1300. if replication_config.rule_id:
  1301. _add_text_child(rule, 'ID', replication_config.rule_id)
  1302. destination = ElementTree.SubElement(rule, 'Destination')
  1303. _add_text_child(destination, 'Bucket', replication_config.target_bucket_name)
  1304. _add_text_child(destination, 'Location', replication_config.target_bucket_location)
  1305. if replication_config.target_transfer_type:
  1306. _add_text_child(destination, 'TransferType', replication_config.target_transfer_type)
  1307. if replication_config.is_enable_historical_object_replication is False:
  1308. _add_text_child(rule, 'HistoricalObjectReplication', 'disabled')
  1309. else:
  1310. _add_text_child(rule, 'HistoricalObjectReplication', 'enabled')
  1311. if replication_config.prefix_list:
  1312. prefix_list_node = ElementTree.SubElement(rule, 'PrefixSet')
  1313. for prefix in replication_config.prefix_list:
  1314. _add_text_child(prefix_list_node, 'Prefix', prefix)
  1315. if replication_config.action_list:
  1316. actions = ''
  1317. for action in replication_config.action_list:
  1318. actions += action
  1319. actions += ','
  1320. actions = actions[:-1]
  1321. _add_text_child(rule, 'Action', actions)
  1322. if replication_config.sync_role_name:
  1323. _add_text_child(rule, 'SyncRole', replication_config.sync_role_name)
  1324. if replication_config.replica_kms_keyid:
  1325. encryption_config = ElementTree.SubElement(rule, 'EncryptionConfiguration')
  1326. _add_text_child(encryption_config, 'ReplicaKmsKeyID', replication_config.replica_kms_keyid)
  1327. if replication_config.sse_kms_encrypted_objects_status in ['Enabled', 'Disabled']:
  1328. criteria = ElementTree.SubElement(rule, 'SourceSelectionCriteria')
  1329. sse_kms_encrypted_objects = ElementTree.SubElement(criteria, 'SseKmsEncryptedObjects')
  1330. _add_text_child(sse_kms_encrypted_objects, 'Status', replication_config.sse_kms_encrypted_objects_status)
  1331. return _node_to_string(root)
  1332. def to_delete_bucket_replication(rule_id):
  1333. root = ElementTree.Element('ReplicationRules')
  1334. _add_text_child(root, 'ID', rule_id)
  1335. return _node_to_string(root)
  1336. def parse_get_bucket_replication_result(result, body):
  1337. root = ElementTree.fromstring(body)
  1338. for rule_node in root.findall("Rule"):
  1339. rule = ReplicationRule()
  1340. if rule_node.find("ID") is not None:
  1341. rule.rule_id = _find_tag(rule_node, "ID")
  1342. destination_node = rule_node.find("Destination")
  1343. rule.target_bucket_name = _find_tag(destination_node, "Bucket")
  1344. rule.target_bucket_location = _find_tag(destination_node, "Location")
  1345. rule.target_transfer_type = _find_tag_with_default(destination_node, "TransferType", None)
  1346. rule.status = _find_tag(rule_node, "Status")
  1347. rule.sync_role_name = _find_tag_with_default(rule_node, 'SyncRole', None)
  1348. rule.replica_kms_keyid = _find_tag_with_default(rule_node, 'EncryptionConfiguration/ReplicaKmsKeyID', None)
  1349. rule.sse_kms_encrypted_objects_status = _find_tag_with_default(rule_node, 'SourceSelectionCriteria/SseKmsEncryptedObjects/Status', None)
  1350. if _find_tag(rule_node, "HistoricalObjectReplication") == 'enabled':
  1351. rule.is_enable_historical_object_replication = True
  1352. else:
  1353. rule.is_enable_historical_object_replication = False
  1354. prefixes_node = rule_node.find('PrefixSet')
  1355. if prefixes_node is not None:
  1356. rule.prefix_list = _find_all_tags(prefixes_node, 'Prefix')
  1357. actions = _find_tag(rule_node, 'Action')
  1358. rule.action_list = actions.split(',')
  1359. result.rule_list.append(rule)
  1360. def parse_get_bucket_replication_location_result(result, body):
  1361. root = ElementTree.fromstring(body)
  1362. result.location_list = _find_all_tags(root, "Location")
  1363. if root.find("LocationTransferTypeConstraint") is not None:
  1364. constraint_node = root.find("LocationTransferTypeConstraint")
  1365. for transfer_type_node in constraint_node.findall("LocationTransferType"):
  1366. location_transfer_type = LocationTransferType()
  1367. location_transfer_type.location = _find_tag_with_default(transfer_type_node, "Location", None)
  1368. location_transfer_type.transfer_type = _find_tag_with_default(transfer_type_node, "TransferTypes/Type", None)
  1369. result.location_transfer_type_list.append(location_transfer_type)
  1370. def parse_get_bucket_replication_progress_result(result, body):
  1371. root = ElementTree.fromstring(body)
  1372. rule_node = root.find("Rule")
  1373. progress = BucketReplicationProgress()
  1374. progress.rule_id = _find_tag(rule_node, "ID")
  1375. destination_node = rule_node.find("Destination")
  1376. progress.target_bucket_name = _find_tag(destination_node, "Bucket")
  1377. progress.target_bucket_location = _find_tag(destination_node, "Location")
  1378. progress.target_transfer_type = _find_tag_with_default(destination_node, "TransferType", None)
  1379. progress.status = _find_tag(rule_node, "Status")
  1380. if _find_tag(rule_node, "HistoricalObjectReplication") == 'enabled':
  1381. progress.is_enable_historical_object_replication = True
  1382. else:
  1383. progress.is_enable_historical_object_replication = False
  1384. prefixes_node = rule_node.find('PrefixSet')
  1385. if prefixes_node is not None:
  1386. progress.prefix_list = _find_all_tags(prefixes_node, 'Prefix')
  1387. actions = _find_tag(rule_node, 'Action')
  1388. progress.action_list = actions.split(',')
  1389. historical_object_progress = _find_tag_with_default(rule_node, 'Progress/HistoricalObject', None)
  1390. if historical_object_progress is not None:
  1391. progress.historical_object_progress = float(historical_object_progress)
  1392. progress.new_object_progress = _find_tag_with_default(rule_node, 'Progress/NewObject', None)
  1393. result.progress = progress
  1394. def to_put_bucket_transfer_acceleration(enabled):
  1395. root = ElementTree.Element('TransferAccelerationConfiguration')
  1396. _add_text_child(root, 'Enabled', str(enabled))
  1397. return _node_to_string(root)
  1398. def parse_get_bucket_transfer_acceleration_result(result, body):
  1399. root = ElementTree.fromstring(body)
  1400. result.enabled = _find_tag(root, "Enabled")
  1401. def to_bucket_cname_configuration(domain, cert=None):
  1402. root = ElementTree.Element("BucketCnameConfiguration")
  1403. cname = ElementTree.SubElement(root, 'Cname')
  1404. _add_text_child(cname, 'Domain', domain)
  1405. if cert is not None:
  1406. certificate = ElementTree.SubElement(cname, 'CertificateConfiguration')
  1407. if cert.cert_id is not None:
  1408. _add_text_child(certificate, 'CertId', cert.cert_id)
  1409. if cert.certificate is not None:
  1410. _add_text_child(certificate, 'Certificate', cert.certificate)
  1411. if cert.private_key is not None:
  1412. _add_text_child(certificate, 'PrivateKey',cert.private_key)
  1413. if cert.previous_cert_id is not None:
  1414. _add_text_child(certificate, 'PreviousCertId', cert.previous_cert_id)
  1415. if cert.force is not None:
  1416. _add_text_child(certificate, 'Force', str(cert.force))
  1417. if cert.delete_certificate is not None:
  1418. _add_text_child(certificate, 'DeleteCertificate', str(cert.delete_certificate))
  1419. return _node_to_string(root)
  1420. def parse_create_bucket_cname_token(result, body):
  1421. root = ElementTree.fromstring(body)
  1422. result.bucket = _find_tag(root, "Bucket")
  1423. result.cname = _find_tag(root, "Cname")
  1424. result.token = _find_tag(root, "Token")
  1425. result.expire_time = _find_tag(root, "ExpireTime")
  1426. def parse_get_bucket_cname_token(result, body):
  1427. root = ElementTree.fromstring(body)
  1428. result.bucket = _find_tag(root, "Bucket")
  1429. result.cname = _find_tag(root, "Cname")
  1430. result.token = _find_tag(root, "Token")
  1431. result.expire_time = _find_tag(root, "ExpireTime")
  1432. def parse_list_bucket_cname(result, body):
  1433. root = ElementTree.fromstring(body)
  1434. result.bucket = _find_tag(root, "Bucket")
  1435. result.owner = _find_tag(root, "Owner")
  1436. for cname in root.findall('Cname'):
  1437. tmp = CnameInfo()
  1438. tmp.domain = _find_tag_with_default(cname, 'Domain', None)
  1439. tmp.last_modified = _find_tag_with_default(cname, 'LastModified', None)
  1440. tmp.status = _find_tag_with_default(cname, 'Status', None)
  1441. tmp.is_purge_cdn_cache = _find_tag_with_default(cname, 'IsPurgeCdnCache', None)
  1442. cert = cname.find('Certificate')
  1443. if cert is not None:
  1444. certificate = CertificateInfo()
  1445. certificate.type = _find_tag_with_default(cert, 'Type', None)
  1446. certificate.cert_id = _find_tag_with_default(cert, 'CertId', None)
  1447. certificate.status = _find_tag_with_default(cert, 'Status', None)
  1448. certificate.creation_date = _find_tag_with_default(cert, 'CreationDate', None)
  1449. certificate.fingerprint = _find_tag_with_default(cert, 'Fingerprint', None)
  1450. certificate.valid_start_date = _find_tag_with_default(cert, 'ValidStartDate', None)
  1451. certificate.valid_end_date = _find_tag_with_default(cert, 'ValidEndDate', None)
  1452. tmp.certificate = certificate
  1453. result.cname.append(tmp)
  1454. def to_do_bucket_meta_query_request(meta_query):
  1455. root = ElementTree.Element("MetaQuery")
  1456. if meta_query.next_token is not None:
  1457. _add_text_child(root, "NextToken", meta_query.next_token)
  1458. _add_text_child(root, "MaxResults", meta_query.max_results)
  1459. _add_text_child(root, "Query", meta_query.query)
  1460. if meta_query.sort is not None:
  1461. _add_text_child(root, "Sort", meta_query.sort)
  1462. if meta_query.order is not None:
  1463. _add_text_child(root, "Order", meta_query.order)
  1464. if meta_query.aggregations:
  1465. aggregations_node = ElementTree.SubElement(root, "Aggregations")
  1466. for aggregation in meta_query.aggregations:
  1467. aggregation_node = ElementTree.SubElement(aggregations_node, 'Aggregation')
  1468. if aggregation.field is not None:
  1469. _add_text_child(aggregation_node, 'Field', aggregation.field)
  1470. if aggregation.operation is not None:
  1471. _add_text_child(aggregation_node, 'Operation', aggregation.operation)
  1472. return _node_to_string(root)
  1473. def parse_get_bucket_meta_query_result(result, body):
  1474. root = ElementTree.fromstring(body)
  1475. result.state = _find_tag(root, "State")
  1476. result.phase = _find_tag(root, "Phase")
  1477. result.create_time = _find_tag(root, "CreateTime")
  1478. result.update_time = _find_tag(root, "UpdateTime")
  1479. def parse_do_bucket_meta_query_result(result, body):
  1480. root = ElementTree.fromstring(body)
  1481. result.next_token = _find_tag(root, "NextToken")
  1482. for file in root.findall('Files/File'):
  1483. tmp = MetaQueryFile()
  1484. tmp.file_name = _find_tag(file, 'Filename')
  1485. tmp.size = int(_find_tag_with_default(file, 'Size', 0))
  1486. tmp.file_modified_time = _find_tag_with_default(file, 'FileModifiedTime', None)
  1487. tmp.file_create_time = _find_tag_with_default(file, 'FileCreateTime', None)
  1488. tmp.file_access_time = _find_tag_with_default(file, 'FileAccessTime', None)
  1489. tmp.oss_object_type = _find_tag_with_default(file, 'OSSObjectType', None)
  1490. tmp.oss_storage_class = _find_tag_with_default(file, 'OSSStorageClass', None)
  1491. tmp.object_acl = _find_tag_with_default(file, 'ObjectACL', None)
  1492. tmp.etag = _find_tag_with_default(file, 'ETag', None)
  1493. tmp.oss_crc64 = _find_tag_with_default(file, 'OSSCRC64', None)
  1494. tmp.oss_tagging_count = int(_find_tag_with_default(file, 'OSSTaggingCount', 0))
  1495. if file.find('OSSTagging') is not None:
  1496. for tagging in file.find('OSSTagging').findall('Tagging'):
  1497. tmp_tagging = OSSTaggingInfo(_find_tag(tagging, 'Key'), _find_tag(tagging, 'Value'))
  1498. tmp.oss_tagging.append(tmp_tagging)
  1499. if file.find('OSSUserMeta') is not None:
  1500. for meta in file.find('OSSUserMeta').findall('UserMeta'):
  1501. tmp_meta = OSSUserMetaInfo(_find_tag(meta, 'Key'), _find_tag(meta, 'Value'))
  1502. tmp.oss_user_meta.append(tmp_meta)
  1503. result.files.append(tmp)
  1504. for aggregation in root.findall('Aggregations/Aggregation'):
  1505. tmp = AggregationsInfo()
  1506. tmp.field = _find_tag(aggregation, 'Field')
  1507. tmp.operation = _find_tag(aggregation, 'Operation')
  1508. tmp.value = float(_find_tag_with_default(aggregation, 'Value', 0))
  1509. for group in aggregation.findall('Groups/Group'):
  1510. tmp_groups = AggregationGroupInfo(_find_tag(group, 'Value'), int(_find_tag_with_default(group, 'Count', 0)))
  1511. tmp.groups.append(tmp_groups)
  1512. result.aggregations.append(tmp)
  1513. def parse_dummy_result(result, body):
  1514. return result
  1515. def to_put_bucket_access_monitor(status):
  1516. root = ElementTree.Element('AccessMonitorConfiguration')
  1517. _add_text_child(root, 'Status', status)
  1518. return _node_to_string(root)
  1519. def parse_get_bucket_access_monitor_result(result, body):
  1520. root = ElementTree.fromstring(body)
  1521. access_monitor = AccessMonitorInfo(_find_tag(root, "Status"))
  1522. result.access_monitor = access_monitor
  1523. def parse_lifecycle_filter_not(filter_not_node):
  1524. if filter_not_node is not None:
  1525. lifecycle_filter = LifecycleFilter()
  1526. for not_node in filter_not_node:
  1527. prefix = _find_tag_with_default(not_node, 'Prefix', None)
  1528. key = _find_tag_with_default(not_node, 'Tag/Key', None)
  1529. value = _find_tag_with_default(not_node, 'Tag/Value', None)
  1530. tag = FilterNotTag(key, value)
  1531. filter_not = FilterNot(prefix, tag)
  1532. lifecycle_filter.filter_not.append(filter_not)
  1533. return lifecycle_filter
  1534. def to_put_bucket_resource_group(resourceGroupId):
  1535. root = ElementTree.Element('BucketResourceGroupConfiguration')
  1536. _add_text_child(root, 'ResourceGroupId', resourceGroupId)
  1537. return _node_to_string(root)
  1538. def parse_get_bucket_resource_group_result(result, body):
  1539. root = ElementTree.fromstring(body)
  1540. result.resource_group_id = _find_tag(root, "ResourceGroupId")
  1541. def to_put_bucket_style(content):
  1542. root = ElementTree.Element('Style')
  1543. _add_text_child(root, 'Content', content)
  1544. return _node_to_string(root)
  1545. def parse_get_bucket_style_result(result, body):
  1546. root = ElementTree.fromstring(body)
  1547. result.name = _find_tag(root, "Name")
  1548. result.content = _find_tag(root, "Content")
  1549. result.create_time = _find_tag(root, "CreateTime")
  1550. result.last_modify_time = _find_tag(root, "LastModifyTime")
  1551. def parse_list_bucket_style(result, body):
  1552. root = ElementTree.fromstring(body)
  1553. for style in root.findall('Style'):
  1554. tmp = BucketStyleInfo()
  1555. tmp.name = _find_tag_with_default(style, 'Name', None)
  1556. tmp.content = _find_tag_with_default(style, 'Content', None)
  1557. tmp.create_time = _find_tag_with_default(style, 'CreateTime', None)
  1558. tmp.last_modify_time = _find_tag_with_default(style, 'LastModifyTime', None)
  1559. result.styles.append(tmp)
  1560. def parse_describe_regions(result, body):
  1561. root = ElementTree.fromstring(body)
  1562. for region in root.findall('RegionInfo'):
  1563. tmp = RegionInfo()
  1564. tmp.region = _find_tag_with_default(region, 'Region', None)
  1565. tmp.internet_endpoint = _find_tag_with_default(region, 'InternetEndpoint', None)
  1566. tmp.internal_endpoint = _find_tag_with_default(region, 'InternalEndpoint', None)
  1567. tmp.accelerate_endpoint = _find_tag_with_default(region, 'AccelerateEndpoint', None)
  1568. result.regions.append(tmp)
  1569. def parse_async_process_object(result, body):
  1570. if body:
  1571. body_dict = eval(body.decode('utf-8'))
  1572. result.event_id = body_dict['EventId']
  1573. result.async_request_id = body_dict['RequestId']
  1574. result.task_id = body_dict['TaskId']
  1575. return result
  1576. def to_do_bucket_callback_policy_request(callback_policy):
  1577. root = ElementTree.Element("BucketCallbackPolicy")
  1578. if callback_policy:
  1579. for policy in callback_policy:
  1580. if policy:
  1581. policy_node = ElementTree.SubElement(root, 'PolicyItem')
  1582. if policy.policy_name is not None:
  1583. _add_text_child(policy_node, 'PolicyName', policy.policy_name)
  1584. if policy.callback is not None:
  1585. _add_text_child(policy_node, 'Callback', policy.callback)
  1586. if policy.callback_var is None:
  1587. _add_text_child(policy_node, 'CallbackVar', '')
  1588. else:
  1589. _add_text_child(policy_node, 'CallbackVar', policy.callback_var)
  1590. return _node_to_string(root)
  1591. def parse_callback_policy_result(result, body):
  1592. root = ElementTree.fromstring(body)
  1593. for policy in root.findall('PolicyItem'):
  1594. tmp = CallbackPolicyInfo()
  1595. tmp.policy_name = _find_tag_with_default(policy, 'PolicyName', None)
  1596. tmp.callback = _find_tag_with_default(policy, 'Callback', None)
  1597. tmp.callback_var = _find_tag_with_default(policy, 'CallbackVar', None)
  1598. result.callback_policies.append(tmp)