如何在启用日志记录的情况下创建s3存储桶,并使用boto3将其设为私有?

时间:2020-07-07 03:16:29

标签: python-3.x amazon-web-services amazon-s3 boto3 bucket

我想用以下方式创建存储桶

  1. 记录
  2. 加密
  3. 私人和
  4. 警报

在不使用https的情况下访问。我该如何实现?

我使用boto3尝试了几行,但是在登录时遇到错误?

def create_S3_Bucket(env, filepath):
s3_client= AWSresourceconnect(filepath,'s3')
bucket_name ="s3bucket123"
print(bucket_name)
try:
    s3_bucket= s3_client.create_bucket(Bucket=bucket_name)
    print('bucket created')
    print(s3_bucket)
    response = s3_client.put_bucket_encryption(Bucket=bucket_name,
        ServerSideEncryptionConfiguration={
            'Rules': [
                {
                    'ApplyServerSideEncryptionByDefault': {
                        'SSEAlgorithm': 'AES256'

                    }
                },
            ]
        }
    )
    print("response of encrytpion")
    print(response) #prints metadata successfully

    responselogging = s3_client.put_bucket_logging(
        Bucket= bucket_name,
        BucketLoggingStatus={
            'LoggingEnabled': {
                'TargetBucket':bucket_name,
                'TargetGrants': [
                    {
                        'Grantee': {
                            'Type': 'Group',
                            'URI': 'http://acs.amazonaws.com/groups/global/AllUsers',
                        },
                        'Permission': 'READ',
                    },
                ],
                'TargetPrefix': 'test/',
            },
        },
    )

    print("response of logging")
    print(responselogging) 

    Output= bucket_name
except Exception as e:
    Output = "error:" + str(e)
    print(e)  #error as An error occurred (InvalidTargetBucketForLogging) when calling the PutBucketLogging operation: You must give the log-delivery group WRITE and READ_ACP permissions to the target bucket
    bucket_name = ''
retrun Output

我要启用

  1. 记录
  2. 私人水桶和物品
  3. 加密

0 个答案:

没有答案