-
Notifications
You must be signed in to change notification settings - Fork 641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added list bucket method to S3 library. #253
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0802ce2
Added recursive listBucket call to get all keys under a specific prefix.
eca8fd8
Properly using the request URI method and constructing queries with t…
eafcbc1
Added tests around query parsing
0e91955
Fixed formatting and removed recoverWithRetries on listbucket calls a…
61a27bf
Using signAndGetAs instead of signAndGet as to not duplicate logic.
a337d3c
Merge branch 'master' into s3-listBucket
jrudolph 4d4a92e
Implemented quick fixes based on comments. Removed recursive call to …
9b3d4e6
Merge branch 's3-listBucket' of github.com:Scalahansolo/alpakka into …
5d50d73
Added execution context. Fixed broken test
414faee
Fixed formatting error.
515ee41
Cleaned up lisBucket call by added a ListBucketState object instead o…
a634d2b
Moved trait for listBucket into the def itself as to remove it from t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,31 @@ import akka.http.scaladsl.model.{ContentTypes, RequestEntity, _} | |
import akka.stream.alpakka.s3.S3Settings | ||
import akka.stream.scaladsl.Source | ||
import akka.util.ByteString | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
private[alpakka] object HttpRequests { | ||
|
||
def listBucket( | ||
bucket: String, | ||
region: String, | ||
prefix: Option[String] = None, | ||
continuationToken: Option[String] = None | ||
)(implicit conf: S3Settings): HttpRequest = { | ||
|
||
val query = Query( | ||
Seq( | ||
"list-type" -> Some("2"), | ||
"prefix" -> prefix, | ||
"continuation-token" -> continuationToken.map(_.replaceAll("=", "%3D")) | ||
).collect { case (k, Some(v)) => k -> v }.toMap | ||
) | ||
|
||
HttpRequest(HttpMethods.GET) | ||
.withHeaders(Host(requestHost(bucket, region))) | ||
.withUri(requestUri(bucket, None, region).withQuery(query)) | ||
} | ||
|
||
def getDownloadRequest(s3Location: S3Location, region: String)(implicit conf: S3Settings): HttpRequest = | ||
s3Request(s3Location, region: String) | ||
|
||
|
@@ -69,42 +88,41 @@ private[alpakka] object HttpRequests { | |
private[this] def s3Request(s3Location: S3Location, | ||
region: String, | ||
method: HttpMethod = HttpMethods.GET, | ||
uriFn: (Uri => Uri) = identity)(implicit conf: S3Settings): HttpRequest = { | ||
|
||
def requestHost(s3Location: S3Location, region: String)(implicit conf: S3Settings): Uri.Host = | ||
conf.proxy match { | ||
case None => | ||
region match { | ||
case "us-east-1" => | ||
if (conf.pathStyleAccess) { | ||
Uri.Host("s3.amazonaws.com") | ||
} else { | ||
Uri.Host(s"${s3Location.bucket}.s3.amazonaws.com") | ||
} | ||
case _ => | ||
if (conf.pathStyleAccess) { | ||
Uri.Host(s"s3-$region.amazonaws.com") | ||
} else { | ||
Uri.Host(s"${s3Location.bucket}.s3-$region.amazonaws.com") | ||
} | ||
} | ||
case Some(proxy) => Uri.Host(proxy.host) | ||
} | ||
uriFn: (Uri => Uri) = identity)(implicit conf: S3Settings): HttpRequest = | ||
HttpRequest(method) | ||
.withHeaders(Host(requestHost(s3Location.bucket, region))) | ||
.withUri(uriFn(requestUri(s3Location.bucket, Some(s3Location.key), region))) | ||
|
||
def requestUri(s3Location: S3Location, region: String)(implicit conf: S3Settings): Uri = { | ||
val uri = if (conf.pathStyleAccess) { | ||
Uri(s"/${s3Location.bucket}/${s3Location.key}").withHost(requestHost(s3Location, region)) | ||
} else { | ||
Uri(s"/${s3Location.key}").withHost(requestHost(s3Location, region)) | ||
} | ||
conf.proxy match { | ||
case None => uri.withScheme("https") | ||
case Some(proxy) => uri.withPort(proxy.port).withScheme(proxy.scheme) | ||
} | ||
private[this] def requestHost(bucket: String, region: String)(implicit conf: S3Settings): Uri.Host = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, to put extract that method. |
||
conf.proxy match { | ||
case None => | ||
region match { | ||
case "us-east-1" => | ||
if (conf.pathStyleAccess) { | ||
Uri.Host("s3.amazonaws.com") | ||
} else { | ||
Uri.Host(s"$bucket.s3.amazonaws.com") | ||
} | ||
case _ => | ||
if (conf.pathStyleAccess) { | ||
Uri.Host(s"s3-$region.amazonaws.com") | ||
} else { | ||
Uri.Host(s"$bucket.s3-$region.amazonaws.com") | ||
} | ||
} | ||
case Some(proxy) => Uri.Host(proxy.host) | ||
} | ||
|
||
HttpRequest(method) | ||
.withHeaders(Host(requestHost(s3Location, region))) | ||
.withUri(uriFn(requestUri(s3Location, region))) | ||
private[this] def requestUri(bucket: String, key: Option[String], region: String)(implicit conf: S3Settings): Uri = { | ||
val uri = if (conf.pathStyleAccess) { | ||
Uri(s"/${bucket}${key.fold("")((someKey) => s"/$someKey")}") | ||
.withHost(requestHost(bucket, region)) | ||
} else { | ||
Uri(s"${key.fold("")((someKey) => s"/$someKey")}").withHost(requestHost(bucket, region)) | ||
} | ||
conf.proxy match { | ||
case None => uri.withScheme("https") | ||
case Some(proxy) => uri.withPort(proxy.port).withScheme(proxy.scheme) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ package akka.stream.alpakka.s3.impl | |
|
||
import java.nio.file.Paths | ||
import java.time.LocalDate | ||
|
||
import akka.NotUsed | ||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
|
@@ -18,7 +17,6 @@ import akka.stream.alpakka.s3.auth.{AWSCredentials, CredentialScope, Signer, Sig | |
import akka.stream.alpakka.s3.{DiskBufferType, MemoryBufferType, S3Exception, S3Settings} | ||
import akka.stream.scaladsl.{Flow, Keep, Sink, Source} | ||
import akka.util.ByteString | ||
|
||
import scala.collection.immutable.Seq | ||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.{Failure, Success} | ||
|
@@ -43,6 +41,8 @@ final case class FailedUpload(reasons: Seq[Throwable]) extends Exception | |
|
||
final case class CompleteMultipartUploadResult(location: Uri, bucket: String, key: String, etag: String) | ||
|
||
final case class ListBucketResult(isTruncated: Boolean, continuationToken: Option[String], keys: Seq[String]) | ||
|
||
object S3Stream { | ||
|
||
def apply(credentials: AWSCredentials, region: String)(implicit system: ActorSystem, mat: Materializer): S3Stream = | ||
|
@@ -65,6 +65,32 @@ private[alpakka] final class S3Stream(credentials: AWSCredentials, | |
Source.fromFuture(request(s3Location, range).flatMap(entityForSuccess).map(_.dataBytes)).flatMapConcat(identity) | ||
} | ||
|
||
def listBucket(bucket: String, prefix: Option[String] = None): Source[String, NotUsed] = { | ||
sealed trait ListBucketState | ||
case object Starting extends ListBucketState | ||
case class Running(continuationToken: String) extends ListBucketState | ||
case object Finished extends ListBucketState | ||
|
||
import system.dispatcher | ||
|
||
def listBucketCall(token: Option[String]): Future[Option[(ListBucketState, Seq[String])]] = | ||
signAndGetAs[ListBucketResult](HttpRequests.listBucket(bucket, region, prefix, token)) | ||
.map { (res: ListBucketResult) => | ||
Some( | ||
res.continuationToken | ||
.fold[(ListBucketState, Seq[String])]((Finished, res.keys))(t => (Running(t), res.keys)) | ||
) | ||
} | ||
|
||
Source | ||
.unfoldAsync[ListBucketState, Seq[String]](Starting) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is great now. |
||
case Finished => Future.successful(None) | ||
case Starting => listBucketCall(None) | ||
case Running(token) => listBucketCall(Some(token)) | ||
} | ||
.mapConcat(identity) | ||
} | ||
|
||
def request(s3Location: S3Location, rangeOption: Option[ByteRange] = None): Future[HttpResponse] = { | ||
val downloadRequest = getDownloadRequest(s3Location, region) | ||
signAndGet(rangeOption match { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it wouldn't make sense to use separate methods for the initial call and the continuation. It seems that you either would want to add a prefix or a token both never both?