A symbolic link is a special object that points to an object. It is similar to a shortcut used in Windows. This topic describes how to create a symbolic link and obtain the name of the object to which the symbolic link points.

Note
  • To delete symbolic links, see Delete objects.
  • After you delete the object to which a symbolic link points, the symbolic link still exists, but the object cannot be accessed by using the symbolic link.

Create a symbolic link

The following code provides an example on how to create symbolic links. When you create a symbolic link, you can specify user metadata of the symbolic link.

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

int main(void)
{
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. */
    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 resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);



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

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

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

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

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

For more information about how to create symbolic links, see PutSymlink.

Query the name of the object to which a symbolic link points

To query the name of the object to which a symbolic link points, you must have the permission to read the symbolic link.

The following code provides an example on how to obtain the name of the object to which the symbolic link points:

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

int main(void)
{
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Specify the name of the bucket. 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 resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* Obtain the name of the object to which the symbolic link points. */
    GetSymlinkRequest request(BucketName, LinkName);
    auto outcome = client.GetSymlink(request);

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

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

For more information about how to obtain symbolic links, see GetSymlink.