Manage symbolic links using OSS SDK for Python 2.0

Updated at:

This topic describes how to manage symbolic links in a versioned bucket.

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of China (Hangzhou) as an example. By default, the external network endpoint is used. If you want to access OSS through other Alibaba Cloud products in the same region, use the internal network endpoint. For more information about regions and endpoints supported by OSS, see OSS regions and endpoints.

  • To create a symbolic link, you need oss:PutObject permissions. To retrieve a symbolic link, you need oss:GetObject permissions. For detailed instructions, see Grant custom policy to RAM users.

Sample code

Create symbolic link

Note
  • A symbolic link can have multiple versions, each potentially pointing to different target objects. The version ID is automatically generated by OSS and is returned as the x-oss-version-id header in the response.

  • In a versioned bucket, a symbolic link cannot be created for a delete marker.

You can use the following code to create a symbolic link pointing to the current version of the target object.

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the script's purpose: create a symbolic link
parser = argparse.ArgumentParser(description="put symlink sample")

# Add command line argument --region, indicating the region where the bucket is located, required parameter
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add command line argument --bucket, indicating the name of the bucket, required parameter
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add command line argument --endpoint, indicating the domain names that other services can use to access OSS, optional parameter
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add command line argument --key, indicating the name of the object, required parameter
parser.add_argument('--key', help='The name of the object.', required=True)
# Add command line argument --target, indicating the destination object to which the symbolic link points, required parameter
parser.add_argument('--target', help='The destination object to which the symbolic link points.', required=True)

def main():
    # Parse the command line arguments to obtain the values entered by the user
    args = parser.parse_args()

    # Load the authentication information required to access OSS from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configuration of the SDK and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command line arguments provided by the user
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the above configuration to prepare for interaction with OSS
    client = oss.Client(cfg)

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

    # Print the status code, request ID, and other related information of the operation result to confirm the request status
    print(f'status code: {result.status_code},'
          f' request id: {result.request_id},'
          f' version id: {result.version_id},'  # Version ID (if any)
          )

# When this script is executed directly, call the main function to start processing logic
if __name__ == "__main__":
    main()  # Script entry point, control program flow starts here

Retrieve symbolic link

Note
  • To retrieve a symbolic link, you must have read permissions on the symbolic link itself.

  • The GetSymlink API retrieves the current version of the symbolic link by default. You can specify a version ID to retrieve a specific version. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found, with x-oss-delete-marker = true and the version ID: x-oss-version-id in the response header.

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

import argparse
import alibabacloud_oss_v2 as oss

# Create a command line argument parser and describe the script's purpose: retrieve symbolic link information
parser = argparse.ArgumentParser(description="get symlink sample")

# Add command line argument --region, indicating the region where the bucket is located, required parameter
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add command line argument --bucket, indicating the name of the bucket, required parameter
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add command line argument --endpoint, indicating the domain names that other services can use to access OSS, optional parameter
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add command line argument --key, indicating the name of the object, required parameter
parser.add_argument('--key', help='The name of the object.', required=True)
# Add command line argument --version_id, indicating the version ID of the object, required parameter
parser.add_argument('--version_id', help='The version id of the object.', required=True)

def main():
    # Parse the command line arguments to obtain the values entered by the user
    args = parser.parse_args()

    # Load the authentication information required to access OSS from environment variables for authentication
    credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

    # Create a configuration object using the default configuration of the SDK and set the credentials provider
    cfg = oss.config.load_default()
    cfg.credentials_provider = credentials_provider

    # Set the region property of the configuration object based on the command line arguments provided by the user
    cfg.region = args.region

    # If a custom endpoint is provided, update the endpoint property in the configuration object
    if args.endpoint is not None:
        cfg.endpoint = args.endpoint

    # Initialize the OSS client using the above configuration to prepare for interaction with OSS
    client = oss.Client(cfg)

    # Send a request to retrieve the symbolic link information
    result = client.get_symlink(oss.GetSymlinkRequest(
        bucket=args.bucket,      # Specify the bucket name
        key=args.key,            # Specify the symbolic link object name
        version_id=args.version_id  # Specify the version ID of the symbolic link object
    ))

    # Print the status code, request ID, version ID, target object, and ETag of the operation result to confirm the request status
    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},'
          )

# When this script is executed directly, call the main function to start processing logic
if __name__ == "__main__":
    main()  # Script entry point, control program flow starts here

References

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

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