Aktifkan fitur bayar-per-permintaan untuk bucket Anda saat berbagi data dalam jumlah besar. Fitur ini mengalihkan biaya akses—seperti biaya trafik dan permintaan—kepada pihak yang melakukan permintaan, sedangkan pemilik bucket hanya membayar biaya tetap seperti penyimpanan. Setelah diaktifkan, akses anonim dinonaktifkan dan semua permintaan harus diautentikasi.
Catatan penggunaan
Fitur bayar-per-permintaan hanya tersedia untuk bucket yang memiliki atribut wilayah.
Cara kerja
Layanan Penyimpanan Objek (OSS) memproses permintaan berdasarkan logika berikut:
Jika suatu permintaan menyertakan header
x-oss-request-payer, OSS akan mengautentikasi pihak yang melakukan permintaan, dan pihak tersebut bertanggung jawab atas seluruh biaya trafik dan permintaan.Jika suatu permintaan tidak menyertakan header
x-oss-request-payer:Jika pihak yang melakukan permintaan adalah pemilik bucket, OSS akan memproses permintaan tersebut dan pemilik bucket membayar seluruh biaya.
Jika pihak yang melakukan permintaan bukan pemilik bucket, OSS akan menolak permintaan tersebut.
Konfigurasi bayar-per-permintaan sebagai pemilik bucket
Langkah 1: Aktifkan bayar-per-permintaan
Masuk ke Konsol OSS.
Di panel navigasi sebelah kiri, klik Buckets. Di halaman Buckets, klik nama bucket yang diinginkan.
Di panel navigasi sebelah kiri, pilih .
Di halaman Pay-by-requester, aktifkan sakelar Pay-by-requester.
Di kotak dialog yang muncul, klik OK.
Langkah 2: Berikan izin akses kepada pihak yang melakukan permintaan
Berikan izin akses kepada pihak yang melakukan permintaan melalui kebijakan bucket. Jika tidak, upaya akses oleh pihak tersebut akan ditolak.
Di halaman Buckets, klik nama bucket yang diinginkan.
Di panel navigasi sebelah kiri, pilih .
Di halaman Bucket Policy, klik Authorize pada tab Add in GUI.
Di panel Authorize, konfigurasikan kebijakan tersebut. Untuk Authorized User, pilih Other Accounts, lalu masukkan ID akun Alibaba Cloud pihak yang melakukan permintaan atau Nama Sumber Daya Alibaba Cloud (ARN) peran RAM. Format ARN adalah
arn:sts::{RoleOwnerUid}:assumed-role/{RoleName}/{RoleSessionName}.Klik OK.
Membuat permintaan berbayar sebagai pihak yang melakukan permintaan
Setelah pemilik bucket mengaktifkan fitur bayar‑per‑permintaan untuk suatu bucket, pihak yang melakukan permintaan harus menunjukkan bahwa mereka memahami bahwa mereka akan dikenai biaya atas permintaan tersebut.
Bagian berikut menjelaskan cara pihak yang melakukan permintaan memberikan persetujuan ini menggunakan Konsol OSS, SDK, CLI (ossutil), atau API.
Konsol
Masuk ke Konsol OSS.
Di panel navigasi sebelah kiri, klik tanda plus (+) di samping Favorite Paths.
Di kotak dialog Add Favorite Paths, konfigurasikan parameter sesuai dengan tabel berikut.
Parameter
Deskripsi
Adding method
Pilih Add from other authorized bucket untuk menambahkan bucket yang diotorisasi ke jalur favorit Anda.
Region
Pilih wilayah tempat bucket yang telah diberi otorisasi berada.
Bucket
Masukkan nama bucket yang telah diberi otorisasi.
Pay-by-requester
Pilih I understand and agree untuk mengonfirmasi bahwa Anda akan membayar biaya terkait. Anda akan ditagih atas biaya trafik dan permintaan yang timbul.
SDK
Jika Anda mengakses bucket yang telah mengaktifkan fitur bayar‑per‑permintaan, Anda perlu memberi tahu OSS bahwa Anda setuju untuk membayar permintaan tersebut. Contoh berikut menunjukkan cara melakukannya saat memanggil PutObject, GetObject, atau DeleteObject. Anda dapat menerapkan pengaturan yang sama pada API lainnya yang membaca atau menulis objek.
Tambahkan header x-oss-request-payer: requester ke permintaan Anda; tanpa header tersebut, OSS akan menolak permintaan dengan tanggapan kesalahan.
Java
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.io.ByteArrayInputStream;
public class Demo {
public static void main(String[] args) throws Exception{
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. For more information about the endpoints of other regions, see Regions and endpoints.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path.
String objectName = "exampledir/exampleobject.txt";
Payer payer = Payer.Requester;
// 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 cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSS Client instance.
// Call the shutdown method to release associated resources when the OSS Client is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Specify the payer when a third party calls the PutObject operation.
String content = "hello";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
putObjectRequest.setRequestPayer(payer);
ossClient.putObject(putObjectRequest);
// Specify the payer when a third party calls the GetObject operation.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
getObjectRequest.setRequestPayer(payer);
OSSObject ossObject = ossClient.getObject(getObjectRequest);
ossObject.close();
// Specify the payer when a third party calls the DeleteObject operation.
GenericRequest genericRequest = new GenericRequest(bucketName, objectName);
genericRequest.setRequestPayer(payer);
ossClient.deleteObject(genericRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// Shut down the OSSClient instance.
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Python
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.headers import OSS_REQUEST_PAYER
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
object_name = 'exampledir/exampleobject.txt'
headers = dict()
headers[OSS_REQUEST_PAYER] = "requester"
# Specify the x-oss-request-payer header in the request to upload the object.
result = bucket.put_object(object_name, 'test-content', headers=headers)
# Specify the x-oss-request-payer header in the request to download the object.
result = bucket.get_object(object_name, headers=headers)
# Specify the x-oss-request-payer header in the request to delete the object.
result = bucket.delete_object(object_name, headers=headers);Go
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// 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 cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
payerClient, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("New Error:", err)
os.Exit(-1)
}
// Specify the name of the bucket.
payerBucket, err := payerClient.Bucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// If pay-by-requester is enabled, external requesters must set the oss.RequestPayer(oss.Requester) parameter to access authorized content.
// If pay-by-requester is not enabled, external requesters are not required to include the oss.RequestPayer(oss.Requester) parameter to access the authorized content.
// Upload an object.
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
key := "exampledir/exampleobject.txt"
err = payerBucket.PutObject(key, strings.NewReader("objectValue"), oss.RequestPayer("requester"))
if err != nil {
fmt.Println("put Error:", err)
os.Exit(-1)
}
// List all objects in the bucket.
lor, err := payerBucket.ListObjects(oss.RequestPayer(oss.Requester))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Display the names of objects in the bucket.
for _, l := range lor.Objects {
fmt.Println("the Key name is :", l.Key)
}
// Download the object.
body, err := payerBucket.GetObject(key, oss.RequestPayer(oss.Requester))
if err != nil {
fmt.Println("Get Error:", err)
os.Exit(-1)
}
// You must close the obtained stream after the object is read. Otherwise, connection leaks may occur. Consequently, no connections are available and an exception occurs.
defer body.Close()
// Read and display the obtained content.
data, err := io.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
// Delete the object.
err = payerBucket.DeleteObject(key, oss.RequestPayer(oss.Requester))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}Node.js
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',
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of your bucket.
bucket: 'yourBucketName',
});
async function main() {
await put();
await get();
await del();
}
async function put() {
const result = await client.putBucketRequestPayment(bucket, payer);
console.log('putBucketRequestPayment:', result);
// Specify the payer when a third party calls the PutObject operation.
const response = await client.put('fileName', path.normalize('D:\\localpath\\examplefile.txt'), {
headers: {
'x-oss-request-payer': 'requester'
}
});
console.log('put:', response);
}
async function get() {
const result = await client.putBucketRequestPayment(bucket, payer);
console.log('putBucketRequestPayment:', result);
// Specify the payer when a third party calls the GetObject operation.
const response = await client.get('fileName', {
headers: {
'x-oss-request-payer': 'requester'
}
});
console.log('get:', response);
}
async function del() {
const result = await client.putBucketRequestPayment(bucket, payer);
console.log('putBucketRequestPayment:', result);
// Specify the payer when a third party calls the DeleteObject operation.
const response = await client.delete('fileName', {
headers: {
'x-oss-request-payer': 'requester'
}
});
console.log('delete:', response);
}
main();
C#
using System;
using System.IO;
using System.Text;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
var objectName = "example.txt";
var objectContent = "More than just cloud.";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
try
{
byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
MemoryStream requestContent = new MemoryStream(binaryData);
// Specify the payer when a third party calls the PutObject operation.
var putRequest = new PutObjectRequest(bucketName, objectName, requestContent);
putRequest.RequestPayer = RequestPayer.Requester;
var result = client.PutObject(putRequest);
// Specify the payer when a third party calls the GetObject operation.
var getRequest = new GetObjectRequest(bucketName, objectName);
getRequest.RequestPayer = RequestPayer.Requester;
var getResult = client.GetObject(getRequest);
// Specify the payer when a third party calls the DeleteObject operation.
var delRequest = new DeleteObjectRequest(bucketName, objectName);
delRequest.RequestPayer = RequestPayer.Requester;
client.DeleteObject(delRequest);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
}
}PHP
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
$provider = new EnvironmentVariableCredentialsProvider();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the name of the bucket. Example: examplebucket.
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
$object = "exampledir/exampleobject.txt";
// Enable pay-by-requester for the bucket.
$options = array(
OssClient::OSS_HEADERS => array(
OssClient::OSS_REQUEST_PAYER => 'requester',
));
try {
$config = array(
"provider" => $provider,
"endpoint" => $endpoint,
"signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
"region"=> "cn-hangzhou"
);
$ossClient = new OssClient($config);
// Specify the payer when a third party calls the PutObject operation.
$content = "hello";
$ossClient->putObject($bucket, $object, $content, $options);
// Specify the payer when a third party calls the GetObject operation.
$ossClient->getObject($bucket, $object, $options);
// Specify the payer when a third party calls the DeleteObject operation.
$ossClient->deleteObject($bucket, $object, $options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n"); C++
#include <alibabacloud/oss/OssClient.h>
#include <fstream>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket that the requester wants to access is located. */
std::string Endpoint = "yourEndpoint";
/* 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 cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket that the requester wants to access. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object that the requester wants to access. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Enable pay-by-requester when you upload an object. */
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);
/* Enable pay-by-requester when you download an object into memory. */
GetObjectRequest getrequest(BucketName, ObjectName);
getrequest.setRequestPayer(RequestPayer::Requester);
auto getoutcome = client.GetObject(getrequest);
/* Enable pay-by-requester when you delete an object. */
DeleteObjectRequest delrequest(BucketName, ObjectName);
delrequest.setRequestPayer(RequestPayer::Requester);
auto deloutcome = client.DeleteObject(delrequest);
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}ossutil
Instal ossutil sebelum memulai.
Untuk mengunduh objek menggunakan perintah cp, tentukan parameter --request-payer=requester.
ossutil cp oss://examplebucket/examplefile.txt /localpath --request-payer=requesterAPI
Untuk membuat permintaan API RESTful langsung, sertakan header x-oss-request-payer: requester dalam permintaan Anda dan pastikan header ini disertakan dalam perhitungan tanda tangan. Untuk petunjuk lebih lanjut, lihat Sertakan tanda tangan dalam header.
GET /oss.jpg HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 24 Feb 2012 06:38:30 GMT
Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218ePenerapan di lingkungan produksi
Atribusi tagihan untuk akses melalui peran RAM: Ketika pihak yang melakukan permintaan mengakses data dengan mengasumsikan peran RAM, akun tempat peran RAM tersebut berasal akan membayar permintaan tersebut.
Praktik yang salah: Mengizinkan pihak yang melakukan permintaan untuk mengasumsikan peran RAM dari akun pemilik bucket guna mendapatkan izin akses. Semua permintaan dijalankan sebagai pemilik bucket, sehingga pemilik bucket dikenai biaya permintaan dan trafik — hal ini bertentangan dengan tujuan fitur bayar‑per‑permintaan.
Praktik yang benar: Berikan izin akses langsung kepada pihak yang melakukan permintaan melalui kebijakan bucket.
Perangkap URL yang ditandatangani sebelumnya:
Praktik yang salah: Pemilik bucket menggunakan kredensial akses (Pasangan Kunci Akses atau kredensial sementara STS) untuk menghasilkan dan membagikan URL yang ditandatangani sebelumnya. Pihak yang melakukan permintaan menjalankan permintaan sebagai pemilik bucket, sehingga pemilik bucket yang ditagih.
Praktik yang benar: Pihak yang melakukan permintaan menghasilkan URL yang ditandatangani sebelumnya menggunakan kredensial identitas (Pasangan Kunci Akses atau kredensial sementara STS) dan menyertakan parameter
x-oss-request-payer=requester. Untuk informasi tentang perhitungan tanda tangan, lihat Sertakan tanda tangan dalam URL. Saat URL ini digunakan, pihak yang menghasilkannya akan ditagih.
Risiko kompatibilitas: Mengaktifkan fitur bayar‑per‑permintaan akan menonaktifkan akses anonim, yang diperlukan untuk hosting situs web statis. Situs web Anda menjadi tidak tersedia. Untuk menghindari masalah ini, host aset antarmuka depan situs web Anda (HTML, CSS, JS) di bucket yang terpisah dari data yang memerlukan fitur bayar‑per‑permintaan.
Tagihan
Saat fitur bayar-per-permintaan diaktifkan, pihak yang melakukan permintaan membayar item yang dapat ditagih berikut. Pemilik bucket tetap membayar semua item lainnya. Untuk daftar lengkap item yang dapat ditagih, lihat Harga OSS.
Biaya | Item yang dapat ditagih |
Trafik keluar melalui Internet | |
Trafik origin | |
Jumlah permintaan PUT | |
Jumlah permintaan GET | |
Pengambilan objek IA | |
Pengambilan objek Archive |