0

I am a new to working with kotlin and the AWS kotlin SDK. Our application receives via AWS SQS an URL form a AWS S3 Bucket pointing to a file. We do have to download the file in our application for further processing. We found the following example code link which is cool.

val request = GetObjectRequest {
    key = keyName
    bucket = bucketName
}

S3Client { region = "us-east-1" }.use { s3 ->
    s3.getObject(request) { resp ->
        val myFile = File(path)
        resp.body?.writeToFile(myFile)
        println("Successfully read $keyName from $bucketName")
    }
}

However we don't know how to transform a S3 Bucket URL into a GetOjectRequest, for java we fond the following code on "Stackoverflow" link :-)

URI fileToBeDownloaded = new URI(" https://s3.amazonaws.com/account-update/input.csv"); 

AmazonS3URI s3URI = new AmazonS3URI(fileToBeDownloaded);

S3Object s3Object = s3Client.getObject(s3URI.getBucket(), s3URI.getKey());

So how to download a S3 Bucket file using the AWS kotlin SDK and and how to convert a URL into a bucketName and a keyName in the same way as in the Java example above.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Why does the sender transmit a URL for the S3 object if the underlying object is not public? The URL would not be usable in that case. A better option would be to send the bucket and key e.g. in a JSON payload. Or is the object actually public? – jarmod Mar 30 '23 at 13:16
  • BTW if `https://s3.amazonaws.com/account-update/input.csv` is the URL then `account-update` is the bucket name and `input.csv` is the key. However, that's not the only possible format of URL for an S3 object. – jarmod Mar 30 '23 at 13:18
  • That second code example [does quite a bit of work behind the scenes](https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/AmazonS3URI.java#L104) to pull out the bucket name and key name (ignoring the errant space in the example). Unless you have a specific case where that makes sense, you're _much_ better off if the component responsible for telling you about the object just informs you of the bucket and key name directly using a standard encoding. – Anon Coward Mar 30 '23 at 14:47

0 Answers0