When pay-by-requester is enabled for a bucket in Object Storage Service (OSS), the requester is charged the requests and traffic fees instead of the bucket owner. The bucket owner is charged only the storage fees. You can enable pay-by-requester for a bucket to share data in the bucket without paying for the request and traffic fees incurred by the access to your bucket.

Enable pay-by-requester

The following code provides an example on how to enable pay-by-requester for a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

async function setBucketRequestPayment(bucket, Payer) {
  try {
    // Specify the name of the bucket for which you want to enable pay-by-requester. 
    // Set Payer to Requester or BucketOwner. 
    // If Payer is set to Requester, pay-by-requester is enabled for the bucket. The requester is charged the traffic and request fees that are generated when the requester reads data in the bucket. 
    // If Payer is set to BucketOwner, pay-by-requester is disabled for the bucket. This is the default configuration for the bucket. In this case, the bucket owner is charged the generated request fees. 
    const result = await client.putBucketRequestPayment(bucket, Payer);
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

setBucketRequestPayment('bucketName', 'Requester')

Query pay-by-requester configurations

The following code provides an example on how to query the pay-by-requester configurations of a bucket:

const OSS = require('ali-oss')

const client = new OSS({
   // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret'
});

async function getBucketRequestPayment(bucket) {
  try {
    // Query the pay-by-requester configurations of the bucket. 
    const result = await client.getBucketRequestPayment(bucket);
    console.log(result.payer);
  } catch (e) {
    console.log(e);
  }
}

getBucketRequestPayment('bucketName')

Specify that third parties are charged for access to objects

If you specify that third parties are charged for access to objects in a bucket, requesters must include the x-oss-request-payer:requester header in the HTTP requests to perform operations on your objects. If this header is not included, an error is returned.

The following code provides an example on how to specify that third parties are charged when they access objects in PutObject, GetObject, and DeleteObject requests. You can specify that third parties are charged when they perform read and write operations on objects in other requests in the similar way.

The following code provides an example on how to specify that third parties are charged when they access objects:

const OSS = require('ali-oss')
const bucket = 'bucket-name'
const payer = 'Requester'

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the bucket name. 
  bucket: bucket
});


async function put() {
  await client.putBucketRequestPayment(bucket, payer)

  // Specify the payer when a third party calls the PutObject operation to access objects. 
  await client.put('fileName', 'fileContent', {
    headers: {
      'x-oss-request-payer': 'requester'
    }
  })
}

async function get() {
  await client.putBucketRequestPayment(bucket, payer)

  // Specify the payer when a third party calls the GetObject operation to access objects. 
  await client.get('fileName', {
    headers: {
      'x-oss-request-payer': 'requester'
    }
  })
}

async function del() {
  await client.putBucketRequestPayment(bucket, payer)

  // Specify the payer when a third party calls the DeleteObject operation to access objects. 
  await client.delete('fileName', {
    headers: {
      'x-oss-request-payer': 'requester'
    }
  })
}
            

References

  • For the complete sample code that is used to enable pay-by-requester for a bucket, visit GitHub.
  • For more information about the API operation that you can call to enable pay-by-requester for a bucket, see PutBucketRequestPayment.
  • For more information about the API operation that you can call to query the pay-by-requester configurations of a bucket, see GetBucketRequestPayment.