enable-logging-programming
Enabling Logging Programmatically
You can enable or disable logging programmatically by using either the Amazon S3 API or the AWS SDKs. To do so, you both enable logging on the bucket and grant the Log Delivery group permission to write logs to the target bucket.
Topics
- Enabling Logging
- Granting the Log Delivery Group WRITE and READ_ACP Permissions
- Example: AWS SDK for .NET
- Related Resources
Enabling Logging
To enable logging, you submit a PUT Bucket logging request to add the logging configuration on the source bucket. The request specifies the target bucket and, optionally, the prefix to be used with all log object keys. The following example identifies logbucket
as the target bucket and logs/
as the prefix.
The log objects are written and owned by the Log Delivery account, and the bucket owner is granted full permissions on the log objects. In addition, you can optionally grant permissions to other users so that they can access the logs. For more information, see PUT Bucket logging.
Amazon S3 also provides the GET Bucket logging API to retrieve logging configuration on a bucket. To delete the logging configuration, you send the PUT Bucket logging request with an empty BucketLoggingStatus
.
You can use either the Amazon S3 API or the AWS SDK wrapper libraries to enable logging on a bucket.
Granting the Log Delivery Group WRITE and READ_ACP Permissions
Amazon S3 writes the log files to the target bucket as a member of the predefined Amazon S3 group Log Delivery. These writes are subject to the usual access control restrictions. You must grant s3:GetObjectAcl
and s3:PutObject
permissions to this group by adding grants to the access control list (ACL) of the target bucket. The Log Delivery group is represented by the following URL.
To grant WRITE
and READ_ACP
permissions, add the following grants. For information about ACLs, see Managing Access with ACLs.
For examples of adding ACL grants programmatically using the AWS SDKs, see Managing ACLs Using the AWS SDK for JavaConfiguring ACL Grants on an Existing Object and Managing ACLs Using the AWS SDK for .NET .
Example: AWS SDK for .NET
The following C# example enables logging on a bucket. You need to create two buckets, a source bucket and a target bucket. The example first grants the Log Delivery group the necessary permission to write logs to the target bucket and then enables logging on the source bucket. For more information, see Enabling Logging Programmatically. For instructions on how to create and test a working sample, see Running the Amazon S3 .NET Code Examples.
Example
Related Resources
- Amazon S3 Server Access Logging
- AWS::S3::Bucket in the AWS CloudFormation User Guide