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 query 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)
{
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a 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";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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 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 network resources. */
    InitializeSdk();

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



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

    /* Configure 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 network resources. */
    ShutdownSdk();
    return 0;
}

Query a symbolic link

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

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

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

int main(void)
{
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a 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";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint 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 network resources. */
    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 network resources. */
    ShutdownSdk();
    return 0;
}

References

  • For more information about the API operation that you can call to create a symbolic link, see PutSymlink.
  • For more information about the API operation that you can call to query a symbolic link, see GetSymlink.