0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Amazon S3のバケットにあるファイルから最新のファイル名を1つ取得

Posted at

AWS SDK (Kotlin)

最大1000件(デフォルト1000件)のリストを取得して、lastModifiedでソートしてインデックス0のデータを取得。
取得できるデータは1000件な名前の昇順であるため、1000件以上のデータがある場合には、正しいデータを得られない。

        fun getLatestUploadObject(context: Context, bucketName: String, objectPrefix: String): S3ObjectSummary? {
            val request = ListObjectsV2Request().also {
                it.bucketName = bucketName
                it.prefix = objectPrefix
            }
            val credentialsProvider = <CredentialsProvider>

            /** Create S3 Client */
            val s3 = AmazonS3Client(credentialsProvider, Region.getRegion(Regions.AP_NORTHEAST_1))
            val result = s3.listObjectsV2(request);
            if (result.objectSummaries.size > 0) {
                val sortedObjects = result.objectSummaries.sortedByDescending {
                    it.lastModified
                }
                return sortedObjects[0]
            }
            return null
        }

AWS CLI

> aws s3api list-objects-v2 \
  --bucket "<BucketName>" \
  --query 'reverse(sort_by(Contents, &LastModified))[:1].Key' \
  --output=text
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?