下载对象

您使用 getObject 接口下载获取指定桶中的指定对象的内容。

以下代码展示如何下载某一个桶中的对象:

bool S3Demo::GetObject()
{
  const Aws::String object_name = "ExampleObject.txt";
  Aws::S3::Model::GetObjectRequest object_request;
  object_request.SetBucket(s3_bucket_name);
  object_request.SetKey(object_name);

  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;
  }
}