All Products
Search
Document Center

Object Storage Service:Manage symbolic links

Last Updated:Oct 17, 2023

A symbolic link provides quick access to an object in a bucket and facilitates access to a frequently used object in Object Storage Service (OSS). A symbolic link works similarly to a shortcut in Windows. 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 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 Attach a custom policy to a RAM user.

Create a symbolic link

A symbolic link can have multiple versions. When you create a symbolic link in a versioning-enabled bucket, the version ID of the symbolic link is automatically generated by OSS and is returned as the x-oss-version-id header in the response. You can create a symbolic link to which the current version of an object points.

Note

In a versioning-enabled bucket, a symbolic link cannot be created for a delete marker.

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())
# 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 the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

objectName = 'yourTargetObjectName'
symlink = 'yourSymlink'

# Upload a symbolic link. 
result = bucket.put_symlink(objectName, symlink)
# Show the version ID of the uploaded symbolic link. 
print('symlink versionid:', result.versionid)

Query a symbolic link

By default, the GetSymlink operation obtains the current version of a symbolic link. You can specify the version ID in the request to query the specified version of a symbolic link. If the current version of the symbolic link is a delete marker, OSS returns 404 Not Found and includes x-oss-delete-marker = true and x-oss-version-id in the response.

Note

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:

# -*- 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())
# 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 the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

symlink = 'yourSymlink'

# Query the symbolic link of the specified version. 
params = dict()
params['versionId'] = 'yourSymlinkVersionId'
result = bucket.get_symlink(symlink, params=params)
# Show the version ID of the returned symbolic link. 
print('get symlink versionid:', result.versionid)

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.