上传对象

使用 putObject 接口上传对象。

bool S3Demo::PutObject()
{
  const Aws::String file_name = "E:/ExampleObject.txt";
  const Aws::String object_name = "ExampleObject.txt";
  // Verify that the file exists.
  struct stat buffer;
  if (stat(file_name.c_str(), &buffer) == -1)
  {
    std::cout << "Error: PutObject: File '" << object_name << "' does not exist." << std::endl;
    return false;
  }

  Aws::S3::Model::PutObjectRequest request;
  request.SetBucket(bucket_name);
  request.SetKey(object_name);
  request.SetACL(Aws::S3::Model::ObjectCannedACL::public_read); // 设置ACL

  std::shared_ptr<Aws::IOStream> input_data =
    Aws::MakeShared<Aws::FStream>("SampleAllocationTag",
      file_name.c_str(),
      std::ios_base::in | std::ios_base::binary);

  request.SetBody(input_data);

  Aws::S3::Model::PutObjectOutcome outcome = s3_client->PutObject(request);
  if (outcome.IsSuccess()) {
    std::cout << "Added object '" << object_name << "' to bucket '"
      << bucket_name << "'.";
    return true;
  }
  else
  {
    std::cout << "Error: PutObject: " << outcome.GetError().GetMessage() << std::endl;
    return false;
  }
}

参数如下:

参数名 意义
bucket 对象储存的桶名
key 对象在桶中的名称
acl 对象访问权限,可选private|public_read|public_read_write

注意:putObject对文件大小有限制,最大能上传1GB大小的文件,超过1GB需要使用分片上传。