Skip to content

Commit c365bd7

Browse files
authored
fix(storage): Improve exception handling when attempting to overwrite a file during download (#3056)
1 parent 23d6e6d commit c365bd7

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/extensions/StorageExceptionExtensions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.amplifyframework.storage.s3.extensions
1616

1717
import com.amplifyframework.storage.StorageException
18+
import com.amplifyframework.storage.StorageFilePermissionException
1819
import com.amplifyframework.storage.StoragePathValidationException
1920

2021
internal fun StoragePathValidationException.Companion.invalidStoragePathException() = StorageException(
@@ -31,3 +32,12 @@ internal fun StoragePathValidationException.Companion.unsupportedStoragePathExce
3132
),
3233
"Provided StoragePath not supported by AWS S3 Storage Plugin"
3334
)
35+
36+
internal fun StorageFilePermissionException.Companion.unableToOverwriteFileException() = StorageException(
37+
"Unable to overwrite this file for download.",
38+
StorageFilePermissionException(
39+
"Unable to overwrite this file for download.",
40+
"Acquire write permission for this file before attempting to overwrite it."
41+
),
42+
"Acquire write permission for this file before attempting to overwrite it."
43+
)

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/TransferManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import aws.sdk.kotlin.services.s3.model.ObjectCannedAcl
2424
import com.amplifyframework.core.Amplify
2525
import com.amplifyframework.core.category.CategoryType
2626
import com.amplifyframework.storage.ObjectMetadata
27+
import com.amplifyframework.storage.StorageFilePermissionException
2728
import com.amplifyframework.storage.s3.AWSS3StoragePlugin
2829
import com.amplifyframework.storage.s3.TransferOperations
30+
import com.amplifyframework.storage.s3.extensions.unableToOverwriteFileException
2931
import com.amplifyframework.storage.s3.transfer.worker.RouterWorker
3032
import com.amplifyframework.storage.s3.transfer.worker.TransferWorkerFactory
3133
import java.io.File
@@ -192,6 +194,9 @@ internal class TransferManager(
192194
val transferRecordId: Int = uri.lastPathSegment?.toInt()
193195
?: throw IllegalStateException("Invalid TransferRecord ID ${uri.lastPathSegment}")
194196
if (file.isFile) {
197+
if (!file.canWrite()) {
198+
throw StorageFilePermissionException.unableToOverwriteFileException()
199+
}
195200
logger.warn("Overwriting existing file: $file")
196201
file.delete()
197202
}

core/api/core.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,6 +4212,9 @@ public final class com/amplifyframework/storage/StorageException : com/amplifyfr
42124212
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;Ljava/lang/String;)V
42134213
}
42144214

4215+
public final class com/amplifyframework/storage/StorageFilePermissionException : com/amplifyframework/AmplifyException {
4216+
}
4217+
42154218
public final class com/amplifyframework/storage/StorageItem {
42164219
public fun <init> (Ljava/lang/String;JLjava/util/Date;Ljava/lang/String;Ljava/lang/Object;)V
42174220
public final fun component1 ()Ljava/lang/String;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amplifyframework.storage
16+
17+
import com.amplifyframework.AmplifyException
18+
import com.amplifyframework.annotations.InternalAmplifyApi
19+
20+
/**
21+
* Exception thrown when the necessary file permissions needed to handle a file have not been granted.
22+
*/
23+
class StorageFilePermissionException @InternalAmplifyApi constructor(
24+
message: String,
25+
recoverySuggestion: String
26+
) : AmplifyException(message, recoverySuggestion) {
27+
@InternalAmplifyApi companion object
28+
}

0 commit comments

Comments
 (0)