All Products
Search
Document Center

Object Storage Service:Query files (Go SDK V1)

Last Updated:Mar 20, 2026

Bucket.SelectObject runs a SQL SELECT statement against a CSV or JSON file stored in OSS and returns matching rows as a stream — without downloading the entire file first.

Prerequisites

Before you begin, make sure you have:

Usage notes

  • SelectObject supports only CSV and JSON objects.

  • The examples on this page use the public endpoint for the China (Hangzhou) region (https://oss-cn-hangzhou.aliyuncs.com). If your application runs in the same region as your bucket, use the internal endpoint instead. For region-specific endpoints, see Regions and endpoints.

  • To create an OSSClient instance using a custom domain or Security Token Service (STS), see Configure OSSClient instances.

Sample code

Query a CSV object

The following example uploads a local CSV file to OSS and then queries it using select * from ossobject.

package main

import (
	"io"
	"log"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Load credentials from environment variables.
	// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Error getting credentials: %v", err)
	}

	// Create an OSSClient instance.
	// Replace the endpoint and region with your bucket's values.
	// China (Hangzhou): endpoint = https://oss-cn-hangzhou.aliyuncs.com, region = cn-hangzhou
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("cn-hangzhou"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Error creating OSS client: %v", err)
	}

	// Get a bucket handle. Replace examplebucket with your bucket name.
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		log.Fatalf("Error getting bucket: %v", err)
	}

	// Upload a local CSV file to OSS.
	// Replace the object key and local path with your actual values.
	objectKey := "exampledir/exampledata.csv"
	localCsvFile := "D:\\localpath\\exampledata.csv"
	if err := bucket.PutObjectFromFile(objectKey, localCsvFile); err != nil {
		log.Fatalf("Error uploading file: %v", err)
	}

	// Build a SelectRequest with the SQL expression to run.
	// The table name in the FROM clause is always ossobject.
	selectRequest := oss.SelectRequest{
		Expression: `select * from ossobject`,
	}

	// Run the query. The result is returned as a streaming response body.
	body, err := bucket.SelectObject(objectKey, selectRequest)
	if err != nil {
		log.Fatalf("Error selecting object: %v", err)
	}
	defer body.Close()

	// Read and print the query result.
	content, err := io.ReadAll(body)
	if err != nil {
		log.Fatalf("Error reading content: %v", err)
	}
	log.Printf("Selected content: %s", string(content))
}

`SelectRequest` fields:

FieldTypeDescriptionExample
ExpressionstringSQL SELECT statement to run. The table name in the FROM clause is always ossobject.select * from ossobject

Query a JSON object

The setup is the same as the CSV example. Change the object key and local file path to point to your JSON file:

objectKey := "exampledir/exampledata.json"
localJsonFile := "D:\\localpath\\exampledata.json"

Pass the JSON object key to bucket.SelectObject with the same SelectRequest struct. All client initialization, upload, result reading, and error-handling steps are identical.

For a complete working example, see GitHub example: select_object.go.

References