Object Storage Service (OSS) içinde bir paket için istek sahibine göre ödeme özelliği etkinleştirildiğinde, istek ve trafik ücretleri paket sahibi yerine isteği yapan kişiye yansıtılır. Paket sahibi yalnızca depolama ücretlerinden sorumlu olur. Bir paket için bu özelliği etkinleştirerek, paketinize yapılan erişimlerden doğan istek ve trafik ücretlerini ödemeden verilerinizi paylaşabilirsiniz.
Notlar
Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili bir uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda kimlik doğrulama bilgileri ortam değişkenlerinden alınır. Kimlik doğrulama bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Kimlik doğrulama bilgilerini yapılandırma.
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 bkz. İstemciyi yapılandırma (Go SDK V1).
İstek sahibine göre ödeme özelliğini etkinleştirmek için
oss:PutBucketRequestPaymentiznine sahip olmanız gerekir. İstek sahibine göre ödeme yapılandırmalarını sorgulamak içinoss:GetBucketRequestPaymentiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
İstek sahibine göre ödemeyi etkinleştirme
Aşağıdaki kod, bir paket için istek sahibine göre ödeme özelliğinin nasıl etkinleştirileceğine ilişkin bir örnek sunar:
package main
import (
"fmt"
"os"
"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 configured.
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))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("New Error:", err)
os.Exit(-1)
}
// Initialize the pay-by-requester mode.
reqPayConf := oss.RequestPaymentConfiguration{
Payer: "Requester",
}
// Enable pay-by-requester for the bucket.
err = client.SetBucketRequestPayment("<yourBucketName>", reqPayConf)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Bir paketin istek sahibine göre ödeme yapılandırmalarını sorgulama
Aşağıdaki kod, bir paketin istek sahibine göre ödeme yapılandırmalarının nasıl sorgulanacağına ilişkin bir örnek sunar:
package main
import (
"fmt"
"os"
"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 configured.
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))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Query the pay-by-requester configurations of the bucket.
ret, err := client.GetBucketRequestPayment("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Display the pay-by-requester configurations of the bucket.
fmt.Println("Bucket request payer:", ret.Payer)
}
Üçüncü tarafların nesnelere erişiminde ücretlerin üçüncü taraflara yansıtılacağını belirtme
Bir paketteki nesnelere erişim ücretlerinin üçüncü taraflara yansıtılacağını belirtirseniz, istekte bulunanların nesneleriniz üzerinde işlem gerçekleştirebilmek için HTTP isteklerine x-oss-request-payer:requester başlığını eklemesi gerekir. Bu başlık eklenmezse bir hata döndürülür.
Aşağıdaki kod, PutObject, GetObject ve DeleteObject işlemlerini çağırarak nesnelere erişim ücretlerinin üçüncü taraflara nasıl yansıtılacağına ilişkin 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 de bu yöntemi kullanabilirsiniz.
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)
}
}
İlgili kaynaklar
İstek sahibine göre ödeme özelliğini yapılandırmaya ilişkin eksiksiz örnek kod için GitHub adresini ziyaret edin.
İstek sahibine göre ödeme özelliğini etkinleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketRequestPayment.
İstek sahibine göre ödeme yapılandırmalarını sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketRequestPayment.