All Products
Search
Document Center

Object Storage Service:Log storage (C++ SDK)

Last Updated:Mar 20, 2026

OSS generates hourly access logs for your buckets and stores them in a target bucket you specify.

OperationAPIDescription
Enable loggingSetBucketLoggingEnable access logging for a bucket and set the target bucket and prefix
Query logging configurationGetBucketLoggingRetrieve the current logging configuration of a bucket
Disable loggingDeleteBucketLoggingTurn 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 logging

    • oss:GetBucketLogging — to query the logging configuration

    • oss: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 OssClient instance using an OSS endpoint. To create an OssClient using 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

ParameterTypeRequiredDescription
BucketNamestringYesThe name of the bucket to enable logging on
TargetBucketNamestringYesThe bucket where OSS stores the log files. Can be the same as BucketName
TargetPrefixstringNoThe 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:

FieldDescription
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