列出S3上的文件夹

时间:2018-09-08 12:48:38

标签: amazon-web-services amazon-s3 kotlin

只是试图解析并显示整个存储桶,我无法摆脱消息no suchKeyException这是奇怪的..

下面的代码正在连接并显示到标准输出:

    import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider
    import software.amazon.awssdk.regions.Region
    import software.amazon.awssdk.services.s3.S3Client
    import software.amazon.awssdk.services.s3.model.EncodingType
    import software.amazon.awssdk.services.s3.model.ListObjectsV2Request
    import software.amazon.awssdk.services.s3.model.ListObjectsV2Response
    import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable
    import java.net.URI

    internal class S3ObjectsOps {

    companion object {

            private val BUCKET: String = "mybucket"
            private val REGION_IRE = software.amazon.awssdk.regions.Region.EU_WEST_1

            val s3c = S3Client.builder()
                    .region(REGION_IRE)
                    .endpointOverride(URI("https://${BUCKET}.s3-eu-west-1.amazonaws.com/"))
                    .credentialsProvider(ProfileCredentialsProvider.builder()
                            .profileName("default")
                            .build())
                    .build()

            fun lsfolders(bucket: String) {
                val listReq = ListObjectsV2Request.builder()
                        .bucket(bucket)
                        .delimiter("/")
                        .prefix("")
                        .maxKeys(10_000)
                        .build()

                val listRes = s3c.listObjectsV2Paginator(listReq)

                listRes.contents().onEach { f ->
                    if (!f.key().isNullOrBlank()) {
                        println(">> ${f.key()}")
                    } else {
                        println(" = ")
                    }
                }

            }

            @JvmStatic
            fun main(args: Array<String>) {
                val bucket: String = ((args.size > 0 && !args[0].isNullOrEmpty()).toString()) ?: BUCKET
                println("s3 ls test for bucket ${bucket}")
                lsfolders(bucket)
            }
        }

    }

哪个加薪:

  

software.amazon.awssdk.services.s3.model.NoSuchKeyException:指定的密钥不存在。 (服务:S3Client;状态代码:404;请求ID:C8AF9CB788D77F74)   线程“主”中的软件software.amazon.awssdk.services.s3.model.NoSuchKeyException异常:指定的键不存在。 (服务:S3Client;状态代码:404;请求ID:C8AF9CB788D77F74)       在software.amazon.awssdk.core.http.pipeline.stages.HandleResponseStage.handleErrorResponse(HandleResponseStage.java:114)

使用流的相同异常

   listRes.contents().stream().forEach { content -> println(" Key: " + content.key() + " | ") }

谢谢大家!

1 个答案:

答案 0 :(得分:0)

ListObjectsV2Request的正确调用是:

val listReq = ListObjectsV2Request.builder()
                .bucket(bucket)
                .delimiter("/")
                .build()

        val listRes = s3c.listObjectsV2Paginator(listReq)
        listRes.contents().stream().forEach { content -> println(" Key: " + content.key() + " | ") }

然后,s3client必须使用最小参数进行初始化:

   val s3c = S3Client.builder()
            .credentialsProvider(ProfileCredentialsProvider.builder().profileName("default").build())
            .region(REGION_IRE)
            .build()