Sembolik bağlantılar, Windows'taki dosya kısayolları gibi çalışır ve Object Storage Service (OSS) içindeki ilişkili nesnelere hızlıca erişmenizi sağlar.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırın.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneğinin nasıl oluşturulacağını gösterir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemciyi yapılandırın (Go SDK V1).
Sembolik bağlantı oluşturmak için
oss:PutObjectiznine sahip olmanız gerekir. Sembolik bağlantıyı sorgulamak içinoss:GetObjectiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atayın.
Sembolik bağlantı oluşturun
Aşağıdaki örnek kod, sembolik bağlantının nasıl oluşturulacağını gösterir:
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")
}
Sembolik bağlantının hedef nesnesini sorgulayın
Sembolik bağlantıyı sorgulamak için sembolik bağlantı üzerinde okuma izniniz olmalıdır. Aşağıdaki örnek kod, sembolik bağlantının ve bu bağlantının işaret ettiği nesnenin adının nasıl sorgulanacağını gösterir:
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)
}
İlgili konular
Sembolik bağlantı oluşturmak için kullanılan API işlemi hakkında bilgi edinmek için bkz. PutSymlink.
Sembolik bağlantıyı sorgulamak için kullanılan API işlemi hakkında bilgi edinmek için bkz. GetSymlink.