Example usage for com.amazonaws.services.s3.transfer Upload waitForException

List of usage examples for com.amazonaws.services.s3.transfer Upload waitForException

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.transfer Upload waitForException.

Prototype

public AmazonClientException waitForException() throws InterruptedException;

Source Link

Document

Waits for this transfer to finish and returns any error that occurred, or returns null if no errors occurred.

Usage

From source file:it.openutils.mgnlaws.magnolia.datastore.S3DataStore.java

License:Open Source License

/**
 * {@inheritDoc}//from  www.  j  a  v  a2 s. c o  m
 */
public DataRecord addRecord(InputStream input) throws DataStoreException {
    File temporary = null;
    try {
        temporary = newTemporaryFile();
        DataIdentifier tempId = new DataIdentifier(temporary.getName());
        usesIdentifier(tempId);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(temporary), digest);
        try {
            IOUtils.copyLarge(input, output);
        } finally {
            IOUtils.closeQuietly(output);
        }
        DataIdentifier identifier = new DataIdentifier(digest.digest());
        // File file;
        String tmpKey = PREFIX_TMP + identifier.toString();
        String key = getKey(identifier);

        if (!objectExists(identifier)) {
            Upload upload = transferManager.upload(bucket, tmpKey, temporary);
            try {
                AmazonClientException e;
                if ((e = upload.waitForException()) != null && upload.getState() != TransferState.Completed) {
                    throw new DataStoreException("Error uploading file to s3", e);
                }
            } catch (InterruptedException e) {
                throw new DataStoreException("Upload interrupted", e);
            }
        }

        S3DataRecord record;
        synchronized (this) {
            // Check if the same record already exists, or
            // move the temporary file in place if needed
            usesIdentifier(identifier);
            if (!objectExists(identifier)) {
                amazonS3.copyObject(bucket, tmpKey, bucket, key);
                amazonS3.deleteObject(bucket, tmpKey);
            } else {
                // effettua un touch sul file
                touch(key);
            }
            record = new S3DataRecord(identifier, new S3LazyObject(amazonS3, bucket, key));
            if (useCache) {
                cache.put(new Element(identifier.toString(),
                        new CachedS3DataRecord(record, cacheDirectory, temporary)));
                // no need to remove file
                temporary = null;
            }
        }
        // this will also make sure that
        // tempId is not garbage collected until here
        inUse.remove(tempId);
        return record;
    } catch (NoSuchAlgorithmException e) {
        throw new DataStoreException(DIGEST + " not available", e);
    } catch (IOException e) {
        throw new DataStoreException("Could not add record", e);
    } finally {
        if (temporary != null) {
            temporary.delete();
        }
    }
}