Manage symbolic links using OSS SDK for Python 2.0

更新时间:
复制 MD 格式

A symbolic link points to a target object in a bucket without duplicating it — similar to a shortcut. In a versioning-enabled bucket, each symbolic link can have multiple versions, and different versions can point to different target objects. This topic shows how to create and retrieve symbolic links using the OSS Python SDK V2.

Prerequisites

Before you begin, ensure that you have:

  • A versioning-enabled OSS bucket

  • The oss:PutObject permission to create a symbolic link, or the oss:GetObject permission to retrieve one. For details, see Grant custom permissions to a RAM user

Usage notes

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) with the public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For a full list of regions and endpoints, see OSS regions and endpoints.

  • You cannot create a symbolic link for a delete marker in a versioning-enabled bucket.

Create a symbolic link

The following code creates a symbolic link named examplesymlink that points to exampleobject.txt in examplebucket. OSS automatically assigns a version ID to the new symbolic link and returns it in the x-oss-version-id response header.

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 symbolic link object.', required=True)
parser.add_argument('--target', help='The target 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,       # Name of the symbolic link object
        target=args.target, # Target object the symbolic link points to
    ))

    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.

Retrieve a symbolic link

The following code retrieves the symbolic link examplesymlink from examplebucket and returns the target object name, ETag, and version ID.

By default, GetSymlink retrieves the current version of the symbolic link. Pass version_id to retrieve a specific version. If the current version is a delete marker, OSS returns a 404 Not Found error with x-oss-delete-marker = true and the version ID in the x-oss-version-id response header.

To retrieve a symbolic link, you must have read permissions on it.
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 symbolic link object.', required=True)
parser.add_argument('--version_id', help='The version ID of the symbolic link 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,
        version_id=args.version_id, # Omit to retrieve the current version
    ))

    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.