Skip to content

Commit

Permalink
Specify size of uploaded S3 objects
Browse files Browse the repository at this point in the history
Not all third-party "S3-compatible" storage implementations support the
x-amz-checksum-algorithm header, so we should instead simply specify the
content length of uploaded objects for wider provider support.
  • Loading branch information
lberrymage committed Sep 18, 2024
1 parent fa09c35 commit 8ed6233
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,13 @@ fun Route.createDraftRoute() {
}

val iconFileId = iconData.inputStream()
.use { runBlocking { storageService.saveFile(it) } }
.use {
runBlocking {
storageService.saveFile(it, iconData.size.toLong())
}
}
val appFileId = tempApkSet.inputStream()
.use { runBlocking { storageService.saveFile(it) } }
.use { runBlocking { storageService.saveFile(it, tempApkSet.size()) } }
val icon = Icon.new { fileId = iconFileId }
Draft.new {
this.label = label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ fun Route.createUpdateRoute() {
return@post
}

val apkSetFileId = tempApkSet.inputStream().use { storageService.saveFile(it) }
val apkSetFileId =
tempApkSet.inputStream().use { storageService.saveFile(it, tempApkSet.size()) }

// There exists:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface FileStorageService {
*
* @return the database ID of the new file
*/
suspend fun saveFile(inputStream: InputStream): EntityID<Int>
suspend fun saveFile(inputStream: InputStream, size: Long): EntityID<Int>

/**
* Marks the given file deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import app.accrescent.parcelo.console.data.Files.deleted
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider.Companion.invoke
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.ChecksumAlgorithm
import aws.sdk.kotlin.services.s3.model.Delete
import aws.sdk.kotlin.services.s3.model.DeleteObjectRequest
import aws.sdk.kotlin.services.s3.model.DeleteObjectsRequest
Expand Down Expand Up @@ -40,7 +39,7 @@ class S3FileStorageService(
private val s3AccessKeyId: String,
private val s3SecretAccessKey: String,
) : FileStorageService {
override suspend fun saveFile(inputStream: InputStream): EntityID<Int> {
override suspend fun saveFile(inputStream: InputStream, size: Long): EntityID<Int> {
S3Client {
endpointUrl = s3EndpointUrl
region = s3Region
Expand All @@ -54,9 +53,7 @@ class S3FileStorageService(
val req = PutObjectRequest {
bucket = s3Bucket
key = objectKey
body = inputStream.asByteStream()
// Necessary to specify so the S3 library can calculate a body hash from a stream
checksumAlgorithm = ChecksumAlgorithm.Sha256
body = inputStream.asByteStream(size)
}
s3Client.putObject(req)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class TempFile : AutoCloseable {
return path.toFile().outputStream()
}

/**
* Returns the size of this file in bytes
*/
fun size(): Long {
return path.toFile().length()
}

/**
* Deletes the underlying file
*/
Expand Down

0 comments on commit 8ed6233

Please sign in to comment.