Todos los productos
Search
Centro de documentos

Object Storage Service:Gestión de enlaces simbólicos (OSS SDK for Go 1.0)

Última actualización: Sep 22, 2026

Los enlaces simbólicos funcionan como los accesos directos de archivos en Windows y permiten acceder rápidamente a los objetos asociados en Object Storage Service (OSS).

Notas

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • Las credenciales de acceso se obtienen de variables de entorno. Para configurar estas credenciales, consulte Configure access credentials.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Configure a client (Go SDK V1).

  • Para crear un enlace simbólico, requiere el permiso oss:PutObject. Para consultar un enlace simbólico, requiere el permiso oss:GetObject. Para obtener más información, consulte Grant a custom policy.

Creación de un enlace simbólico

A continuación se muestra un ejemplo de código para crear un enlace simbólico:

package main

import (
	"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 configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the name of the symbolic link. Example: examplesymlink. 
	symObjectKey := "examplesymlink.txt"
	// Specify the name of the object to which the symbolic link points. Example: exampleobject.txt. 
	objectName := "exampleobject.txt"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Create the symbolic link. 
	options := []oss.Option{
		// Specify whether to overwrite the object that has the same name as the symbolic link. A value of true specifies that the object is overwritten. A value of false specifies that the object is not overwritten. 
		oss.ForbidOverWrite(true),
		// Specify the access control list (ACL) of the object. In this example, the ACL is set to private. 
		oss.ObjectACL(oss.ACLPrivate),
		// Specify the storage class of the object. In this example, the storage class is set to Standard. 
		oss.StorageClass(oss.StorageStandard),
	}

	err = bucket.PutSymlink(symObjectKey, objectName, options...)
	if err != nil {
		log.Fatalf("Failed to create symlink '%s' pointing to '%s': %v", symObjectKey, objectName, err)
	}

	log.Println("Symlink created successfully")
}

Consulta del objeto de destino de un enlace simbólico

Para consultar un enlace simbólico, requiere permisos de lectura sobre dicho enlace. A continuación se muestra un ejemplo de código para consultar un enlace simbólico y obtener el nombre del objeto al que apunta:

package main

import (
	"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 configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	// Specify the name of the symbolic link. 
	symlinkName := "examplesymlink.txt"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Query the name of the object to which the symbolic link points. 
	meta, err := bucket.GetSymlink(symlinkName)
	if err != nil {
		log.Fatalf("Failed to get symlink '%s': %v", symlinkName, err)
	}

	target := meta.Get(oss.HTTPHeaderOssSymlinkTarget)
	log.Printf("Symlink '%s' points to: %s", symlinkName, target)
}

Referencias

  • Para conocer la operación de API utilizada para crear un enlace simbólico, consulte PutSymlink.

  • Para conocer la operación de API utilizada para consultar un enlace simbólico, consulte GetSymlink.