Presigned URLs (C++ SDK V2)

更新时间:
复制 MD 格式

A presigned URL grants third-party users temporary access to your OSS resources without exposing your access credentials. You can use the OSS C++ SDK V2 to generate presigned URLs for download, upload, metadata retrieval, and multipart upload operations.

Usage notes

  • Before you run the sample code, replace <region> with your region, such as cn-hangzhou.

  • The sample code retrieves access credentials from environment variables.

  • Use the setExpirationDuration method of PresignOptions to set the expiration of a presigned URL in seconds.

Examples

Generate a presigned URL for download

The following code generates a presigned URL to download an object:

#include <iostream>
#include <chrono>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    oss::models::PresignOptions options;
    options.setExpirationDuration(std::chrono::seconds(3600));

    auto outcome = client.presign(
        oss::models::GetObjectRequest()
            .setBucket("examplebucket")
            .setKey("exampleobject.txt"),
        &options);

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to generate a presigned URL for download"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Method: " << result.getMethod()
              << ", Expiration: " << result.getExpiration()
              << ", URL: " << result.getUrl() << std::endl;
    for (const auto& h : result.getSignedHeaders()) {
        std::cout << "  Signed Header: " << h.first << " = " << h.second << std::endl;
    }
    return 0;
}
          

Generate a presigned URL for upload

The following code generates a presigned URL to upload an object:

#include <iostream>
#include <chrono>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    oss::models::PresignOptions options;
    options.setExpirationDuration(std::chrono::seconds(3600));

    auto outcome = client.presign(
        oss::models::PutObjectRequest()
            .setBucket("examplebucket")
            .setKey("exampleobject.txt"),
        &options);

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to generate a presigned URL for upload"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Method: " << result.getMethod()
              << ", Expiration: " << result.getExpiration()
              << ", URL: " << result.getUrl() << std::endl;
    for (const auto& h : result.getSignedHeaders()) {
        std::cout << "  Signed Header: " << h.first << " = " << h.second << std::endl;
    }
    return 0;
}
          

Generate a presigned URL for HeadObject

The following code generates a presigned URL to retrieve an object's metadata:

#include <iostream>
#include <chrono>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    oss::models::PresignOptions options;
    options.setExpirationDuration(std::chrono::seconds(3600));

    auto outcome = client.presign(
        oss::models::HeadObjectRequest()
            .setBucket("examplebucket")
            .setKey("exampleobject.txt"),
        &options);

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to generate a presigned URL for a HeadObject request"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Method: " << result.getMethod()
              << ", Expiration: " << result.getExpiration()
              << ", URL: " << result.getUrl() << std::endl;
    for (const auto& h : result.getSignedHeaders()) {
        std::cout << "  Signed Header: " << h.first << " = " << h.second << std::endl;
    }
    return 0;
}
          

Generate a presigned URL for UploadPart

The following code generates a presigned URL for uploading a part, allowing a third party to upload the part directly.

#include <iostream>
#include <chrono>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    oss::models::PresignOptions options;
    options.setExpirationDuration(std::chrono::seconds(3600));

    auto outcome = client.presign(
        oss::models::UploadPartRequest()
            .setBucket("examplebucket")
            .setKey("exampleobject.txt")
            .setUploadId("your-upload-id")
            .setPartNumber(1),
        &options);

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to generate a presigned URL for an UploadPart request"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Method: " << result.getMethod()
              << ", Expiration: " << result.getExpiration()
              << ", URL: " << result.getUrl() << std::endl;
    for (const auto& h : result.getSignedHeaders()) {
        std::cout << "  Signed Header: " << h.first << " = " << h.second << std::endl;
    }
    return 0;
}
          

References

  • For the complete sample code for generating a presigned URL to download an object, see PresignGetObject.cpp.

  • For the complete sample code for generating a presigned URL to upload an object, see PresignPutObject.cpp.

  • For the complete sample code for generating a presigned URL to retrieve object metadata, see PresignHeadObject.cpp.

  • For the complete sample code for generating a presigned URL to upload a part, see PresignUploadPart.cpp.

  • For the complete sample code for generating a presigned URL to upload a part, see PresignUploadPart.cpp.

  • For a complete end-to-end scenario for generating a presigned URL for uploading an object, see PresignUpload.cpp.

  • For a complete end-to-end scenario for generating a presigned URL for downloading an object, see PresignDownload.cpp.