All Products
Search
Document Center

Object Storage Service:Manage symbolic links using OSS SDK for C++

Last Updated:Mar 20, 2026

A symbolic link is a pointer to a target object in OSS. Access the target object through the symlink name without knowing its actual path.

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

For details on granting these permissions, see Attach a custom policy to a RAM user.

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region (https://oss-cn-hangzhou.aliyuncs.com). To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.

  • The examples create an OSSClient instance using a standard OSS endpoint. To create an OSSClient using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.

  • If the target object is deleted, the symbolic link remains but can no longer be used to access it.

Create a symbolic link

The following code:

  1. Reads access credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

  2. Builds a CreateSymlinkRequest with the bucket name, target object path (ObjectName), and optional object metadata.

  3. Sets the symbolic link path with request.SetSymlinkTarget().

  4. Calls client.CreateSymlink(request) to create the symbolic link.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Set yourEndpoint to the endpoint of the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region, use cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the target object. Example: exampledir/exampleobject.txt.
       Do not include the bucket name in the path. */
    std::string ObjectName = "exampledir/exampleobject.txt";
    /* Specify the full path of the symbolic link. Example: shortcut/myobject.txt. */
    std::string LinkName = "shortcut/myobject.txt";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables.
       Make sure that OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are set before running this code. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Set object metadata. */
    auto meta = ObjectMetaData();
    meta.setContentType("text/plain");

    /* Set custom object metadata. */
    meta.UserMetaData()["meta"] = "meta-value";

    /* Create the symbolic link. ObjectName is the target object; LinkName is the symlink path. */
    CreateSymlinkRequest request(BucketName, ObjectName, meta);
    request.SetSymlinkTarget(LinkName);
    auto outcome = client.CreateSymlink(request);

    if (!outcome.isSuccess()) {
        /* Handle the error. */
        std::cout << "CreateSymlink fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Get a symbolic link

Read permissions on the symbolic link are required to retrieve it.

The following code:

  1. Reads access credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.

  2. Builds a GetSymlinkRequest with the bucket name and symbolic link path.

  3. Calls client.GetSymlink(request) and prints the target object path returned in SymlinkTarget().

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Set yourEndpoint to the endpoint of the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region, use https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located.
       For example, if the bucket is in the China (Hangzhou) region, use cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the symbolic link. Example: shortcut/myobject.txt. */
    std::string LinkName = "shortcut/myobject.txt";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables.
       Make sure that OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are set before running this code. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Retrieve the symbolic link. The result contains the target object path. */
    GetSymlinkRequest request(BucketName, LinkName);
    auto outcome = client.GetSymlink(request);

    if (!outcome.isSuccess()) {
        /* Handle the error. */
        std::cout << "GetSymlink fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }
    else {
        std::cout << " GetSymlink success Symlink name:" << outcome.result().SymlinkTarget() << std::endl;
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

References

  • For the API operation to create symbolic links, see PutSymlink.

  • For the API operation to retrieve symbolic links, see GetSymlink.