All Products
Search
Document Center

Object Storage Service:Manage symbolic links (Swift SDK)

Last Updated:Nov 29, 2025

The symbolic link feature provides easy access to frequently used files in a bucket. After you create a symbolic link, you can use it to open a file, which is similar to using a shortcut in Windows. This topic describes how to use the OSS Swift SDK to create and retrieve symbolic links.

Precautions

  • The sample code in this topic uses the China (Hangzhou) region ID cn-hangzhou as an example. The code uses a public endpoint by default. To access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see Regions and endpoints.

  • To create a symbolic link, you must have the oss:PutObject permission. To retrieve a symbolic link, you must have the oss:GetObject permission. For more information, see Grant custom permissions to a RAM user.

Sample code

Create a symbolic link

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

import AlibabaCloudOSS
import Foundation

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

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

            // Configure the parameters for the OSS client.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credentials.
                
            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }

            // Create an OSS client instance.
            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 the error message and continue.
            print("error: \(error)")
        }
    }
}

Get a symbolic link

You can use the following code to retrieve a symbolic link and the name of its target object.

import AlibabaCloudOSS
import Foundation

@main
struct Main {
    static func main() async {

        do {
            // Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
            let region = "cn-hangzhou" // Replace with the actual region ID.
            // Specify the bucket name.
            let bucket = "yourBucketName" // Replace with the actual bucket name.
            // Optional. Specify the domain name to access OSS. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
            let endpoint: String? = nil
            // Specify the name of the symbolic link. Example: document.txt.
            let key = "yourObjectName" // Replace with the actual name of the symbolic link.

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

            // Configure the parameters for the OSS client.
            let config = Configuration.default()
                .withRegion(region)        // Set the region where the bucket is located.
                .withCredentialsProvider(credentialsProvider)  // Set the access credentials.
                
            // Set the endpoint.
            if let endpoint = endpoint {
                config.withEndpoint(endpoint)
            }
            
            // Create an OSS client instance.
            let client = Client(config)

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

        } catch {
            // Print the error message and continue.
            print("error: \(error)")
        }
    }
}

References

  • For the complete sample code for symbolic links, see the GitHub examples put_symlink and get_symlink.

  • For more information about operations on symbolic links, see Symbolic links.

  • For more information about the API operation to create a symbolic link, see PutSymlink.

  • For more information about the API operation to retrieve a symbolic link, see GetSymlink.