Os links simbólicos funcionam como atalhos de arquivo no Windows e permitem acessar rapidamente objetos associados no Object Storage Service (OSS).
Observações
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
As credenciais de acesso neste tópico são obtidas de variáveis de ambiente. Para saber mais sobre como configurar credenciais de acesso, consulte Configurar credenciais de acesso.
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para outras configurações, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Configurar um cliente (Go SDK V1).
Para criar um link simbólico, você precisa da permissão
oss:PutObject. Para consultar um link simbólico, você precisa da permissãooss:GetObject. Para mais detalhes, consulte Conceder uma política personalizada.
Criar um link simbólico
O código de exemplo a seguir mostra como criar um link 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")
}
Consultar o objeto de destino de um link simbólico
Para consultar um link simbólico, você precisa de permissões de leitura sobre ele. O código de exemplo a seguir mostra como consultar um link simbólico e obter o nome do objeto para o qual ele aponta:
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)
}
Referências
Para a operação de API usada para criar um link simbólico, consulte PutSymlink.
Para a operação de API usada para consultar um link simbólico, consulte GetSymlink.