diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java index 48dce2ee2..46eef5e65 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java @@ -28,6 +28,7 @@ import com.amplifyframework.auth.CognitoCredentialsProvider; import com.amplifyframework.core.Consumer; import com.amplifyframework.core.NoOpConsumer; +import com.amplifyframework.core.async.AmplifyOperation; import com.amplifyframework.core.configuration.AmplifyOutputsData; import com.amplifyframework.storage.BucketInfo; import com.amplifyframework.storage.InvalidStorageBucketException; @@ -377,23 +378,19 @@ public StorageGetUrlOperation getUrl( validateObjectExistence ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StorageGetPresignedUrlOperation operation = new AWSS3StorageGetPresignedUrlOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, awsS3StoragePluginConfiguration, onSuccess, onError); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -419,22 +416,18 @@ public StorageGetUrlOperation getUrl( validateObjectExistence ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StoragePathGetPresignedUrlOperation operation = new AWSS3StoragePathGetPresignedUrlOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, onSuccess, onError); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -513,15 +506,10 @@ public StorageDownloadFileOperation downloadFile( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StorageDownloadFileOperation operation = new AWSS3StorageDownloadFileOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, @@ -530,7 +518,8 @@ public StorageDownloadFileOperation downloadFile( onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -555,23 +544,19 @@ public StorageDownloadFileOperation downloadFile( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StoragePathDownloadFileOperation operation = new AWSS3StoragePathDownloadFileOperation( request, - storageService, + result.storageService, executorService, authCredentialsProvider, onProgress, onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -654,15 +639,10 @@ public StorageUploadFileOperation uploadFile( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StorageUploadFileOperation operation = new AWSS3StorageUploadFileOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, @@ -671,7 +651,8 @@ public StorageUploadFileOperation uploadFile( onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -699,23 +680,19 @@ public StorageUploadFileOperation uploadFile( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StoragePathUploadFileOperation operation = new AWSS3StoragePathUploadFileOperation( request, - storageService, + result.storageService, executorService, authCredentialsProvider, onProgress, onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -796,15 +773,10 @@ public StorageUploadInputStreamOperation uploadInputStream( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StorageUploadInputStreamOperation operation = new AWSS3StorageUploadInputStreamOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, awsS3StoragePluginConfiguration, @@ -813,7 +785,8 @@ public StorageUploadInputStreamOperation uploadInputStream( onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -841,24 +814,20 @@ public StorageUploadInputStreamOperation uploadInputStream( useAccelerateEndpoint ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StoragePathUploadInputStreamOperation operation = new AWSS3StoragePathUploadInputStreamOperation( request, - storageService, + result.storageService, executorService, authCredentialsProvider, onProgress, onSuccess, onError ); - operation.start(); + + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -901,16 +870,11 @@ public StorageRemoveOperation remove( options.getTargetIdentityId() ); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StorageRemoveOperation operation = new AWSS3StorageRemoveOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, @@ -918,7 +882,7 @@ public StorageRemoveOperation remove( onSuccess, onError); - operation.start(); + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -933,23 +897,18 @@ public StorageRemoveOperation remove( ) { AWSS3StoragePathRemoveRequest request = new AWSS3StoragePathRemoveRequest(path); - AWSS3StorageService storageService = defaultStorageService; - try { - storageService = getStorageService(options.getBucket()); - } catch (StorageException exception) { - onError.accept(exception); - } + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); AWSS3StoragePathRemoveOperation operation = new AWSS3StoragePathRemoveOperation( - storageService, + result.storageService, executorService, authCredentialsProvider, request, onSuccess, onError); - operation.start(); + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -1096,9 +1055,11 @@ public StorageListOperation list(@NonNull String path, options.getNextToken(), options.getSubpathStrategy()); + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); + AWSS3StorageListOperation operation = new AWSS3StorageListOperation( - defaultStorageService, + result.storageService, executorService, authCredentialsProvider, request, @@ -1106,7 +1067,7 @@ public StorageListOperation list(@NonNull String path, onSuccess, onError); - operation.start(); + handleGetStorageServiceResult(onError, result, operation); return operation; } @@ -1125,20 +1086,47 @@ public StorageListOperation list( options.getNextToken(), options.getSubpathStrategy()); + GetStorageServiceResult result = getStorageServiceResult(options.getBucket()); + AWSS3StoragePathListOperation operation = new AWSS3StoragePathListOperation( - defaultStorageService, + result.storageService, executorService, authCredentialsProvider, request, onSuccess, onError); - operation.start(); + handleGetStorageServiceResult(onError, result, operation); return operation; } + private static void handleGetStorageServiceResult( + @NonNull Consumer onError, + GetStorageServiceResult result, + AmplifyOperation operation + ) { + if (result.storageException == null) { + operation.start(); + } else { + onError.accept(result.storageException); + } + } + + @VisibleForTesting + @NonNull + GetStorageServiceResult getStorageServiceResult(@Nullable StorageBucket bucket) { + StorageException storageException = null; + AWSS3StorageService storageService = defaultStorageService; + try { + storageService = getStorageService(bucket); + } catch (StorageException exception) { + storageException = exception; + } + return new GetStorageServiceResult(storageService, storageException); + } + @SuppressLint("UnsafeOptInUsageError") @VisibleForTesting @NonNull @@ -1209,4 +1197,16 @@ public String getConfigurationKey() { return configurationKey; } } + + @VisibleForTesting + @SuppressWarnings("checkstyle:VisibilityModifier") + static class GetStorageServiceResult { + final AWSS3StorageService storageService; + final StorageException storageException; + + GetStorageServiceResult(AWSS3StorageService storageService, StorageException exception) { + this.storageService = storageService; + this.storageException = exception; + } + } } diff --git a/aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/AWSS3StoragePluginTest.kt b/aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/AWSS3StoragePluginTest.kt index 5380af91b..2d51cecd6 100644 --- a/aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/AWSS3StoragePluginTest.kt +++ b/aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/AWSS3StoragePluginTest.kt @@ -22,6 +22,7 @@ import com.amplifyframework.storage.StorageException import com.amplifyframework.storage.s3.service.AWSS3StorageService import com.amplifyframework.testutils.configuration.amplifyOutputsData import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import io.kotest.matchers.throwable.shouldHaveCauseOfType import io.mockk.every @@ -149,4 +150,48 @@ class AWSS3StoragePluginTest { } exception.shouldHaveCauseOfType() } + + @Test + fun `getStorageServiceResult returns result without exception`() { + val data = amplifyOutputsData { + storage { + awsRegion = "test-region" + bucketName = "test-bucket" + buckets { + awsRegion = "test-region" + bucketName = "test-bucket" + name = "test-name" + } + } + } + + plugin.configure(data, mockk()) + val bucket = StorageBucket.fromOutputs("test-name") + val result = plugin.getStorageServiceResult(bucket) + val service = result.storageService + val exception = result.storageException + service shouldNotBe null + exception shouldBe null + } + + @Test + fun `getStorageServiceResult returns result with exception`() { + val data = amplifyOutputsData { + storage { + awsRegion = "test-region" + bucketName = "test-bucket" + buckets { + awsRegion = "test-region" + bucketName = "test-bucket" + name = "test=name" + } + } + } + + plugin.configure(data, mockk()) + val bucket = StorageBucket.fromOutputs("myBucket") + val exception = plugin.getStorageServiceResult(bucket).storageException + exception shouldNotBe null + exception.shouldHaveCauseOfType() + } }