amazon-s3 - 使用 aws-sdk-cpp 构建示例应用程序以从 s3 存储桶下载对象
问题描述
我是新手,最近开始学习 AWS。
我已经下载并编译了 aws-sdk-cpp。我正在尝试使用 aws sdk 文档中提供的 get_object.cpp 示例构建示例应用程序。
但是我的示例给出了 curl 代码 56。我检查了 tcpdump,发现它无法找到正确的主机。我没有使用任何代理。我试过 curl "https://api.dev.abc.com:8080/bucket_name/object_name" 这工作正常。但是我的示例应用程序出现 curl 错误 56。
我附上示例应用程序供您参考。能否请您看一下代码并帮助我解决问题。
示例代码
#include <iostream>
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentials.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/GetObjectRequest.h>
#include <fstream>
#include <awsdoc/s3/s3_examples.h>
bool AwsDoc::S3::GetObject(const Aws::String& objectKey,
const Aws::String& fromBucket, const Aws::String& region)
{
Aws::Client::ClientConfiguration config;
//curl https://api.dev.abc.com:8080/bucket_name/object_name
if (!region.empty())
{
config.region = region;
config.proxyHost = "api.dev.abc.com";
config.proxyPort = 8080;
config.proxyScheme = Aws::Http::Scheme::HTTPS;
}
Aws::Auth::AWSCredentials cred("xxxxxxx", "yyyyyy");
Aws::S3::S3Client s3_client(cred, config);
Aws::S3::Model::GetObjectRequest object_request;
object_request.SetBucket(fromBucket);
object_request.SetKey(objectKey);
Aws::S3::Model::GetObjectOutcome get_object_outcome =
s3_client.GetObject(object_request);
if (get_object_outcome.IsSuccess())
{
auto& retrieved_file = get_object_outcome.GetResultWithOwnership().
GetBody();
// Print a beginning portion of the text file.
std::cout << "Beginning of file contents:\n";
char file_data[255] = { 0 };
retrieved_file.getline(file_data, 254);
std::cout << file_data << std::endl;
return true;
}
else
{
auto err = get_object_outcome.GetError();
std::cout << "Error: GetObject: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
return false;
}
}
int main()
{
Aws::SDKOptions options;
Aws::InitAPI(options);
{
//TODO: Change bucket_name to the name of a bucket in your account.
const Aws::String bucket_name = "bucket_name";
//TODO: The bucket "DOC-EXAMPLE-BUCKET" must have been created and previously loaded with "my-file.txt".
//See create_bucket.cpp and put_object.cpp to create a bucket and load an object into that bucket.
const Aws::String object_name = "object_name";
//TODO: Set to the AWS Region in which the bucket was created.
const Aws::String region = "India_South";
if (!AwsDoc::S3::GetObject(object_name, bucket_name, region))
{
return 1;
}
}
Aws::ShutdownAPI(options);
return 0;
}
解决方案
推荐阅读
- python-3.x - 功能外观,需要生成一个随机选择 4 个选项的列表
- angular - 运行业力单元测试用例“引发未知 [对象对象]”时出错
- python - 我的时间序列图如何从“00:00”开始并在“23:00”结束?两边没有空格
- asp.net-mvc - 在 Bamboo 中设置 NSIS 构建
- java - 从另一个类调用时函数崩溃应用程序但从 MainActivity.java 调用时不会
- python - 如何用 Python 重新排列一个复杂的方程
- tensorflow - 视频对象分类建议
- highcharts - HIghchart Treemap - 在图例单击时添加双击事件
- android - 获取所有画廊图像并在 android 应用程序中显示
- postcss - PostCSS 自定义插件 - TypeError:无法读取未定义的属性“_autoprefixerPrefix”