S3使用Python boto3在大量存储桶上的默认服务器端加密

问题描述 投票:0回答:1

Hi Iam尝试使用python boto3脚本在帐户中的所有存储桶上打开默认的s3加密,如下所示。

import boto3
from botocore.exceptions import ClientError


s3 = boto3.client('s3')

response = s3.list_buckets()

for bucket in response['Buckets']:
    enc = s3.get_bucket_encryption(Bucket=bucket['Name'])
    s3.put_bucket_encryption(
        Bucket=enc,
        ServerSideEncryptionConfiguration={
          'Rules': [
            {
                'ApplyServerSideEncryptionByDefault': {
                    'SSEAlgorithm': 'AES256'
                }
            },
          ]
        }
    )

但是我正在为我的代码无法正常工作而苦苦挣扎

给出错误

Traceback (most recent call last):
  File "apply.py", line 17, in <module>
    'SSEAlgorithm': 'AES256'
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 272, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 549, in _make_api_call
    api_params, operation_model, context=request_context)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 595, in _convert_to_request_dict
    api_params, operation_model, context)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/client.py", line 627, in _emit_api_params
    params=api_params, model=operation_model, context=context)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/hooks.py", line 356, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/hooks.py", line 228, in emit
    return self._emit(event_name, kwargs)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/hooks.py", line 211, in _emit
    response = handler(**kwargs)
  File "/Users/hhaqqani/Library/Python/2.7/lib/python/site-packages/botocore/handlers.py", line 223, in validate_bucket_name
    if not VALID_BUCKET.search(bucket) and not VALID_S3_ARN.search(bucket):
TypeError: expected string or buffe

python amazon-web-services amazon-s3 command-line-interface boto3
1个回答
0
投票

将对Bucket=enc的呼叫中的Bucket=bucket['Name']更改为put_bucket_encryption

© www.soinside.com 2019 - 2024. All rights reserved.