All Products
Search
Document Center

Object Storage Service:Manage symbolic links (Python SDK V1)

Last Updated:Nov 26, 2025

Symbolic links work like file shortcuts on Windows and allow you to quickly access associated objects in Object Storage Service (OSS).

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 OSS 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:PutObject permission. To query a symbolic link, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Create a symbolic link

The following sample code provides an example on 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 this 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 endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the object to which the symbolic link points, for example, exampleobject.txt. For more information about object naming conventions, see Object.
object_name = 'exampleobject.txt'
# Specify the name of the symbolic link, for example, examplesymlick.txt.
symlink = 'examplesymlick.txt'

# headers = dict()
# Specify whether to overwrite an existing object that has the same name when you create the symbolic link. In this example, this parameter is set to true, which indicates that overwriting is prohibited.
# headers['x-oss-forbid-overwrite'] = 'true'
# Specify the access control list (ACL) of the object. In this example, this parameter is set to OBJECT_ACL_PRIVATE, which indicates that the object has private access permissions. Only the object owner and authorized users have read and write permissions on the object. Other users cannot access the object.
# headers[OSS_OBJECT_ACL] = oss2.OBJECT_ACL_PRIVATE
# Specify the storage class of the object. In this example, this parameter is set to BUCKET_STORAGE_CLASS_STANDARD, which indicates the Standard storage class.
# headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_STANDARD
# bucket.put_symlink(object_name, symlink, headers=headers)
# Create a symbolic link.
bucket.put_symlink(object_name, symlink)           

Get the name of the target object

To query a symbolic link, you must have read permissions on the symbolic link. The following sample code provides an example on how to query a symbolic link and the name of the object to which the symbolic link points:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before you run this 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 endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"

# Replace examplebucket with the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "examplebucket", region=region)

# Specify the name of the symbolic link.
symlink = 'examplesymlick.txt'
# Get the symbolic link and the name of the object to which it points.
bucket.get_symlink(symlink)
result = bucket.get_symlink(symlink)    
print(result.target_key)

References

  • For more information about the API operation used to create symbolic links, see PutSymlink.

  • For more information about the API operation used to retrieve symbolic links, see GetSymlink.