无法使用 scala akka-http 将文件保存到 s3

Unable to save file to s3 using scala akka-http

正在尝试将文件保存到 s3。文件以 Source[ByteString, Any] 形式出现,因此必须将其转换为 InputStream 以用于 PutObjectRequest。然后计算ObjectMetadata内容长度和md5。这是我的代码,后面是错误消息。任何输入将不胜感激。谢谢。

def uploadFile(source: Source[ByteString, _]) = {

  val stream: InputStream = source.runWith(StreamConverters.asInputStream(FiniteDuration(3, TimeUnit.SECONDS)))

  val metadata = new ObjectMetadata()
  metadata.setContentLength(IOUtils.toByteArray(stream).length)
  metadata.setContentMD5(Base64.encodeBase64(DigestUtils.md5(stream)).toString)

  s3.putObject(new PutObjectRequest("bucketName", "key", stream, metadata))
}

堆栈跟踪错误消息:com.amazonaws.services.s3.model.AmazonS3Exception with message The Content-MD5 you specified was invalid.

非阻塞,更多 reactive alternative would be to use Alpakka S3 接收器。

它将为您解决所有编码和 S3 协议问题,而且它将完全启用背压以增强可扩展性。

此外,它以 Sink[ByteString,_] 的形式出现,因此在您的情况下不需要更多数据 adaptation/conversion。