PutObject错误:使用aws-cpp-sdk将文件上载到AWS S3

时间:2016-09-02 06:30:35

标签: c++ amazon-web-services amazon-s3 aws-sdk

我有一个c ++文件,用于将avi文件上传到AWS S3存储。 该计划如下。

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/core/utils/HashingUtils.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <iostream>
#include <fstream>

using namespace Aws::S3::Model;
using namespace std;
using namespace Aws::Utils;

static const char* KEY = "test.avi";
static const char* BUCKET = "testnmn";

int main()
{
    Aws::SDKOptions options;
    Aws::InitAPI(options);   

    const Aws::String bucket_name = BUCKET;
    const Aws::String key_name = KEY;
    const Aws::String dir_name = "/home/Softwares/Projects/S3upload/build";

    std::cout << "Uploading " << key_name << " to S3 bucket: " <<
        bucket_name << std::endl;

    Aws::S3::S3Client s3_client;

    Aws::S3::Model::PutObjectRequest object_request;
    object_request.WithBucket(bucket_name).WithKey(key_name);

    auto input_data = Aws::MakeShared<Aws::FStream>(key_name.c_str(), dir_name.c_str(), std::ios_base::in);

    object_request.SetBody(input_data);

    auto put_object_outcome = s3_client.PutObject(object_request);

    if(put_object_outcome.IsSuccess()) {
        std::cout << "Done!" << std::endl;
    }
    else {
         std::cout << "PutObject error: " <<
             put_object_outcome.GetError().GetExceptionName() << " " <<
             put_object_outcome.GetError().GetMessage() << std::endl;
    }

    Aws::ShutdownAPI(options);

    return 0;  
}

当我运行exe文件时,我的错误为

Uploading test.avi to S3 bucket: testnmn
PutObject error: PermanentRedirect Unable to parse ExceptionName: PermanentRedirect Message: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

该错误可能有什么问题?

由于

1 个答案:

答案 0 :(得分:1)

您尝试访问的Bucket看起来像是在不同的区域创建的(默认情况下,我认为是us-east-1)&amp;因此端点不匹配。

您可以使用ClientConfiguration设置区域:

Aws::Client::ClientConfiguration myConf;
myConf.region = Aws::Region::US_WEST_2;
Aws::S3::S3Client s3_client(myConf);