Bu konu, CSV ve JSON dosyalarını sorgulamak için Bucket.SelectObject yönteminin nasıl kullanılacağını açıklar.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek isterseniz 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 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ö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).
Nesneleri sorgulamak için
oss:GetObjectiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.SelectObject kullanılarak yalnızca CSV ve JSON biçimindeki nesneler sorgulanabilir.
Örnek kod
CSV nesnesini sorgulama
Aşağıdaki kod, bir CSV dosyasının nasıl sorgulanacağına ilişkin bir örnek sunar.
package main
import (
"io"
"log"
"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 {
log.Fatalf("Error getting credentials: %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("Error creating OSS client: %v", err)
}
// Specify the bucket name. For example, examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Error getting bucket: %v", err)
}
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampledata.csv.
objectKey := "exampledir/exampledata.csv"
// Specify the full path of the local CSV file. For example, D:\\localpath\\exampledata.csv.
localCsvFile := "D:\\localpath\\exampledata.csv"
// Upload the local file to OSS.
if err := bucket.PutObjectFromFile(objectKey, localCsvFile); err != nil {
log.Fatalf("Error uploading file: %v", err)
}
// Use a SELECT statement to query data from the file.
selectRequest := oss.SelectRequest{
Expression: `select * from ossobject`,
}
// Execute the SELECT request.
body, err := bucket.SelectObject(objectKey, selectRequest)
if err != nil {
log.Fatalf("Error selecting object: %v", err)
}
defer body.Close()
// Read the content.
content, err := io.ReadAll(body)
if err != nil {
log.Fatalf("Error reading content: %v", err)
}
// Print the result.
log.Printf("Selected content: %s", string(content))
}
JSON nesnesini sorgulama
Aşağıdaki kod, bir JSON dosyasının nasıl sorgulanacağına ilişkin bir örnek sunar.
package main
import (
"io"
"log"
"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 {
log.Fatalf("Error getting credentials: %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("Error creating OSS client: %v", err)
}
// Specify the bucket name. For example, examplebucket.
bucketName := "examplebucket"
bucket, err := client.Bucket(bucketName)
if err != nil {
log.Fatalf("Error getting bucket: %v", err)
}
// Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampledata.json.
objectKey := "exampledir/exampledata.json"
// Specify the full path of the local JSON file. For example, D:\\localpath\\exampledata.json.
localJsonFile := "D:\\localpath\\exampledata.json"
// Upload the local file to OSS.
if err := bucket.PutObjectFromFile(objectKey, localJsonFile); err != nil {
log.Fatalf("Error uploading file: %v", err)
}
// Use a SELECT statement to query data from the file.
selectRequest := oss.SelectRequest{
Expression: `select * from ossobject`,
}
// Execute the SELECT request.
body, err := bucket.SelectObject(objectKey, selectRequest)
if err != nil {
log.Fatalf("Error selecting object: %v", err)
}
defer body.Close()
// Read the content.
content, err := io.ReadAll(body)
if err != nil {
log.Fatalf("Error reading content: %v", err)
}
// Print the result.
log.Printf("Selected content: %s", string(content))
}
İlgili kaynaklar
Dosya sorgulama için tam örnek kod için bkz. GitHub örneği.
Dosya sorgulama API işlemi hakkında daha fazla bilgi için bkz. SelectObject.