Quick way to list all files in Amazon S3 bucket?

前端 未结 28 1809
星月不相逢
星月不相逢 2020-11-28 01:32

I have an amazon s3 bucket that has tens of thousands of filenames in it. What\'s the easiest way to get a text file that lists all the filenames in the bucket?

28条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 01:45

    Simplified and updated version of the Scala answer by Paolo:

    import scala.collection.JavaConversions.{collectionAsScalaIterable => asScala}
    import com.amazonaws.services.s3.AmazonS3
    import com.amazonaws.services.s3.model.{ListObjectsRequest, ObjectListing, S3ObjectSummary}
    
    def buildListing(s3: AmazonS3, request: ListObjectsRequest): List[S3ObjectSummary] = {
      def buildList(listIn: List[S3ObjectSummary], bucketList:ObjectListing): List[S3ObjectSummary] = {
        val latestList: List[S3ObjectSummary] = bucketList.getObjectSummaries.toList
    
        if (!bucketList.isTruncated) listIn ::: latestList
        else buildList(listIn ::: latestList, s3.listNextBatchOfObjects(bucketList))
      }
    
      buildList(List(), s3.listObjects(request))
    }
    

    Stripping out the generics and using the ListObjectRequest generated by the SDK builders.

提交回复
热议问题