use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class AWSS3StorageUploadInputStreamOperation method start.
@SuppressLint("SyntheticAccessor")
@Override
public void start() {
// Only start if it hasn't already been started
if (transferObserver != null) {
return;
}
// Grab the inputStream to upload...
InputStream inputStream = getRequest().getLocal();
// Set up the metadata
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setUserMetadata(getRequest().getMetadata());
objectMetadata.setContentType(getRequest().getContentType());
ServerSideEncryption storageServerSideEncryption = getRequest().getServerSideEncryption();
if (!ServerSideEncryption.NONE.equals(storageServerSideEncryption)) {
objectMetadata.setSSEAlgorithm(storageServerSideEncryption.getName());
}
awsS3StoragePluginConfiguration.getAWSS3PluginPrefixResolver(cognitoAuthProvider).resolvePrefix(getRequest().getAccessLevel(), getRequest().getTargetIdentityId(), prefix -> {
try {
String serviceKey = prefix.concat(getRequest().getKey());
transferObserver = storageService.uploadInputStream(serviceKey, inputStream, objectMetadata);
transferObserver.setTransferListener(new UploadTransferListener());
} catch (IOException ioException) {
onError.accept(new StorageException("Issue uploading inputStream.", ioException, "See included exception for more details and suggestions to fix."));
}
}, onError);
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testDownloadToFileGetsFile.
/**
* Test that calling download file method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageDownloadFileResult} with correct file path.
*
* @throws Exception when an error is encountered while downloading
*/
@Test
public void testDownloadToFileGetsFile() throws Exception {
final String fromRemoteKey = RandomString.string();
final File toLocalFile = new RandomTempFile();
// Since we use a mock StorageService, it will return a null
// result by default. We need a non-null transfer observer.
// One option is to mock that, too.
TransferObserver observer = mock(TransferObserver.class);
when(storageService.downloadToFile(anyString(), any(File.class))).thenReturn(observer);
// Since we use a mock TransferObserver, it has no internal logic
// to know to call back the listener! So, we simulate the success
// callback, as part of our "happy path" test.
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageDownloadFileResult result = Await.<StorageDownloadFileResult, StorageException>result((onResult, onError) -> storage.downloadFile(fromRemoteKey, toLocalFile, onResult, onError));
assertEquals(toLocalFile.getAbsolutePath(), result.getFile().toString());
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadFileError.
/**
* Test that calling upload file method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the upload file cannot be created
*/
@Test
public void testUploadFileError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
final String toRemoteKey = RandomString.string();
final File fromLocalFile = new RandomTempFile(FILE_SIZE);
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadFile(anyString(), any(File.class), any(ObjectMetadata.class))).thenReturn(observer);
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onError(0, testError);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageException error = Await.<StorageUploadFileResult, StorageException>error((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(testError, error.getCause());
}
use of com.amplifyframework.storage.StorageException 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));
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testUploadInputStreamGetsKey.
/**
* Test that calling upload inputStream method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageUploadFileResult} with correct remote key.
*
* @throws Exception when an error is encountered while uploading
*/
@Test
public void testUploadInputStreamGetsKey() throws Exception {
final String toRemoteKey = RandomString.string();
final InputStream inputStream = new ByteArrayInputStream(RandomBytes.bytes());
TransferObserver observer = mock(TransferObserver.class);
when(storageService.uploadInputStream(anyString(), any(InputStream.class), any(ObjectMetadata.class))).thenReturn(observer);
doAnswer(invocation -> {
TransferListener listener = invocation.getArgument(0);
listener.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageUploadInputStreamResult result = Await.<StorageUploadInputStreamResult, StorageException>result((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
assertEquals(toRemoteKey, result.getKey());
}
Aggregations