Query files using OSS SDK for Go 2.0
This topic describes how to use the SelectObject method to query data in CSV and JSON files.
Usage notes
-
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints. -
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
-
Querying files requires the
oss:GetObjectpermission. For more information, see Grant custom permissions to a RAM user. -
OSS supports querying only CSV and JSON files.
Method definition
func (c *Client) SelectObject(ctx context.Context, request *SelectObjectRequest, optFns ...func(*Options)) (*SelectObjectResult, error)
Request parameters
|
Parameter |
Type |
Description |
|
ctx |
context.Context |
The context of the request. You can use this parameter to set the total timeout period for the request. |
|
request |
*SelectObjectRequest |
The request parameters of the operation. For more information, see SelectObjectRequest |
|
optFns |
...func(*Options) |
(Optional) The operation-level configuration parameters. For more information, see Options |
Response parameters
|
Parameter |
Type |
Description |
|
result |
*SelectObjectResult |
The response parameters of the operation, valid only when err is nil. For more information, see SelectObjectResult |
|
err |
error |
The status of the request. If the request fails, err is not nil. |
Sample code
Query a CSV file
You can use the following sample code to execute an SQL statement on a target CSV file and return the execution result.
package main
import (
"context"
"flag"
"io"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The storage region.
bucketName string // The bucket name.
objectName string // The object name.
)
// The init function is used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and set the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to select an object.
request := &oss.SelectObjectRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(objectName), // The object name.
SelectRequest: &oss.SelectRequest{
Expression: oss.Ptr("select * from ossobject limit 10"), // Define an SQL expression to query the first 10 rows of data in the object.
InputSerializationSelect: oss.InputSerializationSelect{
CsvBodyInput: &oss.CSVSelectInput{
FileHeaderInfo: oss.Ptr("Use"),
},
},
OutputSerializationSelect: oss.OutputSerializationSelect{
OutputHeader: oss.Ptr(true),
},
},
}
// Execute the request to select the object.
result, err := client.SelectObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to select object %v", err)
}
content, err := io.ReadAll(result.Body)
if err != nil {
log.Fatalf("failed to read object %v", err)
}
// Print the result of selecting the object.
log.Printf("select object result:%#v\n", string(content))
}
Query a JSON file
You can use the following sample code to execute an SQL statement on a target JSON file and return the execution result.
package main
import (
"context"
"flag"
"io"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The storage region.
bucketName string // The bucket name.
objectName string // The object name.
)
// The init function is used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
flag.StringVar(&objectName, "object", "", "The name of the object.")
}
func main() {
// Parse command line parameters.
flag.Parse()
// Check whether the bucket name is empty.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is empty.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Check whether the object name is empty.
if len(objectName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Load the default configurations and set the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to select an object.
request := &oss.SelectObjectRequest{
Bucket: oss.Ptr(bucketName), // The bucket name.
Key: oss.Ptr(objectName), // The object name.
SelectRequest: &oss.SelectRequest{
Expression: oss.Ptr("select * from ossobject limit 10"), // Define an SQL expression to query the first 10 rows of data in the object.
InputSerializationSelect: oss.InputSerializationSelect{
JsonBodyInput: &oss.JSONSelectInput{
JSONType: oss.Ptr("DOCUMENT"),
},
},
OutputSerializationSelect: oss.OutputSerializationSelect{
OutputHeader: oss.Ptr(true),
},
},
}
// Send the request to select the object.
result, err := client.SelectObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to select object %v", err)
}
content, err := io.ReadAll(result.Body)
if err != nil {
log.Fatalf("failed to read object %v", err)
}
// Print the result of selecting the object.
log.Printf("select object result:%#v\n", string(content))
}
References
-
For the complete sample code for querying files, see GitHub sample.
-
For more information about the API operation used to query files, see SelectObject.
-
For more information about querying files, see Query files.