All Products
Search
Document Center

Object Storage Service:Manage symbolic links (C++ SDK)

Last Updated:Nov 29, 2025

The symbolic link feature provides a convenient way to access frequently used objects in a bucket. You can use a symbolic link as a shortcut to access the target object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

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

Create a symbolic link

A symbolic link can have multiple versions. When you create a symbolic link in a versioning-enabled bucket, the version ID of the symbolic link is automatically generated by OSS and is returned as the x-oss-version-id header in the response. You can create a symbolic link to which the current version of an object points.

Note

In a versioning-enabled bucket, a symbolic link cannot be created for a delete marker.

The following code shows how to create a symbolic link:

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to 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, set the Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";
    /* Specify the name of the symbolic link. */
    std::string LinkName = "yourLinkName";

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

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

    /* Set the HTTP header. */
    auto meta = ObjectMetaData();
    meta.setContentType("text/plain");

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

    /* Create a symbolic link. */
    CreateSymlinkRequest request(BucketName, ObjectName, meta);
    request.SetSymlinkTarget(LinkName);
    auto outcome = client.CreateSymlink(request);

    /* View the version ID of the uploaded symbolic link. */
    if (outcome.isSuccess()) {
        std::cout << "versionid:" << outcome.result().VersionId() << std::endl;
    }
    else {
        /* Handle exceptions. */
        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

By default, the GetSymlink operation obtains the current version of a symbolic link. You can specify the version ID in the request to query the specified version of a symbolic link. If the current version of the symbolic link is a delete marker, OSS returns 404 Not Found and includes x-oss-delete-marker = true and x-oss-version-id in the response.

Note

To query a symbolic link, you must have read permissions on the symbolic link.

The following code shows how to retrieve a symbolic link:

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

int main(void)
 {
    /* Initialize the OSS account information. */
            
    /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to 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, set the Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the name of the symbolic link. */
    std::string LinkName = "yourLinkName";

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

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

    /* Get a specific version of the symbolic link file. */
    GetSymlinkRequest request(BucketName, LinkName);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.GetSymlink(request);

    /* View the version ID of the returned symbolic link file. */
    if (outcome.isSuccess()) {
        std::cout << "versionid:" << outcome.result().VersionId()
        << "Symlink name:" << outcome.result().SymlinkTarget() << std::endl;
    }
    else {
        /* Handle exceptions. */
        std::cout << "GetSymlink fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
    }

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

References

  • 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.