All Products
Search
Document Center

Object Storage Service:Query objects

Last Updated:Sep 13, 2023

This topic describes how to query CSV and JSON objects.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To query objects, you must have oss:GetObject permission. For more information, see Common examples of RAM policies.

  • Only objects in the CSV and JSON formats can be queried.

Sample code

Query a CSV object

The following code provides an example on how to query a CSV object:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"io/ioutil"
	"os"
)

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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampledata.csv. 
	key := "exampledir/exampledata.csv"
	// Specify the full path of the local CSV file. Example: D:\\localpath\\exampledata.csv. 
	localCsvFile := "D:\\localpath\\exampledata.csv"
	err = bucket.PutObjectFromFile(key, localCsvFile)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	selReq := oss.SelectRequest{}
	// Execute the SELECT statement to query data in the object. 
	selReq.Expression = `select * from ossobject`
	body, err := bucket.SelectObject(key, selReq)

	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Read the content of the object. 
	fc, err := ioutil.ReadAll(body)

	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	defer body.Close()
	fmt.Println(string(fc))
}

Query a JSON object

The following code provides an example on how to query a JSON object:

package main

import (
	"fmt"
	"io/ioutil"
	"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. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket. 
	bucket, err := client.Bucket("examplebucket")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampledata.json. 
	key := "exampledir/exampledata.json"
	// Specify the full path of the local JSON file. Example: D:\\localpath\\exampledata.json. 
	localJsonFile := "D:\\localpath\\examplepdata.json"
	err = bucket.PutObjectFromFile(key, localJsonFile)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	selReq := oss.SelectRequest{}
	// Execute the SELECT statement to query data in the object. 
	selReq.Expression = `select * from ossobject`
	body, err := bucket.SelectObject(key, selReq)

	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Read the content of the object. 
	fc, err := ioutil.ReadAll(body)

	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	defer body.Close()
	fmt.Println(string(fc))
}

References

  • For the complete sample code that is used to query objects, visit GitHub.

  • For more information about the API operation that you can call to query objects, see SelectObject.