All Products
Search
Document Center

Object Storage Service:Manage symbolic links

Last Updated:Sep 11, 2023

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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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.

  • 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 Common examples of RAM policies.

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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. For more information about the endpoints of other regions, see Regions and endpoints. 
# Specify the name of the bucket. Example: examplebucket. For more information about the naming conventions for buckets, see Bucket naming conventions. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the name of the object to which you want the symbolic link to point. Example: exampleobject.txt. // For more information about the naming conventions for objects, see Object naming conventions. 
object_name = 'exampleobject.txt'
# Specify the name of the symbolic link. Example: examplesymlick.txt. 
symlink = 'examplesymlick.txt';

# headers = dict()
# Specify whether to overwrite the object that has the same name as the symbolic link. In this example, x-oss-forbid-overwrite is set to true. This indicates that the object that has the same name cannot be overwritten. 
# 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 ACL of the object is private. Only the owner of the object and authorized users have read and write permissions on the object. 
# headers[OSS_OBJECT_ACL] = oss2.OBJECT_ACL_PRIVATE
# Specify the storage class of the object. In this example, the storage class is set to Standard. 
# headers['x-oss-storage-class'] = oss2.BUCKET_STORAGE_CLASS_STANDARD
# bucket.put_symlink(object_name, symlink, headers=headers)
# Create the symbolic link. 
bucket.put_symlink(object_name, symlink)           

Query the name of the object to which a symbolic link points

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 the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Specify the name of the symbolic link. 
symlink = 'examplesymlick.txt';
# Query the symbolic link and the name of the object to which the symbolic link points. 
bucket.get_symlink(symlink)
result = bucket.get_symlink(symlink)        
print(result.target_key)          

References

  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.

  • For more information about the API operation that you can call to query a symbolic link, see GetSymlink.