Veri dizinleme özelliğini kullanarak ad, ETag, depolama sınıfı, boyut ve son değiştirilme zamanı gibi belirli meta veri koşullarını karşılayan nesneleri hızla bulabilirsiniz. Sorgu sonuçlarını iş gereksinimlerinize göre sıralayabilir ve toplayabilirsiniz; bu sayede büyük veri yığınları arasında belirli nesneleri konumlandırma verimliliğiniz artar.
Notlar
OSS SDK for Go 2.2.5 ve sonraki sürümler veri dizinleme özelliğini destekler.
Bu konuda China (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgede bulunan diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili uç noktayı kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda kimlik bilgileri ortam değişkenlerinden alınır. Kimlik bilgilerini yapılandırma 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ği oluşturmayı gösterir. Özel alan adı kullanımı 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 için meta veri yönetimi özelliğini etkinleştirme
Aşağıdaki örnek kod, bir paket için meta veri yönetimi özelliğinin nasıl etkinleştirileceğini gösterir. Bu özelliği etkinleştirdikten sonra OSS bir meta veri dizin kitaplığı oluşturur ve paketteki mevcut tüm nesneleri dizine ekler. OSS daha sonra artımlı nesneleri yarı gerçek zamanlı olarak tarar ve otomatik olarak dizine ekler.
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)
}
// Enable the metadata management feature.
err = client.OpenMetaQuery("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Open data query success")
}
Paketin meta veri dizin kitaplığını sorgulama
Aşağıdaki kod, bir paketin meta veri dizin kitaplığının nasıl sorgulanacağını gösterir:
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 information about the metadata index library.
result, err := client.GetMetaQueryStatus("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("State:%s\n", result.State)
fmt.Printf("Phase:%s\n", result.Phase)
fmt.Printf("CreateTime:%s\n", result.CreateTime)
fmt.Printf("UpdateTime:%s\n", result.UpdateTime)
}
Belirli koşulları karşılayan nesneleri sorgulama
Aşağıdaki örnek kod, belirli koşulları karşılayan nesnelerin nasıl sorgulanacağını ve nesne bilgilerinin belirli alanlara ve sıralama yöntemlerine göre nasıl listeleneceğini gösterir:
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 objects that are larger than 30 bytes in size, return up to 10 objects at the same time, and then sort the objects in ascending order.
query := oss.MetaQuery{
NextToken: "",
MaxResults: 10,
Query: `{"Field": "Size","Value": "30","Operation": "gt"}`,
Sort: "Size",
Order: "asc",
}
// Query objects that match the specified conditions and list object information based on the specified fields and sorting methods.
result, err := client.DoMetaQuery("examplebucket", query)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("NextToken:%s\n", result.NextToken)
for _, file := range result.Files {
fmt.Printf("File name: %s\n", file.Filename)
fmt.Printf("size: %d\n", file.Size)
fmt.Printf("File Modified Time:%s\n", file.FileModifiedTime)
fmt.Printf("Oss Object Type:%s\n", file.OssObjectType)
fmt.Printf("Oss Storage Class:%s\n", file.OssStorageClass)
fmt.Printf("Object ACL:%s\n", file.ObjectACL)
fmt.Printf("ETag:%s\n", file.ETag)
fmt.Printf("Oss CRC64:%s\n", file.OssCRC64)
fmt.Printf("Oss Tagging Count:%d\n", file.OssTaggingCount)
for _, tagging := range file.OssTagging {
fmt.Printf("Oss Tagging Key:%s\n", tagging.Key)
fmt.Printf("Oss Tagging Value:%s\n", tagging.Value)
}
for _, userMeta := range file.OssUserMeta {
fmt.Printf("Oss User Meta Key:%s\n", userMeta.Key)
fmt.Printf("Oss User Meta Key Value:%s\n", userMeta.Value)
}
}
}
Paket için meta veri yönetimi özelliğini devre dışı bırakma
Aşağıdaki örnek kod, bir paket için meta veri yönetimi özelliğinin nasıl devre dışı bırakılacağını gösterir:
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)
}
// Disable the metadata management feature.
err = client.CloseMetaQuery("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("Closed data query success")
}
İlgili Konular
Meta veri yönetimi özelliğini etkinleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. OpenMetaQuery.
Meta veri dizin kitaplığı hakkında bilgi sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetMetaQueryStatus.
Belirli koşulları karşılayan nesneleri sorgulamak ve nesne bilgilerini belirli alanlara ve sıralama yöntemlerine göre listelemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DoMetaQuery.
Meta veri yönetimi özelliğini devre dışı bırakmak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. CloseMetaQuery.