OSS generates hourly access logs for your buckets and stores them in a target bucket you specify.
| Operation | API | Description |
|---|---|---|
| Enable logging | SetBucketLogging | Enable access logging for a bucket and set the target bucket and prefix |
| Query logging configuration | GetBucketLogging | Retrieve the current logging configuration of a bucket |
| Disable logging | DeleteBucketLogging | Turn off access logging for a bucket |
Prerequisites
Before you begin, ensure that you have:
An OSS bucket to enable logging on
A target bucket to store the log files (can be the same bucket or a different one)
The required RAM permissions:
oss:PutBucketLogging— to enable loggingoss:GetBucketLogging— to query the logging configurationoss:DeleteBucketLogging— to disable logging
For details on granting these permissions, see Grant custom access policies to RAM users.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For more information, see Regions and endpoints.
The examples create an
OssClientinstance using an OSS endpoint. To create anOssClientusing a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
Enable logging for a bucket
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// Replace with the endpoint for your bucket's region.
// Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
std::string Endpoint = "yourEndpoint";
// Replace with the region ID for your bucket.
// Example: cn-hangzhou
std::string Region = "yourRegion";
// The source bucket to enable logging on.
std::string BucketName = "examplebucket";
// The target bucket to store log files. Can be the same as BucketName.
std::string TargetBucketName = "destbucket";
// The prefix for log file names. Logs are saved under this path in the target bucket.
// If left blank, logs are saved to the root directory of the target bucket.
std::string TargetPrefix = "log/";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
// Load credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
SetBucketLoggingRequest request(BucketName, TargetBucketName, TargetPrefix);
auto outcome = client.SetBucketLogging(request);
if (!outcome.isSuccess()) {
std::cout << "SetBucketLogging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
ShutdownSdk();
return 0;
}Key parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
BucketName | string | Yes | The name of the bucket to enable logging on |
TargetBucketName | string | Yes | The bucket where OSS stores the log files. Can be the same as BucketName |
TargetPrefix | string | No | The prefix added to log file names. If not specified, logs are saved to the root directory of the target bucket |
Query the logging configuration of a bucket
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
GetBucketLoggingRequest request(BucketName);
auto outcome = client.GetBucketLogging(request);
if (outcome.isSuccess()) {
std::cout << "GetBucketLogging success" <<
", TargetBucket: " << outcome.result().TargetBucket() <<
", TargetPrefix: " << outcome.result().TargetPrefix() << std::endl;
} else {
std::cout << "GetBucketLogging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
ShutdownSdk();
return 0;
}On success, the response includes the current logging configuration:
| Field | Description |
|---|---|
TargetBucket() | The bucket where log files are stored |
TargetPrefix() | The prefix used for log file names |
Disable logging for a bucket
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
DeleteBucketLoggingRequest request(BucketName);
auto outcome = client.DeleteBucketLogging(request);
if (!outcome.isSuccess()) {
std::cout << "DeleteBucketLogging fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
ShutdownSdk();
return 0;
}What's next
For the complete sample code, see the GitHub example.
For API details, see PutBucketLogging, GetBucketLogging, and DeleteBucketLogging.