All Products
Search
Document Center

Object Storage Service:Pay-by-requester (C++ SDK)

Last Updated:Feb 28, 2026

Pay-by-requester shifts request and traffic fees from the bucket owner to the requester. The bucket owner pays only for storage. Enable this mode to share data without incurring costs from other users' access.

Billing responsibility

Cost categoryPaid by
StorageBucket owner
API requests (GET, PUT, DELETE, etc.)Requester
TrafficRequester

Prerequisites

Before you begin, make sure that you have:

  • An Object Storage Service (OSS) bucket

  • The oss:PutBucketRequestPayment permission to enable pay-by-requester

  • The oss:GetBucketRequestPayment permission to query pay-by-requester configurations

  • For more information, see Attach a custom policy to a RAM user

Usage notes

  • The examples use the public endpoint of the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, see Regions and endpoints.

  • The examples create an OssClient instance using an OSS endpoint. To use custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • Anonymous requests are denied on pay-by-requester buckets. All requesters must be authenticated Alibaba Cloud users.

Common setup

The following initialization code is shared by all examples. Each subsequent example shows only the operation-specific code that replaces the /* Perform operations */ placeholder.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";

    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";

    /* Specify the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Perform operations */

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Enable pay-by-requester

Replace the /* Perform operations */ section in the common setup with the following code:

/* Enable pay-by-requester. */
SetBucketRequestPaymentRequest request(BucketName);
request.setRequestPayer(RequestPayer::Requester);

auto outcome = client.SetBucketRequestPayment(request);

if (!outcome.isSuccess()) {
    /* Handle the exception. */
    std::cout << "SetBucketRequestPayment fail" <<
    ",code:" << outcome.error().Code() <<
    ",message:" << outcome.error().Message() <<
    ",requestId:" << outcome.error().RequestId() << std::endl;
    return -1;
}

Disable pay-by-requester

To revert to bucket-owner-pays mode, set the payer to BucketOwner:

/* Disable pay-by-requester (revert to bucket-owner-pays). */
SetBucketRequestPaymentRequest request(BucketName);
request.setRequestPayer(RequestPayer::BucketOwner);

auto outcome = client.SetBucketRequestPayment(request);

if (!outcome.isSuccess()) {
    /* Handle the exception. */
    std::cout << "SetBucketRequestPayment fail" <<
    ",code:" << outcome.error().Code() <<
    ",message:" << outcome.error().Message() <<
    ",requestId:" << outcome.error().RequestId() << std::endl;
    return -1;
}

Query pay-by-requester configuration

Replace the /* Perform operations */ section in the common setup with the following code. Define the ToRequestPayerName helper function before main():

const char* ToRequestPayerName(RequestPayer payer)
{
    static const char* PayerName[] = { "NotSet", "BucketOwner", "Requester"};
    return PayerName[static_cast<int>(payer) - static_cast<int>(RequestPayer::NotSet)];
}
/* Query pay-by-requester configuration. */
GetBucketRequestPaymentRequest request(BucketName);
auto outcome = client.GetBucketRequestPayment(request);

if (outcome.isSuccess())
{
    std::cout << "GetBucketRequestPayment success Payer:" << ToRequestPayerName(outcome.result().Payer()) << std::endl;
}
else {
    /* Handle the exception. */
    std::cout << "GetBucketPayment fail" <<
    ",code:" << outcome.error().Code() <<
    ",message:" << outcome.error().Message() <<
    ",requestId:" << outcome.error().RequestId() << std::endl;
    return -1;
}

The ToRequestPayerName helper function maps the RequestPayer enum to one of three values: NotSet, BucketOwner, or Requester.

Access objects as a third-party requester

When a bucket has pay-by-requester enabled, third-party requesters must include the x-oss-request-payer:requester header in their requests. Without this header, the request fails with an error.

In the C++ SDK, add setRequestPayer(RequestPayer::Requester) to each request object. The following example demonstrates this pattern with PutObject, GetObject, and DeleteObject. Apply the same approach to other operations.

#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
    /* Initialize the OSS account information. */

    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";

    /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
    std::string Region = "yourRegion";

    /* Specify the name of the bucket that the requester wants to access. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object that the requester wants to access. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampleobject.txt";
    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the requester's OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are set as environment variables. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Upload an object as a third-party requester. */
    std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
    *content << "test cpp sdk";
    PutObjectRequest putrequest(BucketName, ObjectName, content);
    putrequest.setRequestPayer(RequestPayer::Requester);
    auto putoutcome = client.PutObject(putrequest);

    /* Download an object as a third-party requester. */
    GetObjectRequest getrequest(BucketName, ObjectName);
    getrequest.setRequestPayer(RequestPayer::Requester);
    auto getoutcome = client.GetObject(getrequest);

    /* Delete an object as a third-party requester. */
    DeleteObjectRequest delrequest(BucketName, ObjectName);
    delrequest.setRequestPayer(RequestPayer::Requester);
    auto deloutcome = client.DeleteObject(delrequest);

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

References