5

我正在 Scala 中构建一个 Play Framework 应用程序,我想将一个字节数组流式传输到 S3。我正在使用Play-S3库来执行此操作。文档部分的“多部分文件上传”与此处相关:

// Retrieve an upload ticket
val result:Future[BucketFileUploadTicket] =
  bucket initiateMultipartUpload BucketFile(fileName, mimeType)

// Upload the parts and save the tickets
val result:Future[BucketFilePartUploadTicket] =
  bucket uploadPart (uploadTicket, BucketFilePart(partNumber, content))

// Complete the upload using both the upload ticket and the part upload tickets
val result:Future[Unit] =
  bucket completeMultipartUpload (uploadTicket, partUploadTickets)

我试图在我的应用程序中做同样的事情,但使用Iteratees 和Enumerators。

流和异步性使事情变得有点复杂,但这是我到目前为止所拥有的(注意uploadTicket在代码前面定义):

val partNumberStream = Stream.iterate(1)(_ + 1).iterator
val partUploadTicketsIteratee = Iteratee.fold[Array[Byte], Future[Vector[BucketFilePartUploadTicket]]](Future.successful(Vector.empty[BucketFilePartUploadTicket])) { (partUploadTickets, bytes) =>
  bucket.uploadPart(uploadTicket, BucketFilePart(partNumberStream.next(), bytes)).flatMap(partUploadTicket => partUploadTickets.map( _ :+ partUploadTicket))
}
(body |>>> partUploadTicketsIteratee).andThen {
  case result =>
    result.map(_.map(partUploadTickets => bucket.completeMultipartUpload(uploadTicket, partUploadTickets))) match {
      case Success(x) => x.map(d => println("Success"))
      case Failure(t) => throw t
    }
}

一切都编译并运行,没有发生任何事件。事实上,"Success"被打印出来了,但没有文件出现在 S3 上。

4

1 回答 1

6

您的代码可能存在多个问题。map方法调用导致它有点不可读。你未来的作曲可能有问题。另一个问题可能是由于所有块(最后一个块除外)应至少为 5MB。

下面的代码未经测试,但显示了不同的方法。迭代方法是一种您可以创建小型构建块并将它们组合成操作管道的方法。

为了使代码编译,我添加了一个特征和一些方法

trait BucketFilePartUploadTicket
val uploadPart: (Int, Array[Byte]) => Future[BucketFilePartUploadTicket] = ???
val completeUpload: Seq[BucketFilePartUploadTicket] => Future[Unit] = ???
val body: Enumerator[Array[Byte]] = ???

这里我们创建了几个部分

// Create 5MB chunks
val chunked = {
  val take5MB = Traversable.takeUpTo[Array[Byte]](1024 * 1024 * 5)
  Enumeratee.grouped(take5MB transform Iteratee.consume())
}

// Add a counter, used as part number later on
val zipWithIndex = Enumeratee.scanLeft[Array[Byte]](0 -> Array.empty[Byte]) {
  case ((counter, _), bytes) => (counter + 1) -> bytes
}

// Map the (Int, Array[Byte]) tuple to a BucketFilePartUploadTicket
val uploadPartTickets = Enumeratee.mapM[(Int, Array[Byte])](uploadPart.tupled)

// Construct the pipe to connect to the enumerator
// the ><> operator is an alias for compose, it is more intuitive because of 
// it's arrow like structure
val pipe = chunked ><> zipWithIndex ><> uploadPartTickets

// Create a consumer that ends by finishing the upload
val consumeAndComplete = 
  Iteratee.getChunks[BucketFilePartUploadTicket] mapM completeUpload

运行它是通过简单地连接部件来完成的

// This is the result, a Future[Unit]
val result = body through pipe run consumeAndComplete 

请注意,我没有测试任何代码,并且可能在我的方法中犯了一些错误。然而,这显示了处理问题的不同方式,应该可以帮助您找到一个好的解决方案。

请注意,此方法会等待一个部分完成上传,然后再进行下一部分。如果从您的服务器到亚马逊的连接比从浏览器到您的服务器的连接慢,则此机制将减慢输入速度。

您可以采取另一种方法,您无需等待Future部分上传完成。这将导致您使用另一个步骤Future.sequence将上传期货序列转换为包含结果序列的单个期货。结果将是一种机制,一旦您有足够的数据,就会将零件发送到亚马逊。

于 2014-12-21T13:53:16.773 回答