For Scala developers, here a recursive function performs a full scan and display of AmazonS3 bucket contents using the official AWS SDK for Java
import com.amazonaws.services.s3.AmazonS3Client import com.amazonaws.services.s3.model.{S3ObjectSummary, ObjectListing, GetObjectRequest} import scala.collection.JavaConversions.{collectionAsScalaIterable => asScala} def map[T](s3: AmazonS3Client, bucket: String, prefix: String)(f: (S3ObjectSummary) => T) = { def scan(acc:List[T], listing:ObjectListing): List[T] = { val summaries = asScala[S3ObjectSummary](listing.getObjectSummaries()) val mapped = (for (summary <- summaries) yield f(summary)).toList if (!listing.isTruncated) mapped.toList else scan(acc ::: mapped, s3.listNextBatchOfObjects(listing)) } scan(List(), s3.listObjects(bucket, prefix)) }
To call the aforementioned curried map() function, simply pass the already-built (and correctly initialized) AmazonS3Client object (refer to the official AWS SDK for Java API Link ), the bucket name and the prefix name in the list of first parameters. Also pass the f() function that you want to apply to match all the object summaries in the second parameter list.
for instance
map(s3, bucket, prefix) { s => println(s.getKey.split("/")(1)) }
print all file names (no prefix)
val tuple = map(s3, bucket, prefix)(s => (s.getKey, s.getOwner, s.getSize))
will return the full list of tuples (key, owner, size) in this bucket / prefix
val totalSize = map(s3, "bucket", "prefix")(s => s.getSize).sum
will return the total size of its contents (note the additional folding function sum() applied at the end of the expression ;-)
You can combine map() with many other functions, since you usually approach Monads in functional programming