This topic describes how to manage symbolic links in a versioning-enabled bucket.

Create a symbolic link

You can create a symbol link that directs to the current version of the target object.
Note In a bucket with versioning enabled or suspended, you cannot create a symbol link for a delete marker.

A symbol link can has multiple versions that direct to different target objects. The version ID of the symbol link is automatically generated and is returned as the x-oss-version-id field in the response header.

The following code provides an example on 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.*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";
    std::string LinkName = "yourLinkName";

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

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

    /*Set 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);

    /*View the version ID of the created 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;
        ShutdownSdk();
        return -1;
    }

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

For more information about creating a symbol link, see PutSymlink.

Obtain symbolic links

GetSymlink obtains the current version of the target symbol link by default. You can specify the versionId in the request to obtain the specified version of a symbol link. If the current version of the target symbol link is a delete marker, OSS returns the 404 Not Found error and includes x-oss-delete-marker = true and x-oss-version-id in the response header.
Note You must have the read permission on a symbol link to obtain it.

The following code provides an example on how to obtain a symbolic link:

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

int main(void)
 {
   /* Initialize the OSS account information. */
     std::string AccessKeyId = "yourAccessKeyId";
     std::string AccessKeySecret = "yourAccessKeySecret";
     std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string LinkName = "yourLinkName";

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

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

    /* Obtain the symbolic link with the specified version ID. */
    GetSymlinkRequest request(BucketName, LinkName);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.GetSymlink(request);

    /* View the returned version ID of the symbolic link. */
    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;
        ShutdownSdk();
        return -1;
    }

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

For more information about obtaining a symbol link, see GetSymlink.