Manage symbolic links (C++ SDK)

Updated at:

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.

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.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from 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 Grant a custom policy.

Create a symbolic link

A symbolic link can have multiple versions, and each version can point to a different target object. OSS automatically generates a version ID and returns it in the `x-oss-version-id` response header. You can create a symbolic link that points to the current version of the target object.

Note

In a versioning-enabled bucket, you cannot create a symbolic link 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

The GetSymlink operation retrieves the current version of a symbolic link by default. You can specify a version ID to retrieve a specific version. If the current version of the symbolic link is a delete marker, OSS returns a 404 Not Found error. The response header includes `x-oss-delete-marker = true` and the version ID `x-oss-version-id`.

Note

To retrieve a symbolic link, you must have read permissions for it.

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.