【问题标题】:How to enable s3 server access logging using the boto3 sdk?如何使用 boto3 sdk 启用 s3 服务器访问日志记录?
【发布时间】:2019-06-18 22:18:00
【问题描述】:

我正在尝试使用 boto3 SDK 通过 python 启用服务器访问日志记录。但是,我不断收到以下错误:

您必须授予日志传递组对目标存储桶的 WRITE 和 READ_ACP 权限

我知道我需要向该组添加权限,但我不知道如何通过 Python SDK 来做到这一点。

我已尝试关注Enabling Logging Programmatically - Amazon Simple Storage Service,但无法将其转换为 Python。

我还尝试将 Grantee 和 Permissions 放入 put_bucket_logging 调用,但无济于事。

下面列出的是我尝试执行此操作导致上述错误的函数:

def enableAccessLogging(clientS3, bucketName, storageBucket, 
                        targetPrefix):

    #Give the group log-delievery WRITE and READ_ACP permisions to the
    #target bucket
    acl = get_bucket_acl(clientS3, storageBucket)

    new_grant = {
        'Grantee': {
            'ID' : 'LogDelivery',
            'Type' : 'Group'
        },
        'Permission': 'FULL_CONTROL',
    }

    modified_acl = copy.deepcopy(acl)
    modified_acl['Grants'].append(new_grant)

    setBucketAcl(clientS3, bucketName, modified_acl)

    response = clientS3.put_bucket_logging(
        Bucket=bucketName,
        BucketLoggingStatus={
            'LoggingEnabled': {
                'TargetBucket': storageBucket,
                'TargetPrefix': targetPrefix
            }
        }

    )

【问题讨论】:

    标签: amazon-web-services boto3


    【解决方案1】:

    我想通了,我正确地创建了新的 acl,但是当我应用它时,我将它应用到源存储桶而不是 targetBucket 所以对于其他人这样做,正确的代码如下:

    def enableAccessLogging(clientS3, bucketName, storageBucket, 
                            targetPrefix):
    
        #Give the group log-delievery WRITE and READ_ACP permisions to the
        #target bucket
        acl = get_bucket_acl(clientS3, storageBucket)
    
        new_grant = {
            'Grantee': {
                'URI': "http://acs.amazonaws.com/groups/s3/LogDelivery",
                'Type' : 'Group'
            },
            'Permission': 'FULL_CONTROL',
        }
    
        modified_acl = copy.deepcopy(acl)
        modified_acl['Grants'].append(new_grant)
    
        setBucketAcl(clientS3, storageBucket, modified_acl)
    
        response = clientS3.put_bucket_logging(
            Bucket=bucketName,
            BucketLoggingStatus={
                'LoggingEnabled': {
                    'TargetBucket': storageBucket,
                    'TargetPrefix': targetPrefix
                }
            }
    
        )
    

    【讨论】:

    • 授予 FULL_CONTROL 不是必要的,也不可取。
    猜你喜欢
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2020-11-07
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多