Bu konu, bir paket için envanterin nasıl oluşturulacağını ve bir paket için yapılandırılan envanterlerin nasıl sorgulanacağını, listeleneceğini ve silineceğini açıklar.
Notlar
Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye 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, 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 konuda, bir OSS uç noktası kullanılarak bir OSSClient örneğinin oluşturulması gösterilmektedir. Özel bir etki alanı kullanmak veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulaması yapmak gibi alternatif yapılandırmalar için bkz. Bir istemci yapılandırma (Go SDK V1).
Bir paket için envanter oluşturma, görüntüleme, listeleme ve silme izinlerine sahip olduğunuzdan emin olun. Varsayılan olarak, paket sahibi önceki işlemleri gerçekleştirme iznine sahiptir. Önceki işlemleri gerçekleştirme izniniz yoksa paket sahibinden size bu izinleri vermesini isteyin.
Bir paket için en fazla 1.000 envanter yapılandırabilirsiniz.
Envanter yapılandırmak istediğiniz kaynak paketi, envanter listesinin depolandığı hedef paketle aynı bölgede dağıtmanız gerekir.
Bir paket için envanter oluşturma
Aşağıdaki kod, bir paket için envanterin nasıl oluşturulacağını gösteren 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)
}
IsEnabled := true
// The following code provides an example on how to encrypt the inventory lists by using customer master keys (CMKs) managed by Key Management Service (KMS).
//var invEncryption oss.InvEncryption
//var invSseOss oss.InvSseOss
//var invSseKms oss.InvSseKms
//invSseKms.KmsId = "<yourKmsId>" // Specify the ID of CMK that is managed by KMS.
//invEncryption.SseOss = &invSseOss // Use OSS-managed keys (SSE-OSS) to encrypt the inventory lists.
//invEncryption.SseKms = &invSseKms // Use CMKs managed by KMS (SSE-KMS) to encrypt inventory lists.
invConfig := oss.InventoryConfiguration{
// Specify the name of the inventory. The name must be globally unique in the current bucket.
Id: "yourInventoryId2",
// Enable the inventory.
IsEnabled: &IsEnabled,
// Specify the rule that is used to filter the objects included in inventories. The following code provides an example on how to filter the objects by prefix.
Prefix: "yourFilterPrefix",
OSSBucketDestination: oss.OSSBucketDestination{
// Specify the format of the exported inventory lists.
Format: "CSV",
// Specify the ID of the account that is granted permissions by the bucket owner to perform the operation. Example: 109885487000****.
AccountId: "yourGrantAccountId",
// Specify the name of the RAM role that is granted permissions by the bucket owner to perform the operation. Example: acs:ram::109885487000****:role/ram-test.
RoleArn: "yourRoleArn",
// Specify the name of the bucket in which you want to store the generated inventory lists.
Bucket: "acs:oss:::" + "yourDestBucketName",
// Specify the prefix of the path in which you want to store the generated inventory lists.
Prefix: "yourDestPrefix",
// The following sample code provides an example on how to encrypt inventory lists.
//Encryption: &invEncryption,
},
// Specify the frequency at which inventory lists are exported.
Frequency: "Daily",
// Specify whether to include all versions of objects or only the current versions of objects in the inventory lists.
IncludedObjectVersions: "All",
OptionalFields: oss.OptionalFields{
// Specify the fields that are included in inventory lists.
Field: []string{
"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
},
},
}
// Specify the name of the bucket for which you want to configure the inventory.
err = client.SetBucketInventory("yourBucketName", invConfig)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Bir paketin envanterlerini sorgulama
Aşağıdaki kod, bir paket için yapılandırılan bir envanterin nasıl sorgulanacağını gösteren bir örnek sunar:
package main
import (
"encoding/xml"
"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)
}
// Specify the name of the bucket
// Specify the name of the inventory.
result, err := client.GetBucketInventory("yourBucketName", "yourInventoryId")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Display the inventory.
bs, err := xml.MarshalIndent(result, " ", " ")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println(string(bs))
}
Birden çok envanteri aynı anda listeleme
Tek bir istekte en fazla 100 envanter sorgulayabilirsiniz. 100'den fazla envanteri sorgulamak için birden çok istek göndermeniz ve her istek için döndürülen belirteci sonraki isteğin parametresi olarak kullanmanız gerekir.
Aşağıdaki kod, bir paket için yapılandırılan envanterlerin nasıl listeleneceğini gösteren bir örnek sunar:
package main
import (
"encoding/xml"
"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)
}
var sumResult oss.ListInventoryConfigurationsResult
vmarker := ""
for {
// Specify the name of the bucket
listResult, err := client.ListBucketInventory("yourBucketName", vmarker)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
sumResult.InventoryConfiguration = append(sumResult.InventoryConfiguration, listResult.InventoryConfiguration...)
if listResult.IsTruncated != nil && *listResult.IsTruncated {
vmarker = listResult.NextContinuationToken
} else {
break
}
}
// Display the inventories of the bucket.
bs, err := xml.MarshalIndent(sumResult, " ", " ")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println(string(bs))
}
Bir paketin envanterini silme
Aşağıdaki kod, bir paket için yapılandırılan bir envanterin nasıl silineceğini gösteren 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)
}
// Delete the inventory.
// Specify the name of the bucket
// Specify the name of the inventory.
err = client.DeleteBucketInventory("yourBucketName", "yourInventoryId")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
İlgili konular
Paket envanterlerini yönetmeye ilişkin tam örnek kod için GitHub adresini ziyaret edin.
Bir paket için envanter oluşturmaya yönelik API işlemi hakkında daha fazla bilgi için bkz. PutBucketInventory.
Bir paket için yapılandırılan bir envanteri sorgulamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. GetBucketInventory.
Bir paket için yapılandırılan envanterleri listelemeye yönelik API işlemi hakkında daha fazla bilgi için bkz. ListBucketInventory.
Bir paket için yapılandırılan envanterleri silmeye yönelik API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketInventory.