use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class AWSS3StorageUploadFileOperation method start.
@SuppressLint("SyntheticAccessor")
@Override
public void start() {
// Only start if it hasn't already been started
if (transferObserver != null) {
return;
}
// Grab the file to upload...
File file = 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());
}
// Upload!
awsS3StoragePluginConfiguration.getAWSS3PluginPrefixResolver(cognitoAuthProvider).resolvePrefix(getRequest().getAccessLevel(), getRequest().getTargetIdentityId(), prefix -> {
try {
String serviceKey = prefix.concat(getRequest().getKey());
transferObserver = storageService.uploadFile(serviceKey, file, objectMetadata);
transferObserver.setTransferListener(new UploadTransferListener());
} catch (Exception exception) {
onError.accept(new StorageException("Issue uploading file.", exception, "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 testUploadFileGetsKey.
/**
* Test that calling upload file 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 testUploadFileGetsKey() throws Exception {
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.onStateChanged(0, TransferState.COMPLETED);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageUploadFileResult result = Await.<StorageUploadFileResult, StorageException>result((onResult, onError) -> storage.uploadFile(toRemoteKey, fromLocalFile, onResult, onError));
assertEquals(toRemoteKey, result.getKey());
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testRemoveObjectGetsKey.
/**
* Test that calling remove method from Storage category correctly
* invokes the registered AWSS3StoragePlugin instance and returns a
* {@link StorageRemoveResult} with key of removed item.
*
* @throws StorageException when an error is encountered while deleting
* file from storage
*/
@Test
public void testRemoveObjectGetsKey() throws StorageException {
final String remoteKey = RandomString.string();
StorageRemoveResult result = Await.<StorageRemoveResult, StorageException>result((onResult, onError) -> storage.remove(remoteKey, onResult, onError));
assertEquals(remoteKey, result.getKey());
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testGenerateUrlGetsPresignedUrl.
/**
* Test that calling get URL method from Storage category correctly invokes
* the registered AWSS3StoragePlugin instance and returns a {@link URL}
* instance for that download.
*
* @throws StorageException when an error is encountered while generating
* URL from storage service
*/
@Test
public void testGenerateUrlGetsPresignedUrl() throws StorageException {
final String fromRemoteKey = RandomString.string();
final URL urlFromRemoteKey;
try {
// URL instance cannot be mocked so just make one
// https://{random-host}:0/{fromRemoteKey}
urlFromRemoteKey = new URL("https", RandomString.string(), 0, fromRemoteKey, null);
} catch (MalformedURLException exception) {
throw new RuntimeException(exception);
}
// Allow mock StorageService instance to return a non-null
// URL instance.
when(storageService.getPresignedUrl(anyString(), anyInt())).thenReturn(urlFromRemoteKey);
// Let Storage category invoke getUrl on mock Storage Service.
StorageGetUrlResult result = Await.<StorageGetUrlResult, StorageException>result((onResult, onError) -> storage.getUrl(fromRemoteKey, onResult, onError));
assertEquals(urlFromRemoteKey, result.getUrl());
}
use of com.amplifyframework.storage.StorageException in project amplify-android by aws-amplify.
the class StorageComponentTest method testInputStreamError.
/**
* Test that calling upload inputStream method from Storage category fails
* successfully when {@link TransferListener} emits an error.
*
* @throws IOException when the upload file cannot be created
*/
@Test
public void testInputStreamError() throws IOException {
final StorageException testError = new StorageException("Test error message", "Test recovery message");
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.onError(0, testError);
return null;
}).when(observer).setTransferListener(any(TransferListener.class));
StorageException error = Await.<StorageUploadInputStreamResult, StorageException>error((onResult, onError) -> storage.uploadInputStream(toRemoteKey, inputStream, onResult, onError));
assertEquals(testError, error.getCause());
}
Aggregations