Tous les produits
Search
Centre de documentation

Object Storage Service:Interroger des fichiers (Go SDK V1)

Dernière mise à jour :Sep 22, 2026

Cette rubrique explique comment utiliser la méthode Bucket.SelectObject pour interroger des fichiers CSV et JSON.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour plus d'informations sur les régions et les points de terminaison OSS, consultez la page Régions et points de terminaison.

  • Les identifiants d'accès utilisés dans cette rubrique sont récupérés depuis des variables d'environnement. Pour savoir comment configurer vos identifiants d'accès, consultez la page Configurer les identifiants d'accès.

  • Cette rubrique montre comment créer une instance OSSClient avec un point de terminaison OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez la page Configurer un client (Go SDK V1).

  • Pour interroger des objets, vous devez disposer de l'autorisation oss:GetObject. Pour plus d'informations, consultez la section Accorder une politique personnalisée.

  • Seuls les objets aux formats CSV et JSON peuvent être interrogés via SelectObject.

Exemple de code

Interroger un objet CSV

Le code suivant montre comment interroger un fichier CSV.

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))
}

Interroger un objet JSON

Le code suivant montre comment interroger un fichier JSON.

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))
}

Références

  • Pour obtenir l'exemple de code complet relatif à l'interrogation de fichiers, consultez l'exemple GitHub.

  • Pour en savoir plus sur l'opération API permettant d'interroger des fichiers, consultez SelectObject.