【问题标题】:How to set a content length for AWS S3 HTTP upload with presigned url for a file that is gz-compressed in std::fstream?如何为在 std::fstream 中进行 gz 压缩的文件设置带有预签名 URL 的 AWS S3 HTTP 上传的内容长度?
【发布时间】:2019-01-31 15:14:04
【问题描述】:

我必须上传一个带有 S3 为我创建的预签名 URL 的文件。为了节省一些空间和时间,我使用了一个将文件压缩为 .gz 格式的流。所以 S3 只接收 gz 文件。

我使用AWS S3 CPP SDKAWS::HttpClient 用于 HTTP 请求,boost 用于压缩。

我在问自己如何正确设置 HTTP 请求的内容长度?一方面它是一个流,另一方面流将被压缩(gz),这使得它不容易。很遗憾,我不知道该怎么做。

我尝试使用seekg()tellg() 获取长度,但我无法解决我的问题。

auto&& f = std::fstream{ filepath.string(), std::ios_base::in | std::ios_base::binary };

auto in = std::make_shared<boost::iostreams::filtering_stream<boost::iostreams::bidirectional>>();
in->push(boost::iostreams::combine(boost::iostreams::gzip_compressor(), boost::iostreams::gzip_decompressor()));
in->push(f);

req->AddContentBody(in);
//req->SetContentLength(); <-- How?
req->SetContentType("application/octet-stream");

auto res = MyHttpClient->MakeRequest(req);  //upload stream

【问题讨论】:

    标签: c++ amazon-web-services http amazon-s3 stream


    【解决方案1】:

    Content-Length 必须是实际发送的字节数。如果您在发送数据时动态压缩数据,那么显然您无法预先知道最终压缩大小以正确设置Content-Length。幸运的是,您不必这样做,因为 S3 有一个支持流式上传的multipart upload API。见Can I stream a file upload to S3 without a content-length header?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2020-10-23
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多