使用 AWS c++ 接口将文件上传到 s3 时内容类型标签不正确



我正在尝试使用 AWS C++ 开发工具包将文件上传到 S3。文件传输很好,但内容类型是"二进制/八位字节流"而不是"文本/html"。当我在 S3 控制台中查看文件时。有一个类型为"text/html"的"x-amz-meta-content-type"元数据记录。如何设置要上传的文件的实际内容类型?

int main (int, char**)
{
Aws::SDKOptions options;
Aws::InitAPI (options);
client = new Aws::S3::S3Client;
Aws::S3::Model::PutObjectRequest request;
request.SetBucket ("mywebsite.notreal.net");
request.SetKey ("index.html");
request.SetACL (Aws::S3::Model::ObjectCannedACL::public_read);
request.SetContentEncoding ("UTF-8");
request.AddMetadata ("Content-Type", "text/html");
auto inputData = Aws::MakeShared<Aws::FStream> ("PutObjectInputStream", "index.html", std::ios_base::in | std::ios_base::ate);
request.SetContentLength (inputData-> tellg ());
request.SetBody (inputData);
auto result = client-> PutObject (request);
return result.IsSuccess () ? 0 : 1;
}

v

需要在实际的PUT请求中将Content-Type设置为HTTP标头。

查看文档,您可以使用一种SetContentType方法。

最新更新