引入 aws-for-java 框架

前置步骤:安装 sdk(https://github.com/aws/aws-sdk-java-v2

s4-demo.java
implementation 'software.amazon.awssdk:s3:2.20.29'

连接到 S4 Endpoint

s4-demo.java
fun getS3Client() = S3Client.builder().region(Region.CN_NORTH_1).endpointOverride(URI.create("https://s3.bitiful.net/"))// 请填入控制台 “Bucket 设置” 页面底部的 “Endpoint” 标签中的信息
        .credentialsProvider(AwsCredentialsProvider {
            return@AwsCredentialsProvider object :AwsCredentials{
                override fun accessKeyId(): String {
                    return accessKey // 请到控制台创建子账户,并为子账户创建相应 accessKey
                }
                override fun secretAccessKey(): String {
                    return sercetKey // !!切记,创建子账户时,需要手动为其分配具体权限!!
                }
            }
        }).build()

列出桶中对象

s4-demo.java
val s3 = getS3Client()
s3.listObjects(ListObjectsRequest.builder().bucket(bucketName))

获取具备时效的预签名文件下载链接

s4-demo.java
val presign = S3Presigner.builder().region(Region.CN_NORTH_1).endpointOverride(URI.create("https://s3.bitiful.net/"))// 请填入控制台 “Bucket 设置” 页面底部的 “Endpoint” 标签中的信息
        .credentialsProvider(AwsCredentialsProvider {
            return@AwsCredentialsProvider object :AwsCredentials{
                override fun accessKeyId(): String {
                    return accessKey // 请到控制台创建子账户,并为子账户创建相应 accessKey
                }

                override fun secretAccessKey(): String {
                    return sercetKey // !!切记,创建子账户时,需要手动为其分配具体权限!!
                }

            }
        }).build()
        
val request = GetObjectRequest.builder().bucket(bucketName).key(key).build()
val presignRequest = GetObjectPresignRequest.builder()
           .signatureDuration(Duration.ofSeconds(time))
           .getObjectRequest(request).build()
val url = presign.presignGetObject(presignRequest).url().toString()