Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:İstek başına ödeme (OSS SDK for Java 1.0)

Son Güncelleme:Oct 01, 2026

Object Storage Service (OSS) içinde bir paket için istek başına ödeme etkinleştirildiğinde, istek ve trafik ücretleri paket sahibinin yerine isteği yapan kişi tarafından ödenir. Paket sahibi yalnızca depolama ücretlerinden sorumludur. Bir paket için istek başına ödemeyi etkinleştirerek, paketinize yapılan erişimlerden kaynaklanan istek ve trafik ücretlerini ödemeden paketinizdeki verileri paylaşabilirsiniz.

Kullanım notları

  • Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili uç noktayı kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için Bölgeler ve uç noktalar konusuna bakın.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Daha fazla bilgi için Configure access credentials konusuna bakın.

  • Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için İstemci yapılandırması konusuna bakın.

  • İstek başına ödemeyi etkinleştirmek için oss:PutBucketRequestPayment iznine sahip olmanız gerekir. İstek başına ödeme yapılandırmalarını sorgulamak için oss:GetBucketRequestPayment iznine sahip olmanız gerekir. Daha fazla bilgi için Özel ilke atama konusuna bakın.

Bir paket için istek başına ödemeyi etkinleştirme

Aşağıdaki kod, bir paket için istek başına ödemeyi nasıl etkinleştireceğinize dair bir örnek sunar:

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.*;

public class Demo {
    public static void main(String[] args) throws Exception{
        // In this example, the endpoint of the China (Hangzhou) region is used. Replace it with your actual endpoint. 
        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 set. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // 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 OSSClient 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 {
            // Enable pay-by-requester for the bucket. 
            Payer payer = Payer.Requester;
            ossClient.setBucketRequestPayment(bucketName, payer);
        } 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();
            }
        }
    }
}

Bir paketin istek başına ödeme yapılandırmalarını sorgulama

Aşağıdaki kod, bir paketin istek başına ödeme yapılandırmalarını nasıl sorgulayacağınıza dair bir örnek sunar:

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.*;

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. 
        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 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 {
            // Query the pay-by-requester configurations of the bucket. 
            GetBucketRequestPaymentResult result = ossClient.getBucketRequestPayment(bucketName);
            System.out.println("Payer:" + result.getPayer());
        } 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();
            }
        }
    }
}            

Nesnelere erişen üçüncü tarafların ücretlendirmesini belirtme

Bir paketteki nesnelere erişim ücretlerinin üçüncü taraflara yansıtılmasını belirtirseniz, istekte bulunanların nesneleriniz üzerinde işlem gerçekleştirebilmesi için HTTP isteklerine x-oss-request-payer:requester üst bilgisini eklemesi gerekir. Bu üst bilgi eklenmezse hata döndürülür.

Aşağıdaki kod, PutObject, GetObject ve DeleteObject işlemlerini çağırarak nesnelere erişen üçüncü tarafların ücretlendirmesini nasıl belirteceğinize dair bir örnek sunar. Benzer şekilde, diğer API işlemlerini çağırarak nesneler üzerinde okuma ve yazma işlemleri gerçekleştiren üçüncü tarafların ücretlendirilmesini belirtmek için aynı yöntemi kullanabilirsiniz.

Aşağıdaki kod, nesnelere erişen üçüncü tarafların ücretlendirmesini nasıl belirteceğinize dair bir örnek sunar:

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();
            }
        }
    }
}

İlgili konular

  • İstek başına ödemeyi yapılandırmak için kullanılan tam örnek kodu görmek üzere GitHub adresini ziyaret edin.

  • İstek başına ödemeyi etkinleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için PutBucketRequestPayment konusuna bakın.

  • İstek başına ödeme yapılandırmalarını sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için GetBucketRequestPayment konusuna bakın.