AWS SDK CPP

使用 AWS 官方 SDK aws-sdk-cpp

/*
   Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This file is licensed under the Apache License, Version 2.0 (the "License").
   You may not use this file except in compliance with the License. A copy of
   the License is located at

    http://aws.amazon.com/apache2.0/

   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied. See the License for the
   specific language governing permissions and limitations under the License.
*/
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
#include <aws/core/auth/AWSCredentialsProviderChain.h>
#include <aws/core/utils/ratelimiter/DefaultRateLimiter.h>
#include <aws/core/utils/threading/Executor.h>

using namespace Aws::Auth;
using namespace Aws::Http;
using namespace Aws::S3;

/**
 * Create an Amazon S3 bucket.
 */
int main(int argc, char** argv)
{
    if (argc < 2)
    {
        std::cout << "create_bucket - create an S3 bucket" << std::endl
            << "\nUsage:" << std::endl
            << "  create_bucket <bucket>" << std::endl
            << "\nWhere:" << std::endl
            << "  bucket - the bucket to create" << std::endl
            << "\nExample:" << std::endl
            << "  create_bucket testbucket\n" << std::endl << std::endl;
        exit(1);
    }

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        static std::shared_ptr<S3Client> Client;
        static const char* ALLOCATION_TAG = "S3Client";

        Aws::String endpoint = "cn-east-1-s3.qiniu.com";
        static const char* const  region = "cn-east-1";

        // Config a client
        Aws::Client::ClientConfiguration config;
        config.endpointOverride = endpoint;
        config.region = region;
        config.scheme = Scheme::HTTPS;
        config.connectTimeoutMs = 30000;
        config.requestTimeoutMs = 30000;
        config.executor = Aws::MakeShared<Aws::Utils::Threading::PooledThreadExecutor>(ALLOCATION_TAG, 4);

        // // to use a proxy, uncomment the next lines.
        // config.proxyScheme = Scheme::HTTP;
        // config.proxyHost = "127.0.0.1";
        // config.proxyPort = 8888;

        const AWSCredentials& credentials = AWSCredentials("<your-access-key-id>", "<your-access-key-secret>", "");

        Client = Aws::MakeShared<S3Client>(ALLOCATION_TAG,
                    credentials,
                    config,
                    false /*signPayloads*/,
                    false /*useVirtualAddressing*/);

        // Create a bucket
        const Aws::String bucketName = argv[1];

        Model::CreateBucketRequest request;
        request.SetBucket(bucketName);

        std::cout << "Creating S3 bucket: " << bucketName << std::endl;

        auto outcome = Client->CreateBucket(request);

        if (outcome.IsSuccess())
        {
            std::cout << "Done!" << std::endl;
        }
        else
        {
            std::cout << "Create bucket error: "
                << outcome.GetError().GetExceptionName() << std::endl
                << outcome.GetError().GetMessage() << std::endl;
        }
    }
    Aws::ShutdownAPI(options);
}

results matching ""

    No results matching ""