You can use symbolic links to quickly access frequently used files in an Object Storage Service (OSS) bucket. A symbolic link is similar to a Windows shortcut. After you create a symbolic link, you can use it to quickly open the source file. This topic describes how to manage symbolic links in a versioning-enabled bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To create a symbolic link, you must have the
oss:PutObjectpermission. To query a symbolic link, you must have theoss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Create a symbolic link
A symbolic link can have multiple versions, and each version can point to a different target object. OSS automatically generates a version ID and returns it in the `x-oss-version-id` response header. You can create a symbolic link that points to the current version of the target object.
In a versioning-enabled bucket, you cannot create a symbolic link for a delete marker.
The following code shows how to create a symbolic link:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
objectName = 'yourTargetObjectName'
symlink = 'yourSymlink'
# Upload the symbolic link.
result = bucket.put_symlink(objectName, symlink)
# View the version ID of the uploaded symbolic link.
print('symlink versionid:', result.versionid)Get a symbolic link
The GetSymlink operation retrieves the current version of a symbolic link by default. You can specify a version ID to retrieve a specific version. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found error. The response header includes `x-oss-delete-marker = true` and the version ID `x-oss-version-id`.
To retrieve a symbolic link, you must have read permissions for it.
The following code shows how to retrieve a symbolic link:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
symlink = 'yourSymlink'
# Get a specific version of the symbolic link file.
params = dict()
params['versionId'] = 'yourSymlinkVersionId'
result = bucket.get_symlink(symlink, params=params)
# View the version ID of the returned symbolic link file.
print('get symlink versionid:', result.versionid)References
For more information about the API operation to create a symbolic link, see PutSymlink.
For more information about the API operation to retrieve a symbolic link, see GetSymlink.