All Products
Search
Document Center

Object Storage Service:Manage symbolic links (OSS SDK for Python V2)

Last Updated:Jul 31, 2025

The symbolic link feature provides a convenient way to access frequently used objects in a bucket. After you create a symbolic link, you can use it to access an object, similar to a shortcut in Windows. This topic describes how to use the OSS SDK for Python to create and obtain symbolic links.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region (cn-hangzhou) as an example. It uses a public endpoint by default. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see OSS regions and endpoints.

  • To create a symbolic link, you must have the oss:PutObject permission. To obtain a symbolic link, you must have the oss:GetObject permission. For more information, see Grant custom permissions to a RAM user.

Method definitions

Create a symbolic link

put_symlink(request: PutSymlinkRequest, **kwargs) → PutSymlinkResult

Obtain a symbolic link

get_symlink(request: GetSymlinkRequest, **kwargs) → GetSymlinkResult

Request parameters

Parameter

Type

Description

request

PutSymlinkRequest

The request parameters to create a symbolic link. For more information, see PutSymlinkRequest

GetSymlinkRequest

The request parameters to obtain a symbolic link. For more information, see GetSymlinkRequest

Return values

Type

Description

PutSymlinkResult

The return value. For more information, see PutSymlinkResult

GetSymlinkResult

The return value. For more information, see GetSymlinkResult

For the complete definition of the method to create a symbolic link, see put_symlink.

For the complete definition of the method to obtain a symbolic link, see get_symlink.

Sample code

Create a symbolic link

You can use the following code to create a symbolic link.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command-line argument parser
parser = argparse.ArgumentParser(description="put symlink sample")
# Add command-line arguments
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)
parser.add_argument('--target', help='The destination object to which the symbolic link points.', required=True)

def main():
    # Parse command-line arguments
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK
    cfg = oss.config.load_default()
    # Set the credential provider
    cfg.credentials_provider = credentials_provider
    # Set the region
    cfg.region = args.region
    # If an endpoint is provided, set the endpoint
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client
    client = oss.Client(cfg)

    # Create a request to create a symbolic link
    result = client.put_symlink(oss.PutSymlinkRequest(
        bucket=args.bucket,  # Specify the bucket name
        key=args.key,        # Specify the object name
        target=args.target,  # Specify the target object
    ))

    # Print the result
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
    )

# If the script is run directly, call the main function
if __name__ == "__main__":
    main()

Obtain a symbolic link

You can use the following code to obtain a symbolic link and the name of its target object.

import argparse
import alibabacloud_oss_v2 as oss

# Create an ArgumentParser object to parse command-line arguments
parser = argparse.ArgumentParser(description="get symlink sample")
# Add command-line arguments
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)

def main():
    # Parse command-line arguments
    args = parser.parse_args()

    # Load credentials from environment variables
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Use the default configurations of the SDK
    cfg = oss.config.load_default()
    # Set the credential provider
    cfg.credentials_provider = credentials_provider
    # Set the region
    cfg.region = args.region
    # If an endpoint is provided, set the endpoint
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Create an OSS client
    client = oss.Client(cfg)

    # Send a request to obtain the symbolic link
    result = client.get_symlink(oss.GetSymlinkRequest(
        bucket=args.bucket,
        key=args.key,
    ))

    # Print the result
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'
          f' target: {result.target},'
          f' etag: {result.etag},'
    )

# If the script is run directly, call the main function
if __name__ == "__main__":
    main()

References

  • For the complete sample code for creating a symbolic link, see put_symlink.py.

  • For the complete sample code for obtaining a symbolic link, see get_symlink.py.