All Products
Search
Document Center

Object Storage Service:Manage symbolic links using OSS SDK for Python 2.0

Last Updated:Mar 20, 2026

Symbolic links let you access frequently used objects in a bucket through shortcuts, similar to shortcuts in Windows. This topic describes how to create and get symbolic links using the OSS SDK for Python V2.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket in your target region

  • The oss:PutObject permission to create symbolic links

  • The oss:GetObject permission to get symbolic links

For details on granting permissions, see Grant custom permissions to a RAM user.

Usage notes

Note: The sample code uses the China (Hangzhou) region (cn-hangzhou) with a public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see OSS regions and endpoints.

Method definitions

put_symlink(request: PutSymlinkRequest, **kwargs) → PutSymlinkResult
get_symlink(request: GetSymlinkRequest, **kwargs) → GetSymlinkResult

For the complete method definitions, see put_symlink and get_symlink.

Request parameters

PutSymlinkRequest — see PutSymlinkRequest

ParameterDescription
bucketThe name of the bucket.
keyThe name of the symbolic link object.
targetThe name of the target object the symbolic link points to.

GetSymlinkRequest — see GetSymlinkRequest

ParameterDescription
bucketThe name of the bucket.
keyThe name of the symbolic link object.

Return values

TypeDescription
PutSymlinkResultThe return value for creating a symbolic link. See PutSymlinkResult.
GetSymlinkResultThe return value for getting a symbolic link. See GetSymlinkResult.

Create a symbolic link

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="put symlink sample")
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():
    args = parser.parse_args()

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

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    result = client.put_symlink(oss.PutSymlinkRequest(
        bucket=args.bucket,
        key=args.key,
        target=args.target,
    ))

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

if __name__ == "__main__":
    main()

For the complete sample, see put_symlink.py.

Get a symbolic link

import argparse
import alibabacloud_oss_v2 as oss

parser = argparse.ArgumentParser(description="get symlink sample")
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():
    args = parser.parse_args()

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

    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider
    cfg.region = args.region
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    client = oss.Client(cfg)

    result = client.get_symlink(oss.GetSymlinkRequest(
        bucket=args.bucket,
        key=args.key,
    ))

    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 __name__ == "__main__":
    main()

For the complete sample, see get_symlink.py.