Search in sources :

Example 1 with StorageItem

use of com.amplifyframework.storage.StorageItem in project amplify-android by aws-amplify.

the class AWSS3StorageListAccessLevelTest method assertListContainsUploadedFile.

private void assertListContainsUploadedFile(StorageListResult result) {
    List<StorageItem> items = result.getItems();
    assertNotNull(items);
    assertEquals(1, items.size());
    // Get the first item (there should only be one)
    StorageItem storedItem = items.get(0);
    assertEquals(UPLOAD_SIZE, storedItem.getSize());
    assertEquals(uploadKey, storedItem.getKey());
}
Also used : StorageItem(com.amplifyframework.storage.StorageItem)

Example 2 with StorageItem

use of com.amplifyframework.storage.StorageItem in project amplify-android by aws-amplify.

the class AWSS3StorageService method listFiles.

/**
 * List items inside an S3 path.
 * @param path The path to list items from
 * @param prefix path appended to S3 keys
 * @return A list of parsed items
 */
@NonNull
public List<StorageItem> listFiles(@NonNull String path, @NonNull String prefix) {
    startServiceIfNotAlreadyStarted();
    ArrayList<StorageItem> itemList = new ArrayList<>();
    ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(this.bucket).withPrefix(path);
    ListObjectsV2Result result;
    do {
        result = client.listObjectsV2(request);
        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            // Remove the access level prefix from service key
            String serviceKey = objectSummary.getKey();
            String amplifyKey = S3Keys.extractAmplifyKey(serviceKey, prefix);
            itemList.add(new StorageItem(amplifyKey, objectSummary.getSize(), objectSummary.getLastModified(), objectSummary.getETag(), null));
        }
        // If there are more than maxKeys keys in the bucket, get a continuation token
        // and fetch the next batch of objects.
        String token = result.getNextContinuationToken();
        request.setContinuationToken(token);
    } while (result.isTruncated());
    return itemList;
}
Also used : ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ArrayList(java.util.ArrayList) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) StorageItem(com.amplifyframework.storage.StorageItem) NonNull(androidx.annotation.NonNull)

Example 3 with StorageItem

use of com.amplifyframework.storage.StorageItem in project amplify-android by aws-amplify.

the class StorageComponentTest method testListObject.

/**
 * Test that calling list method from Storage category correctly
 * invokes the registered AWSS3StoragePlugin instance and returns a
 * {@link StorageListResult} with list of stored items.
 *
 * @throws StorageException when an error is encountered while listing
 *                          files inside storage
 */
@Test
public void testListObject() throws StorageException {
    final String path = RandomString.string();
    final StorageItem item = new StorageItem(RandomString.string(), 0L, new Date(), RandomString.string(), null);
    when(storageService.listFiles(anyString(), anyString())).thenReturn(Collections.singletonList(item));
    StorageListResult result = Await.<StorageListResult, StorageException>result((onResult, onError) -> storage.list(path, onResult, onError));
    assertEquals(item, result.getItems().get(0));
}
Also used : StorageListResult(com.amplifyframework.storage.result.StorageListResult) Mockito.anyString(org.mockito.Mockito.anyString) RandomString(com.amplifyframework.testutils.random.RandomString) StorageException(com.amplifyframework.storage.StorageException) Date(java.util.Date) StorageItem(com.amplifyframework.storage.StorageItem) Test(org.junit.Test)

Aggregations

StorageItem (com.amplifyframework.storage.StorageItem)3 NonNull (androidx.annotation.NonNull)1 ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)1 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)1 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)1 StorageException (com.amplifyframework.storage.StorageException)1 StorageListResult (com.amplifyframework.storage.result.StorageListResult)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 Mockito.anyString (org.mockito.Mockito.anyString)1