Paket ilkesi, Object Storage Service (OSS) paketleri için bir yetkilendirme ilkesidir. Alibaba Cloud hesapları, Resource Access Management (RAM) kullanıcıları ve RAM rolleri gibi kimliği doğrulanmış ziyaretçilere veya anonim ziyaretçilere belirli OSS kaynaklarına yönelik ayrıntılı erişim izni vermek veya bu erişimi reddetmek için paket ilkesini kullanabilirsiniz. Örneğin, başka bir Alibaba Cloud hesabına ait bir RAM kullanıcısına belirli OSS kaynaklarında salt okunur izinler verebilirsiniz.
Notlar
Paket ilkesini yapılandırmadan önce bu özelliği anladığınızdan emin olun. Daha fazla bilgi için bkz. Paket ilkesi.
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili 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 erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin oluşturulmasını göstermektedir. Ö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).
Paket ilkesi ayarlamak için
oss:PutBucketPolicyiznine sahip olmanız gerekir. Paket ilkesini almak içinoss:GetBucketPolicyiznine sahip olmanız gerekir. Paket ilkesini silmek içinoss:DeleteBucketPolicyiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke verme.
Örnek kod
Paket ilkesi ayarlama
Aşağıdaki kod, paket ilkesinin nasıl ayarlanacağını gösterir:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 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.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// The following example shows how a resource owner (the bucket owner with UID 174649585760xxxx) uses a bucket policy to grant a specified user (the RAM user with UID 20214760404935xxxx) permissions to list all files in the examplebucket.
policyConfig := `
{
"Statement": [
{
"Action": [
"oss:GetObject",
"oss:ListObjects"
],
"Principal": [
"20214760404935xxxx"
],
"Effect" : "Allow",
"Resource" : ["acs:oss:*:174649585760xxxx:examplebucket/*"]
}
],
"Version": "1"
}`
// Set the bucket policy.
bucketName := "examplebucket"
err = client.SetBucketPolicy(bucketName, policyConfig)
if err != nil {
log.Fatalf("Failed to set bucket policy for '%s': %v", bucketName, err)
}
log.Println("SetBucketPolicy success")
}
Paket ilkesini alma
Aşağıdaki kod, paket ilkesinin nasıl alınacağını gösterir:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 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.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Get the bucket policy configuration.
bucketName := "yourBucketName"
strPolicy, err := client.GetBucketPolicy(bucketName)
if err != nil {
log.Fatalf("Failed to get bucket policy for '%s': %v", bucketName, err)
}
log.Printf("Bucket policy for '%s': %s", bucketName, strPolicy)
}
Paket ilkesini silme
Aşağıdaki kod, paket ilkesinin nasıl silineceğini gösterir:
package main
import (
"log"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// 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.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
log.Fatalf("Failed to create credentials provider: %v", err)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
log.Fatalf("Failed to create OSS client: %v", err)
}
// Delete the bucket policy.
bucketName := "yourBucketName"
err = client.DeleteBucketPolicy(bucketName)
if err != nil {
log.Fatalf("Failed to delete bucket policy for '%s': %v", bucketName, err)
}
log.Println("DeleteBucketPolicy success")
}
İlgili bağlantılar
Paket ilkesi ayarlamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. SetBucketPolicy.
Paket ilkesini almaya yönelik API işlemi hakkında daha fazla bilgi için bkz. GetBucketPolicy.
Paket ilkesini silmeye yönelik API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketPolicy.