Les liens symboliques fonctionnent comme des raccourcis de fichiers sous Windows et vous permettent d'accéder rapidement aux objets associés dans Object Storage Service (OSS).
Notes
Cette rubrique utilise l'endpoint 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 endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Dans cette rubrique, les identifiants d'accès sont récupérés à partir des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès.
Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configurer un client (Go SDK V1).
Pour créer un lien symbolique, vous devez disposer de l'autorisation
oss:PutObject. Pour interroger un lien symbolique, vous devez disposer de l'autorisationoss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Créer un lien symbolique
L'exemple de code suivant montre comment créer un lien symbolique :
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")
}
Interroger l'objet cible d'un lien symbolique
Pour interroger un lien symbolique, vous devez disposer des autorisations de lecture sur ce lien. L'exemple de code suivant montre comment interroger un lien symbolique et obtenir le nom de l'objet ciblé par ce lien :
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)
}
Références
Pour l'opération API utilisée pour créer un lien symbolique, consultez PutSymlink.
Pour l'opération API utilisée pour interroger un lien symbolique, consultez GetSymlink.