Manage symbolic links (Swift SDK)

Updated at:

A symbolic link points to an object in a bucket, similar to a shortcut in Windows. This topic describes how to create a symbolic link and retrieve its target object name using the OSS Swift SDK.

Prerequisites

Before you begin, make sure you have:

  • The oss:PutObject permission to create a symbolic link

  • The oss:GetObject permission to retrieve a symbolic link

  • (Optional) A custom endpoint if you need to access OSS with a specific domain

For permission setup, see Grant custom permissions to a RAM user.

Usage notes

The sample code uses the China (Hangzhou) region (cn-hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For endpoint details, see Regions and endpoints.

Create a symbolic link

Sample code

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // Required. Specify the region where the bucket is located. Example: cn-hangzhou.
            let region = "cn-hangzhou"
            // Required. Specify the bucket name.
            let bucket = "yourBucketName"
            // Required. Specify the name of the symbolic link to create. Example: symlink.txt.
            let key = "yourSymlinkName"
            // Required. Specify the path of the target object. Example: /path/to/target.txt.
            let symlinkTarget = "yourTargetObjectPath"
            // Optional. Specify a custom endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil

            // Initialize the credential provider using the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure the OSS client.
            let config = Configuration.default()
                .withRegion(region)
                .withCredentialsProvider(credentialsProvider)

            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            let client = Client(config)

            // Create the symbolic link.
            let result = try await client.putSymlink(
                PutSymlinkRequest(
                    bucket: bucket,
                    key: key,
                    symlinkTarget: symlinkTarget
                )
            )
            print("result:\n\(result)")

        } catch {
            print("error: \(error)")
        }
    }
}

Get a symbolic link

Sample code

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {
        do {
            // Required. Specify the region where the bucket is located. Example: cn-hangzhou.
            let region = "cn-hangzhou"
            // Required. Specify the bucket name.
            let bucket = "yourBucketName"
            // Required. Specify the name of the symbolic link. Example: document.txt.
            let key = "yourObjectName"
            // Optional. Specify a custom endpoint. Example: https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil

            // Initialize the credential provider using the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
            let credentialsProvider = EnvironmentCredentialsProvider()

            // Configure the OSS client.
            let config = Configuration.default()
                .withRegion(region)
                .withCredentialsProvider(credentialsProvider)

            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            let client = Client(config)

            // Retrieve the symbolic link.
            let result = try await client.getSymlink(
                GetSymlinkRequest(
                    bucket: bucket,
                    key: key
                )
            )
            print("result:\n\(result)")

        } catch {
            print("error: \(error)")
        }
    }
}

What's next