Skip to content

Commit

Permalink
feat(storage): implement multiple buckets support (#2904)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode committed Sep 17, 2024
1 parent b4cda6c commit 639ef96
Show file tree
Hide file tree
Showing 64 changed files with 2,230 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.amplifyframework.storage.StorageCategory;
import com.amplifyframework.storage.StorageException;
import com.amplifyframework.storage.options.StorageDownloadFileOptions;
import com.amplifyframework.storage.options.StorageRemoveOptions;
import com.amplifyframework.storage.options.StorageUploadFileOptions;
import com.amplifyframework.storage.s3.UserCredentials.Credential;
import com.amplifyframework.storage.s3.UserCredentials.IdentityIdSource;
Expand All @@ -33,6 +34,7 @@
import com.amplifyframework.testutils.sync.SynchronousAuth;
import com.amplifyframework.testutils.sync.SynchronousStorage;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -85,6 +87,15 @@ public static void setUpOnce() throws Exception {
uploadTestFile();
}

/**
* Clean up test resources from test suite.
* @throws Exception from failure to remove test resources.
*/
@AfterClass
public static void tearDownOnce() throws Exception {
removeUploadedTestFiles();
}

/**
* Signs out by default and sets up download file destination.
*
Expand Down Expand Up @@ -263,4 +274,39 @@ private static void uploadTestFile() throws Exception {
.build();
storage.uploadFile(key, uploadFile, options);
}

private static void removeUploadedTestFiles() throws Exception {
final String key = UPLOAD_NAME;

synchronousAuth.signOut();
synchronousAuth.signIn(userOne.getUsername(), userOne.getPassword());

StorageRemoveOptions options;
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PUBLIC)
.build();
storage.remove(key, options);

options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PROTECTED)
.build();
storage.remove(key, options);

options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PRIVATE)
.build();
storage.remove(key, options);

// Upload as user two
synchronousAuth.signOut();
synchronousAuth.signIn(userTwo.getUsername(), userTwo.getPassword());
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PROTECTED)
.build();
storage.remove(key, options);
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PRIVATE)
.build();
storage.remove(key, options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import android.content.Context;

import com.amplifyframework.auth.AuthPlugin;
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.async.Cancelable;
Expand All @@ -31,6 +30,7 @@
import com.amplifyframework.storage.TransferState;
import com.amplifyframework.storage.operation.StorageDownloadFileOperation;
import com.amplifyframework.storage.options.StorageDownloadFileOptions;
import com.amplifyframework.storage.options.StorageRemoveOptions;
import com.amplifyframework.storage.options.StorageUploadFileOptions;
import com.amplifyframework.storage.s3.options.AWSS3StorageDownloadFileOptions;
import com.amplifyframework.storage.s3.test.R;
Expand All @@ -41,6 +41,7 @@
import com.amplifyframework.testutils.sync.SynchronousStorage;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -89,7 +90,7 @@ public final class AWSS3StorageDownloadTest {
public static void setUpOnce() throws Exception {
Context context = getApplicationContext();
WorkmanagerTestUtils.INSTANCE.initializeWorkmanagerTestUtil(context);
SynchronousAuth.delegatingToCognito(context, (AuthPlugin) new AWSCognitoAuthPlugin());
SynchronousAuth.delegatingToCognito(context, new AWSCognitoAuthPlugin());

// Get a handle to storage
storageCategory = TestStorageCategory.create(context, R.raw.amplifyconfiguration);
Expand All @@ -112,6 +113,21 @@ public static void setUpOnce() throws Exception {
synchronousStorage.uploadFile(key, smallFile, uploadOptions);
}

/**
* Clean up test resources from test suite.
* @throws Exception from failure to remove test resources.
*/
@AfterClass
public static void tearDownOnce() throws Exception {
StorageRemoveOptions options = StorageRemoveOptions
.builder()
.accessLevel(TESTING_ACCESS_LEVEL)
.build();

synchronousStorage.remove(SMALL_FILE_NAME, options);
synchronousStorage.remove(LARGE_FILE_NAME, options);
}

/**
* Sets up the options to use for transfer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import android.content.Context;

import com.amplifyframework.auth.AuthException;
import com.amplifyframework.auth.AuthPlugin;
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
import com.amplifyframework.storage.StorageAccessLevel;
Expand All @@ -25,6 +26,7 @@
import com.amplifyframework.storage.StorageItem;
import com.amplifyframework.storage.options.StorageListOptions;
import com.amplifyframework.storage.options.StoragePagedListOptions;
import com.amplifyframework.storage.options.StorageRemoveOptions;
import com.amplifyframework.storage.options.StorageUploadFileOptions;
import com.amplifyframework.storage.result.StorageListResult;
import com.amplifyframework.storage.s3.UserCredentials.IdentityIdSource;
Expand All @@ -35,6 +37,7 @@
import com.amplifyframework.testutils.sync.SynchronousAuth;
import com.amplifyframework.testutils.sync.SynchronousStorage;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -91,6 +94,16 @@ public static void setUpOnce() throws Exception {
uploadTestFiles();
}

/**
* Remove upload test files from test suite.
*
* @throws Exception from failure to remove test resources.
*/
@AfterClass
public static void tearDownOnce() throws Exception {
removeUploadedTestFiles();
}

/**
* Signs out by default.
*
Expand Down Expand Up @@ -326,4 +339,44 @@ private static void uploadMultipleTestFiles() throws Exception {
// Upload as user one
synchronousAuth.signOut();
}

private static void removeUploadedTestFiles() throws AuthException, StorageException {
// remove PUBLIC test files
synchronousAuth.signOut();
synchronousAuth.signIn(userOne.getUsername(), userOne.getPassword());
StorageRemoveOptions options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PUBLIC)
.build();
for (int i = 0; i < 10; i++) {
storage.remove(pagedUploadKeyPrefix + i, options);
}
storage.remove(uploadKey, options);

// remove PROTECTED test files
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PROTECTED)
.build();
storage.remove(uploadKey, options);

// remove PRIVATE test files
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PRIVATE)
.build();
storage.remove(uploadKey, options);

synchronousAuth.signOut();
synchronousAuth.signIn(userTwo.getUsername(), userTwo.getPassword());
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PROTECTED)
.build();
storage.remove(uploadKey, options);

// remove PRIVATE test files
options = StorageRemoveOptions.builder()
.accessLevel(StorageAccessLevel.PRIVATE)
.build();
storage.remove(uploadKey, options);

synchronousAuth.signOut();
}
}
Loading

0 comments on commit 639ef96

Please sign in to comment.