Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.amplifyframework.auth.cognito

import aws.sdk.kotlin.services.cognitoidentity.CognitoIdentityClient
import aws.sdk.kotlin.services.cognitoidentityprovider.CognitoIdentityProviderClient
import aws.smithy.kotlin.runtime.http.endpoints.Endpoint
import aws.smithy.kotlin.runtime.client.endpoints.Endpoint
import com.amplifyframework.statemachine.codegen.data.AuthConfiguration

interface AWSCognitoAuthService {
Expand All @@ -30,8 +30,8 @@ interface AWSCognitoAuthService {

CognitoIdentityProviderClient {
this.region = it.region
this.endpointResolver = it.endpoint?.let { endpoint ->
AWSEndpointResolver(Endpoint(endpoint))
this.endpointProvider = it.endpoint?.let { endpoint ->
AWSEndpointProvider(Endpoint(endpoint))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
Expand All @@ -12,16 +12,14 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.auth.cognito

import aws.sdk.kotlin.runtime.endpoint.AwsEndpoint
import aws.sdk.kotlin.runtime.endpoint.AwsEndpointResolver
import aws.sdk.kotlin.runtime.endpoint.CredentialScope
import aws.smithy.kotlin.runtime.http.endpoints.Endpoint
import aws.sdk.kotlin.services.cognitoidentityprovider.endpoints.EndpointParameters
import aws.sdk.kotlin.services.cognitoidentityprovider.endpoints.EndpointProvider
import aws.smithy.kotlin.runtime.client.endpoints.Endpoint

internal class AWSEndpointResolver(val endpoint: Endpoint) : AwsEndpointResolver {
override suspend fun resolve(service: String, region: String): AwsEndpoint {
return AwsEndpoint(endpoint.uri, CredentialScope(region, service))
internal class AWSEndpointProvider(val endpoint: Endpoint) : EndpointProvider {
override suspend fun resolveEndpoint(params: EndpointParameters): Endpoint {
return Endpoint(uri = endpoint.uri)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.amplifyframework.storage.operation.StorageDownloadFileOperation;
import com.amplifyframework.storage.options.StorageDownloadFileOptions;
import com.amplifyframework.storage.options.StorageUploadFileOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageDownloadFileOptions;
import com.amplifyframework.storage.s3.test.R;
import com.amplifyframework.storage.s3.util.WorkmanagerTestUtils;
import com.amplifyframework.testutils.FileAssert;
Expand All @@ -52,6 +53,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -96,8 +98,8 @@ public static void setUpOnce() throws Exception {
// Upload to PUBLIC for consistency
String key;
StorageUploadFileOptions uploadOptions = StorageUploadFileOptions.builder()
.accessLevel(TESTING_ACCESS_LEVEL)
.build();
.accessLevel(TESTING_ACCESS_LEVEL)
.build();

// Upload large test file
largeFile = new RandomTempFile(LARGE_FILE_NAME, LARGE_FILE_SIZE);
Expand All @@ -119,8 +121,8 @@ public static void setUpOnce() throws Exception {
public void setUp() throws Exception {
// Always interact with PUBLIC access for consistency
options = StorageDownloadFileOptions.builder()
.accessLevel(TESTING_ACCESS_LEVEL)
.build();
.accessLevel(TESTING_ACCESS_LEVEL)
.build();

// Create a set to remember all the subscriptions
subscriptions = new HashSet<>();
Expand Down Expand Up @@ -273,7 +275,7 @@ public void testGetTransferOnPause() throws Exception {
final AtomicReference<StorageDownloadFileOperation<?>> opContainer = new AtomicReference<>();
final AtomicReference<String> transferId = new AtomicReference<>();
final AtomicReference<Throwable> errorContainer = new AtomicReference<>();
// Listen to Hub events to resume when operation has been paused
// Listen to Hub events to resume when operation has been paused
SubscriptionToken resumeToken = Amplify.Hub.subscribe(HubChannel.STORAGE, hubEvent -> {
if (StorageChannelEventName.DOWNLOAD_STATE.toString().equals(hubEvent.getName())) {
HubEvent<String> stateEvent = (HubEvent<String>) hubEvent;
Expand Down Expand Up @@ -317,4 +319,16 @@ public void testGetTransferOnPause() throws Exception {
assertNull(errorContainer.get());
FileAssert.assertEquals(largeFile, downloadFile);
}

/**
* Tests download fails due to acceleration mode disabled.
*
* @throws Exception download fails because acceleration is not enabled on test bucket.
*/
@Test
public void testDownloadLargeFileWithAccelerationEnabled() throws Exception {
AWSS3StorageDownloadFileOptions awsS3Options =
AWSS3StorageDownloadFileOptions.builder().setUseAccelerateEndpoint(true).build();
synchronousStorage.downloadFile(LARGE_FILE_NAME, downloadFile, awsS3Options, EXTENDED_TIMEOUT_MS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
import com.amplifyframework.storage.StorageAccessLevel;
import com.amplifyframework.storage.StorageCategory;
import com.amplifyframework.storage.StorageChannelEventName;
import com.amplifyframework.storage.StorageException;
import com.amplifyframework.storage.TransferState;
import com.amplifyframework.storage.operation.StorageUploadFileOperation;
import com.amplifyframework.storage.operation.StorageUploadInputStreamOperation;
import com.amplifyframework.storage.options.StorageUploadFileOptions;
import com.amplifyframework.storage.options.StorageUploadInputStreamOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageUploadFileOptions;
import com.amplifyframework.storage.s3.test.R;
import com.amplifyframework.storage.s3.util.WorkmanagerTestUtils;
import com.amplifyframework.testutils.random.RandomTempFile;
Expand All @@ -53,6 +55,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -74,6 +77,7 @@ public final class AWSS3StorageUploadTest {

/**
* Initialize mobile client and configure the storage.
*
* @throws Exception if mobile client initialization fails
*/
@BeforeClass
Expand All @@ -95,8 +99,8 @@ public static void setUpOnce() throws Exception {
public void setUp() {
// Always interact with PUBLIC access for consistency
options = StorageUploadFileOptions.builder()
.accessLevel(TESTING_ACCESS_LEVEL)
.build();
.accessLevel(TESTING_ACCESS_LEVEL)
.build();

// Create a set to remember all the subscriptions
subscriptions = new HashSet<>();
Expand Down Expand Up @@ -132,7 +136,7 @@ public void testUploadSmallFile() throws Exception {
*/
@Test
public void testUploadSmallFileStream() throws Exception {
File uploadFile = new RandomTempFile(4 * 1024 * 1024);
File uploadFile = new RandomTempFile(SMALL_FILE_SIZE);
String fileName = uploadFile.getName();
StorageUploadInputStreamOptions options = StorageUploadInputStreamOptions.builder()
.accessLevel(TESTING_ACCESS_LEVEL)
Expand All @@ -149,6 +153,8 @@ public void testUploadSmallFileStream() throws Exception {
public void testUploadLargeFile() throws Exception {
File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
String fileName = uploadFile.getName();
AWSS3StorageUploadFileOptions options =
AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();
synchronousStorage.uploadFile(fileName, uploadFile, options, EXTENDED_TIMEOUT_MS);
}

Expand All @@ -157,7 +163,7 @@ public void testUploadLargeFile() throws Exception {
* transfer hasn't completed yet.
*
* @throws Exception if upload is not canceled successfully
* before timeout
* before timeout
*/
@SuppressWarnings("unchecked")
@Test
Expand Down Expand Up @@ -206,7 +212,7 @@ public void testUploadFileIsCancelable() throws Exception {
* while the transfer hasn't completed yet.
*
* @throws Exception if upload is not paused, resumed, and
* completed successfully before timeout
* completed successfully before timeout
*/
@SuppressWarnings("unchecked")
@Test
Expand Down Expand Up @@ -258,7 +264,7 @@ public void testUploadFileIsResumable() throws Exception {
* using getTransfer API.
*
* @throws Exception if upload is not paused, resumed, and
* completed successfully before timeout
* completed successfully before timeout
*/
@SuppressWarnings("unchecked")
@Test
Expand Down Expand Up @@ -303,7 +309,8 @@ public void testUploadFileGetTransferOnPause() throws Exception {
opContainer.get().pause();
}
},
result -> { },
result -> {
},
errorContainer::set
);
opContainer.set(op);
Expand All @@ -320,7 +327,7 @@ public void testUploadFileGetTransferOnPause() throws Exception {
* using getTransfer API.
*
* @throws Exception if upload is not paused, resumed, and
* completed successfully before timeout
* completed successfully before timeout
*/
@SuppressWarnings("unchecked")
@Test
Expand Down Expand Up @@ -367,7 +374,8 @@ public void testUploadInputStreamGetTransferOnPause() throws Exception {
opContainer.get().pause();
}
},
result -> { },
result -> {
},
errorContainer::set
);
opContainer.set(op);
Expand All @@ -378,4 +386,35 @@ public void testUploadInputStreamGetTransferOnPause() throws Exception {
assertTrue(completed.await(EXTENDED_TIMEOUT_MS, TimeUnit.MILLISECONDS));
assertNull(errorContainer.get());
}

/**
* Tests that small file (single-part) uploads successfully.
*
* @throws Exception if upload fails
*/
@Test
public void testUploadSmallFileWithAccelerationEnabled() throws Exception {
File uploadFile = new RandomTempFile(SMALL_FILE_SIZE);
String fileName = uploadFile.getName();
AWSS3StorageUploadFileOptions awss3StorageUploadFileOptions =
AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();
synchronousStorage.uploadFile(fileName, uploadFile,
awss3StorageUploadFileOptions);
}

/**
* Tests that large file (single-part) uploads successfully.
*
* @throws Exception if upload fails
*/
@Test
public void testUploadLargeFileWithAccelerationEnabled() throws Exception {
File uploadFile = new RandomTempFile(LARGE_FILE_SIZE);
String fileName = uploadFile.getName();
AWSS3StorageUploadFileOptions awss3StorageUploadFileOptions =
AWSS3StorageUploadFileOptions.builder().setUseAccelerateEndpoint(true).build();
synchronousStorage.uploadFile(fileName, uploadFile,
awss3StorageUploadFileOptions);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ open class TransferDBTest {
ongoingRestore = false,
restoreExpirationTime = restoreExpirationTime
),
null
null,
useAccelerateEndpoint = false
)
val uri = transferDB.bulkInsertTransferRecords(contentValues)
transferDB.getTransferRecordById(uri).run {
Expand All @@ -144,7 +145,8 @@ open class TransferDBTest {
1L,
0,
null,
null
null,
false
)
contentValues[1] = transferDB.generateContentValuesForMultiPartUpload(
key,
Expand All @@ -157,7 +159,8 @@ open class TransferDBTest {
1L,
0,
null,
null
null,
false
)
contentValues[2] = transferDB.generateContentValuesForMultiPartUpload(
key,
Expand All @@ -170,7 +173,8 @@ open class TransferDBTest {
1L,
1,
null,
null
null,
false
)
val bulkInsertUri = transferDB.bulkInsertTransferRecords(contentValues)
transferDB.getTransferRecordById(bulkInsertUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import com.amplifyframework.storage.s3.operation.AWSS3StorageRemoveOperation;
import com.amplifyframework.storage.s3.operation.AWSS3StorageUploadFileOperation;
import com.amplifyframework.storage.s3.operation.AWSS3StorageUploadInputStreamOperation;
import com.amplifyframework.storage.s3.options.AWSS3StorageDownloadFileOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageGetPresignedUrlOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageUploadFileOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageUploadInputStreamOptions;
import com.amplifyframework.storage.s3.request.AWSS3StorageDownloadFileRequest;
Expand Down Expand Up @@ -254,6 +256,8 @@ public StorageGetUrlOperation<?> getUrl(
@NonNull StorageGetUrlOptions options,
@NonNull Consumer<StorageGetUrlResult> onSuccess,
@NonNull Consumer<StorageException> onError) {
boolean useAccelerateEndpoint = options instanceof AWSS3StorageGetPresignedUrlOptions &&
((AWSS3StorageGetPresignedUrlOptions) options).useAccelerateEndpoint();
AWSS3StorageGetPresignedUrlRequest request = new AWSS3StorageGetPresignedUrlRequest(
key,
options.getAccessLevel() != null
Expand All @@ -262,7 +266,8 @@ public StorageGetUrlOperation<?> getUrl(
options.getTargetIdentityId(),
options.getExpires() != 0
? options.getExpires()
: defaultUrlExpiration
: defaultUrlExpiration,
useAccelerateEndpoint
);

AWSS3StorageGetPresignedUrlOperation operation =
Expand Down Expand Up @@ -313,13 +318,17 @@ public StorageDownloadFileOperation<?> downloadFile(
@NonNull Consumer<StorageDownloadFileResult> onSuccess,
@NonNull Consumer<StorageException> onError
) {
boolean useAccelerateEndpoint =
options instanceof AWSS3StorageDownloadFileOptions &&
((AWSS3StorageDownloadFileOptions) options).useAccelerateEndpoint();
AWSS3StorageDownloadFileRequest request = new AWSS3StorageDownloadFileRequest(
key,
local,
options.getAccessLevel() != null
? options.getAccessLevel()
: defaultAccessLevel,
options.getTargetIdentityId()
options.getTargetIdentityId(),
useAccelerateEndpoint
);

AWSS3StorageDownloadFileOperation operation = new AWSS3StorageDownloadFileOperation(
Expand Down Expand Up @@ -371,6 +380,8 @@ public StorageUploadFileOperation<?> uploadFile(
@NonNull Consumer<StorageUploadFileResult> onSuccess,
@NonNull Consumer<StorageException> onError
) {
boolean useAccelerateEndpoint = options instanceof AWSS3StorageUploadFileOptions &&
((AWSS3StorageUploadFileOptions) options).useAccelerateEndpoint();
AWSS3StorageUploadRequest<File> request = new AWSS3StorageUploadRequest<>(
key,
local,
Expand All @@ -382,7 +393,8 @@ public StorageUploadFileOperation<?> uploadFile(
options instanceof AWSS3StorageUploadFileOptions
? ((AWSS3StorageUploadFileOptions) options).getServerSideEncryption()
: ServerSideEncryption.NONE,
options.getMetadata()
options.getMetadata(),
useAccelerateEndpoint
);

AWSS3StorageUploadFileOperation operation = new AWSS3StorageUploadFileOperation(
Expand Down Expand Up @@ -434,6 +446,8 @@ public StorageUploadInputStreamOperation<?> uploadInputStream(
@NonNull Consumer<StorageUploadInputStreamResult> onSuccess,
@NonNull Consumer<StorageException> onError
) {
boolean useAccelerateEndpoint = options instanceof AWSS3StorageUploadInputStreamOptions &&
((AWSS3StorageUploadInputStreamOptions) options).useAccelerateEndpoint();
AWSS3StorageUploadRequest<InputStream> request = new AWSS3StorageUploadRequest<>(
key,
local,
Expand All @@ -445,7 +459,8 @@ public StorageUploadInputStreamOperation<?> uploadInputStream(
options instanceof AWSS3StorageUploadInputStreamOptions
? ((AWSS3StorageUploadInputStreamOptions) options).getServerSideEncryption()
: ServerSideEncryption.NONE,
options.getMetadata()
options.getMetadata(),
useAccelerateEndpoint
);

AWSS3StorageUploadInputStreamOperation operation = new AWSS3StorageUploadInputStreamOperation(
Expand Down
Loading