skip to Main Content

I know that there are many similar questions, and this one is no exception

But unfortunately I can’t decide on the region for my case, how can I decide on the right region?

For example, when making a request to Postman, I encounter a similar error:
enter image description here

In my console i’m using EU (Frankfurt) eu-central-1 and also in terminal write smth like this:

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

And as I understand it, mine does not fit.

Also here is my AWS class:

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()
        }
}

2

Answers


  1. I think you may have the wrong region code; you do know that a Bucket is available in one and only one Region?

    In your logging settings, set this scope to debug:

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

    Then you should see something like this:

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

    That log is from a bucket in the London region eu-west-2

    Login or Signup to reply.
  2. To use Kotlin to interact with an Amazon S3 bucket (or other AWS services), consider using the AWS SDK for Kotlin. This SDK is meant for Kotlin developers. You are using the AWS SDK for Java.

    To put an object into an Amazon S3 bucket using the AWS SDK for Kotlin, use this code. Notice the region that you want to use is specified in the code block where you define the 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}")
        }
    }
    

    You can find this Kotlin example and many more in the AWS Code Library here:

    Amazon S3 examples using SDK for Kotlin

    ALso you can read the Kotlin DEV guide too. The link is at the start of the Code Example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search