heroku S3例外:您尝试访问的存储桶必须使用指定的端点进行寻址

qf9go6mv  于 2022-12-04  发布在  其他
关注(0)|答案(2)|浏览(165)

我知道有很多类似的问题,this one也不例外
但不幸的是,我不能决定我的情况下的地区,我怎么才能决定正确的地区?
例如,在向Postman发出请求时,我遇到了类似的错误:

在我的控制台中,我使用EU (Frankfurt) eu-central-1,在终端中也使用smth,如下所示:

heroku config:set region="eu-central-1"

据我所知,我的不适合。
下面是我的AWS类:

class AmazonFileStorage : FileStorage {

    private val client: S3Client

    private val bucketName: String = System.getenv("bucketName")

    init {
        val region = System.getenv("region")
        val accessKey = System.getenv("accessKey")
        val secretKey = System.getenv("secretKey")

        val credentials = AwsBasicCredentials.create(accessKey, secretKey)
        val awsRegion = Region.of(region)
        client = S3Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(credentials))
            .region(awsRegion)
            .build() as S3Client
    }

    override suspend fun save(file: File): String =
        withContext(Dispatchers.IO) {
            client.putObject(
                PutObjectRequest.builder().bucket(bucketName).key(file.name).acl(ObjectCannedACL.PUBLIC_READ).build(),
                RequestBody.fromFile(file)
            )
            val request = GetUrlRequest.builder().bucket(bucketName).key(file.name).build()
            client.utilities().getUrl(request).toExternalForm()
        }
}
oyjwcjzk

oyjwcjzk1#

我想您可能输入了错误的地区代码;您是否知道Bucket只在一个区域提供?
在日志记录设置中,将此范围设置为调试:

logging:
  level:
    org.apache.http.wire: debug

然后,您应该会看到如下所示的内容:

http-outgoing-0 >> "HEAD /somefile HTTP/1.1[\r][\n]"
http-outgoing-0 >> "Host: YOURBUCKETNAME.s3.eu-west-2.amazonaws.com[\r][\n]"

这段日志来自伦敦地区的一个桶eu-west-2

9lowa7mx

9lowa7mx2#

要使用Kotlin与Amazon S3 bucket(或其他AWS服务)交互,请考虑使用AWS SDK for Kotlin。此SDK专为Kotlin开发人员设计。您使用的是AWS SDK for Java
要使用AWS SDK forKotlin将对象放入Amazon S3 bucket,请使用以下代码。注意,要使用的区域在定义aws.sdk.kotlin.services.s3.S3Client的代码块中指定。

import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.smithy.kotlin.runtime.content.asByteStream
import java.io.File
import kotlin.system.exitProcess

/**
Before running this Kotlin code example, set up your development environment,
including your credentials.

For more information, see the following documentation topic:
https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html
 */
suspend fun main(args: Array<String>) {

    val usage = """
    Usage:
        <bucketName> <objectKey> <objectPath>

    Where:
        bucketName - The Amazon S3 bucket to upload an object into.
        objectKey - The object to upload (for example, book.pdf).
        objectPath - The path where the file is located (for example, C:/AWS/book2.pdf).
    """

    if (args.size != 3) {
        println(usage)
        exitProcess(0)
    }

    val bucketName = args[0]
    val objectKey = args[1]
    val objectPath = args[2]
    putS3Object(bucketName, objectKey, objectPath)
}

suspend fun putS3Object(bucketName: String, objectKey: String, objectPath: String) {

    val metadataVal = mutableMapOf<String, String>()
    metadataVal["myVal"] = "test"

    val request = PutObjectRequest {
        bucket = bucketName
        key = objectKey
        metadata = metadataVal
        body = File(objectPath).asByteStream()
    }

    S3Client { region = "us-east-1" }.use { s3 ->
        val response = s3.putObject(request)
        println("Tag information is ${response.eTag}")
    }
}

您可以在AWS代码库中找到此Kotlin示例和更多示例,网址为:
Amazon S3 examples using SDK for Kotlin
你也可以阅读KotlinDEV指南。链接在代码示例的开头。

相关问题