Manage symbolic links using OSS SDK for Python 2.0

Updated at:

Symbolic links in Object Storage Service (OSS) work like file shortcuts on Windows and allow you to quickly access associated objects. This topic describes how to use the OSS SDK for Python to create and obtain symbolic links. This topic describes how to use the OSS SDK for Python to create and retrieve symbolic links.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints.

  • 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 Grant custom permissions to a RAM user.

Method

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

Response parameters

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

Below is the sample code for creating 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

Below is the sample code for obtaining 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.