签名URL进行临时授权

  • 临时授权上传文件,User1生成授权上传的URL,User2可以使用经过授权的URL向User1的指定的文件中存储内容。

User1生成上传文件的URL,代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化User1账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:

        //User1指定需要上传到的存储桶以及需要上传的文件(yourPutobjectUrlName)
        std::string BucketName = "yourBucketName";
        std::string PutobjectUrlName = "yourPutobjectUrlName" ;

        //初始化资源
        InitializeSdk();
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

        //设置签名URL有效时间,以秒为单位
        std::time_t t = std::time(nullptr) + 1200;
        //生成上传文件URL
        auto genOutcome = client.GeneratePresignedUrl(BucketName, PutobjectUrlName, t, Http::Put);

        //处理结果
        if (genOutcome.isSuccess()) {
            std::cout << "GeneratePresignedUrl success, Gen url:"<<std::endl;
            std::cout << genOutcome.result().c_str() << std::endl;
        }
        //异常处理
        else {
            std::cout << "GeneratePresignedUrl fail" << std::endl;
            std::cout << "error code:" << genOutcome.error().Code() << std::endl;
            std::cout << "error message:" << genOutcome.error().Message() << std::endl;
            std::cout << "requestId:" << genOutcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }

        //释放网络等资源
        ShutdownSdk();

User2使用User1生成的URL向指定文件上传内容,代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化User2账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:

        //User2使用自己的账户信息以及授权的URL上传
        std::string AccessKeyId = "UserAccessKeyId";
        std::string AccessKeySecret = "UserAccessKeyId";
        std::string Endpoint = "UserEndpoint";
        std::string str_url = "PresignedUrl";

        //初始化网络等资源
        InitializeSdk();
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
        *content << "Welcome to Inspur Cloud Object Storage Service!";

        //使用签名URL上传文件
        auto outcome = client.PutObjectByUrl(str_url, content);

        //处理结果
        if (!outcome.isSuccess()) {
            std::cout << "PutObjectByUrl fail" << std::endl;
            std::cout << "error code:" << outcome.error().Code() << std::endl;
            std::cout << "error message:" << outcome.error().Message() << std::endl;
            std::cout << "requestId:" << outcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }
        else{
            std::cout << "PutObjectByUrl Success!" << std::endl;
        }

        /* 释放网络等资源 */
        ShutdownSdk();
  • 临时授权下载文件,User1生成授权下载的URL,User2可以使用经过授权的URL直接在网页下载文件。

User1生成授权下载的URL。代码中需要引用"inspurcloud/oss/OssClient.h"类库,使用InspurCloud::OSS命名空间,并初始化User1账号信息AccessKeyId、AccessKeySecret、Endpoint,示例代码:

        //指定文件所在的存储桶以及文件名称(Object)
        std::string BucketName = "yourBucketName";
        //需要下载的文件名
        std::string GetobjectUrlName = "yourGetobjectUrlName";

        //初始化资源
        InitializeSdk();
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
        //设置签名URL有效时间,以秒为单位
        std::time_t t = std::time(nullptr) + 600;

        //生成下载文件URL
        auto genOutcome = client.GeneratePresignedUrl(BucketName, GetobjectUrlName, t, Http::Get);

        //处理结果
        if (genOutcome.isSuccess()) {
            std::cout << "GeneratePresignedUrl success, Gen url:" << std::endl << genOutcome.result().c_str() << std::endl;
        }
        //异常处理 
        else {
            std::cout << "GeneratePresignedUrl fail" << std::endl;
            std::cout << "error code:" << genOutcome.error().Code() << std::endl;
            std::cout << "error message:" << genOutcome.error().Message() << std::endl;
            std::cout << "requestId:" << genOutcome.error().RequestId() << std::endl;
            ShutdownSdk();
            return -1;
        }

        //释放资源
        ShutdownSdk();