Skip to content

Commit

Permalink
AWS S3: Expose Content-Type as part of ObjectMetaData (akka#858)
Browse files Browse the repository at this point in the history
* Extract Content-Type from response entity and augment the existing header list
  • Loading branch information
chetanmeh authored and ennru committed Mar 23, 2018
1 parent defa410 commit 53ad598
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
25 changes: 21 additions & 4 deletions s3/src/main/scala/akka/stream/alpakka/s3/impl/S3Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import akka.{Done, NotUsed}
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes.{NoContent, NotFound, OK}
import akka.http.scaladsl.model.headers.`Content-Length`
import akka.http.scaladsl.model.headers.{`Content-Length`, ByteRange, CustomHeader}
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.ByteRange
import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
import akka.stream.Materializer
import akka.stream.alpakka.s3.auth.{CredentialScope, Signer, SigningKey}
Expand Down Expand Up @@ -77,7 +76,7 @@ private[alpakka] final class S3Stream(settings: S3Settings)(implicit system: Act
.fromFuture(future.flatMap(entityForSuccess))
.map(_.dataBytes)
.flatMapConcat(identity)
val meta = future.map(resp ObjectMetadata(resp.headers))
val meta = future.map(resp computeMetaData(resp.headers, resp.entity))
(source, meta)
}

Expand Down Expand Up @@ -117,7 +116,7 @@ private[alpakka] final class S3Stream(settings: S3Settings)(implicit system: Act
request(S3Location(bucket, key), HttpMethods.HEAD, s3Headers = s3Headers).flatMap {
case HttpResponse(OK, headers, entity, _) =>
entity.discardBytes().future().map { _ =>
Some(ObjectMetadata(headers :+ `Content-Length`(entity.contentLengthOption.getOrElse(0))))
Some(computeMetaData(headers, entity))
}
case HttpResponse(NotFound, _, entity, _) =>
entity.discardBytes().future().map(_ => None)
Expand Down Expand Up @@ -221,6 +220,24 @@ private[alpakka] final class S3Stream(settings: S3Settings)(implicit system: Act
}
}

private def computeMetaData(headers: Seq[HttpHeader], entity: ResponseEntity): ObjectMetadata =
ObjectMetadata(
headers ++
Seq(
`Content-Length`(entity.contentLengthOption.getOrElse(0)),
CustomContentTypeHeader(entity.contentType)
)
)

//`Content-Type` header is by design not accessible as header. So need to have a custom
//header implementation to expose that
private case class CustomContentTypeHeader(contentType: ContentType) extends CustomHeader {
override def name(): String = "Content-Type"
override def value(): String = contentType.value
override def renderInRequests(): Boolean = true
override def renderInResponses(): Boolean = true
}

private def completeMultipartUpload(s3Location: S3Location,
parts: Seq[SuccessfulUploadPart]): Future[CompleteMultipartUploadResult] = {
import mat.executionContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ final class ObjectMetadata private (
* @see ObjectMetadata#setContentType(String)
*/
lazy val contentType: Option[String] = metadata.collectFirst {
case ct: ContentType => ct.value
case h if h.lowercaseName() == "content-type" => h.value
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package akka.stream.alpakka.s3.scaladsl

import akka.actor.ActorSystem
import akka.http.scaladsl.model.ContentTypes
import akka.stream.ActorMaterializer
import akka.stream.alpakka.s3.S3Settings
import akka.stream.alpakka.s3.impl.{MetaHeaders, S3Headers}
Expand Down Expand Up @@ -109,6 +110,7 @@ trait S3IntegrationSpec extends FlatSpecLike with BeforeAndAfterAll with Matcher
val (putResult, deleteResult, metaBefore, metaAfter) = Await.ready(result, 90.seconds).futureValue
putResult.eTag should not be empty
metaBefore should not be empty
metaBefore.get.contentType shouldBe Some(ContentTypes.`application/octet-stream`.value)
metaAfter shouldBe empty
}

Expand Down Expand Up @@ -144,6 +146,7 @@ trait S3IntegrationSpec extends FlatSpecLike with BeforeAndAfterAll with Matcher
val (body, meta) = Await.ready(result, 5.seconds).futureValue
body shouldBe objectValue
meta.eTag should not be empty
meta.contentType shouldBe Some(ContentTypes.`application/octet-stream`.value)
}

it should "delete with real credentials" in {
Expand Down

0 comments on commit 53ad598

Please sign in to comment.