0

I'd like to observe a tree of objects, in reverse dependency order, but do not know what combinators to use. The specific case is iterating through AWS Resources in order for deletion, for example deleting S3 Objects before deleting their S3 Buckets, like this:

  val S3 = new AmazonS3Client

  val buckets = Observable.from {
    S3.listBuckets.asScala
  }

  val objects = buckets.flatMap { b => Observable.from(
    S3.listObjects(new ListObjectsRequest().withBucketName(b.getName))
      .getObjectSummaries
      .asScala)
  }

  val combined:Observable[_] = ???

  for (thing <- combined) thing match {
    case b:Bucket => println("delete bucket")
    case o:S3ObjectSummary => println("delete object")
  }

So, the final combined observable should emit all the objects before emitting the bucket. What combinator should i use for that?

Julio Faerman
  • 13,228
  • 9
  • 57
  • 75

1 Answers1

1

You can append b to the end of its objects, e.g.,

val objects = buckets.flatMap { b => Observable.from(
  S3.listObjects(new ListObjectsRequest().withBucketName(b.getName))
    .getObjectSummaries
    .asScala) :+ b
}
zsxwing
  • 20,270
  • 4
  • 37
  • 59
  • Thanks, that worked out fine! Created a Gist, case someone find this useful: https://gist.github.com/jfaerman/256a4d8873c3ab3fd82d – Julio Faerman Aug 14 '15 at 22:48