gpt4 book ai didi

c++ - 使用 C++ SDK 将文件上传到 AWS

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:05 28 4
gpt4 key购买 nike

每当我运行我的程序时,我都会收到错误“无法连接到端点”。但我知道我可以连接到该存储桶,因为我可以使用相同的配置和 s3Client 成功下载该存储桶中的文件。它卡在 PutObject() 行上。

这是代码。

const String KEY = "test2.txt";
const String BUCKET = "savefiles2017";


const String fileName = "test2.txt";

Client::ClientConfiguration config;
config.region = Region::US_WEST_2;
config.scheme = Http::Scheme::HTTPS;

S3Client s3Client(Auth::AWSCredentials("XXXXXX", "YYYYYY"), config);

//putting something into s3
PutObjectRequest putObjectRequest;
putObjectRequest.WithBucket(BUCKET).WithKey(KEY);

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in);

putObjectRequest.SetBody(requestStream);

auto putObjectOutcome = s3Client.PutObject(putObjectRequest);

if (putObjectOutcome.IsSuccess())
{
cout << "Put object succeeded" << endl;
}
else
{
cout << "Error while putting Object " << putObjectOutcome.GetError().GetExceptionName() <<
" " << putObjectOutcome.GetError().GetMessage() << endl;
}

这些也是我正在使用的包含

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>


using namespace Aws;
using namespace Aws::S3;
using namespace Aws::S3::Model;

如有任何帮助,我们将不胜感激。

最佳答案

我想出了解决办法。我不得不换行

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in);

auto requestStream = MakeShared<FStream>("PutObjectInputStream", fileName.c_str(), ios_base::in | ios_base::binary);

所以我只需要添加 ios_base::binary 的额外标志

关于c++ - 使用 C++ SDK 将文件上传到 AWS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com