本文介绍如何在受版本控制的存储空间(Bucket)中管理文件(Object)的访问权限(ACL)。

设置文件访问权限

PutObjectACL默认设置Object当前版本的ACL权限。如果Object的当前版本是删除标记(Delete Marker),OSS将返回404 Not Found。请求参数中指定versionId可以设置指定Object版本的ACL权限。

以下代码用于设置文件访问权限:

#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";


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

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

    /* 设置文件访问权限 */
    SetObjectAclRequest request(BucketName, ObjectName);
    request.setAcl(CannedAccessControlList::Private);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.SetObjectAcl(request);

    /*查看本次修改权限操作的object的版本id*/
    if (outcome.isSuccess()) {
        std::cout << "versionid:" << outcome.result().VersionId() << std::endl;
    }
    else {
        /* 异常处理 */
        std::cout << "SetObjectAcl fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

设置文件访问权限的详细信息请参见PutObjectACL

获取文件访问权限

GetObjectACL默认获取Object当前版本的ACL权限。如果Object的当前版本是删除标记(Delete Marker),OSS将返回404 Not Found。请求参数中指定versionId可以获取指定Object版本的ACL权限。

以下代码用于获取文件访问权限:

#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";

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

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

    /* 获取文件访问权限 */
    GetObjectAclRequest request(BucketName, ObjectName);
    request.setVersionId("yourObjectVersionId");
    auto outcome = client.GetObjectAcl(request);

    /* 查看本次执行获取权限操作的object的版本id */
    if (outcome.isSuccess()) {
        std::cout << "GetObjectAcl success versionid:"
        << outcome.result().VersionId()
        << "Acl:" << outcome.result().Acl() << std::endl;
    }
    else {
        /* 异常处理 */
        std::cout << "GetObjectAcl fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

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

获取文件访问权限的详细信息请参考GetObjectACL