本文介绍如何创建软链接并获取软链接指向的目标文件(Object)名称。

创建软链接

软链接是一种特殊的文件,它指向某个具体的文件,类似于Windows上使用的快捷方式。软链接支持自定义元信息。

以下代码用于创建软链接:

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

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";
    std::string LinkName = "yourLinkName";

    /* 初始化网络等资源 */
    InitializeSdk();

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



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

    /* 设置自定义文件元信息 */
    meta.UserMetaData()["meta"] = "meta-value";

    /* 创建软链接 */
    CreateSymlinkRequest request(BucketName, ObjectName, meta);
    request.SetSymlinkTarget(LinkObjectName);
    auto outcome = client.CreateSymlink(request);

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

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

创建软链接的详细信息请参见PutSymlink

获取软链接指向的目标文件名称

获取软链接要求您对该软链接有读权限。以下代码用于获取软链接指向的目标文件名称:

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

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string LinkName = "yourLinkName";

    /* 初始化网络等资源 */
    InitializeSdk();

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

    /* 获取object软链接 */
    GetSymlinkRequest request(BucketName, LinkName);
    auto outcome = client.GetSymlink(request);

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        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;
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

获取软链接的详细信息请参见GetSymlink