|
81 | 81 | xml_marshal_complete_multipart_upload, |
82 | 82 | xml_marshal_bucket_notifications, |
83 | 83 | xml_marshal_delete_objects) |
84 | | -from . import policy |
85 | 84 | from .fold_case_dict import FoldCaseDict |
86 | 85 | from .thread_pool import ThreadPool |
87 | 86 |
|
@@ -359,91 +358,44 @@ def remove_bucket(self, bucket_name): |
359 | 358 | # Make sure to purge bucket_name from region cache. |
360 | 359 | self._delete_bucket_region(bucket_name) |
361 | 360 |
|
362 | | - def _get_bucket_policy(self, bucket_name): |
363 | | - policy_dict = {} |
364 | | - try: |
365 | | - response = self._url_open("GET", |
366 | | - bucket_name=bucket_name, |
367 | | - query={"policy": ""}) |
368 | | - except NoSuchBucketPolicy as e: |
369 | | - return None |
370 | | - except ResponseError as e: |
371 | | - raise |
372 | | - |
373 | | - data = response.data |
374 | | - if isinstance(data, bytes) and isinstance(data, str): # Python 2 |
375 | | - policy_dict = json.loads(data.decode('utf-8')) |
376 | | - elif isinstance(data, str): # Python 3 |
377 | | - policy_dict = json.loads(data) |
378 | | - else: |
379 | | - policy_dict = json.loads(str(data, 'utf-8')) |
380 | | - |
381 | | - return policy_dict |
382 | | - |
383 | | - def get_bucket_policy(self, bucket_name, prefix=""): |
| 361 | + def get_bucket_policy(self, bucket_name): |
384 | 362 | """ |
385 | 363 | Get bucket policy of given bucket name. |
386 | 364 |
|
387 | 365 | :param bucket_name: Bucket name. |
388 | | - :param prefix: Object prefix. |
389 | 366 | """ |
390 | 367 | is_valid_bucket_name(bucket_name) |
391 | 368 |
|
392 | | - policy_dict = self._get_bucket_policy(bucket_name) |
393 | | - if not policy_dict: |
394 | | - return policy.Policy.NONE |
395 | | - |
396 | | - if policy_dict.get('Statement') is None: |
397 | | - raise ValueError("None Policy statement") |
398 | | - # Normalize statements. |
399 | | - statements = [] |
400 | | - policy._append_statements(statements, policy_dict.get('Statement', [])) |
| 369 | + response = self._url_open("GET", |
| 370 | + bucket_name=bucket_name, |
| 371 | + query={"policy": ""}) |
| 372 | + return response.data |
401 | 373 |
|
402 | | - return policy.get_policy(statements, bucket_name, prefix) |
| 374 | + def delete_bucket_policy(self, bucket_name): |
| 375 | + self._url_open("DELETE", |
| 376 | + bucket_name=bucket_name, |
| 377 | + query={"policy": ""}) |
403 | 378 |
|
404 | | - def set_bucket_policy(self, bucket_name, prefix, policy_access): |
| 379 | + def set_bucket_policy(self, bucket_name, policy): |
405 | 380 | """ |
406 | | - Set bucket policy of given bucket name and object prefix. |
| 381 | + Set bucket policy of given bucket name. |
407 | 382 |
|
408 | 383 | :param bucket_name: Bucket name. |
409 | | - :param prefix: Object prefix. |
| 384 | + :param policy: Access policy/ies in JSON format. |
410 | 385 | """ |
411 | 386 | is_valid_bucket_name(bucket_name) |
412 | 387 |
|
413 | | - policy_dict = self._get_bucket_policy(bucket_name) |
414 | | - if policy_access == policy.Policy.NONE and not policy_dict: |
415 | | - return |
416 | | - |
417 | | - if not policy_dict: |
418 | | - policy_dict = {'Statement': [], |
419 | | - "Version": "2012-10-17"} |
420 | | - |
421 | | - # Normalize statements. |
422 | | - statements = [] |
423 | | - policy._append_statements(statements, policy_dict['Statement']) |
424 | | - |
425 | | - statements = policy.set_policy(statements, policy_access, |
426 | | - bucket_name, prefix) |
427 | | - if not statements: |
428 | | - self._url_open("DELETE", |
429 | | - bucket_name=bucket_name, |
430 | | - query={"policy": ""}) |
431 | | - else: |
432 | | - policy_dict['Statement'] = statements |
433 | | - content = json.dumps(policy_dict) |
434 | | - |
435 | | - headers = { |
436 | | - 'Content-Length': str(len(content)), |
437 | | - 'Content-Md5': get_md5_base64digest(content) |
438 | | - } |
439 | | - content_sha256_hex = get_sha256_hexdigest(content) |
440 | | - |
441 | | - self._url_open("PUT", |
442 | | - bucket_name=bucket_name, |
443 | | - query={"policy": ""}, |
444 | | - headers=headers, |
445 | | - body=content, |
446 | | - content_sha256=content_sha256_hex) |
| 388 | + headers = { |
| 389 | + 'Content-Length': str(len(policy)), |
| 390 | + 'Content-Md5': get_md5_base64digest(policy) |
| 391 | + } |
| 392 | + content_sha256_hex = get_sha256_hexdigest(policy) |
| 393 | + self._url_open("PUT", |
| 394 | + bucket_name=bucket_name, |
| 395 | + query={"policy": ""}, |
| 396 | + headers=headers, |
| 397 | + body=policy, |
| 398 | + content_sha256=content_sha256_hex) |
447 | 399 |
|
448 | 400 | def get_bucket_notification(self, bucket_name): |
449 | 401 | """ |
|
0 commit comments