【问题标题】:PutObject error: in uploading a file to AWS S3 using aws-cpp-sdkPutObject 错误:使用 aws-cpp-sdk 将文件上传到 AWS S3
【发布时间】:2016-09-02 06:30:35
【问题描述】:

我有一个 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.

这个错误可能有什么问题?

谢谢

【问题讨论】:

  • 这个问题你解决了吗?如果你已经完成了,你能分享一下如何解决吗?

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


【解决方案1】:

您尝试访问的存储桶似乎是在不同的区域中创建的(而不是默认区域,我认为是 us-east-1),因此端点不匹配。

您可以使用 ClientConfiguration 设置区域:

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

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 2020-05-07
    • 2012-11-10
    • 2016-10-08
    • 1970-01-01
    • 2020-08-11
    • 2019-12-06
    • 2021-03-09
    • 2014-05-27
    相关资源
    最近更新 更多