Search in sources :

Example 11 with CopyObjectRequest

use of com.amazonaws.services.s3.model.CopyObjectRequest in project jackrabbit by apache.

the class S3RequestDecorator method decorate.

/**
     * Set encryption in {@link CopyObjectRequest}
     */
public CopyObjectRequest decorate(CopyObjectRequest request) {
    switch(getDataEncryption()) {
        case SSE_S3:
            ObjectMetadata metadata = request.getNewObjectMetadata() == null ? new ObjectMetadata() : request.getNewObjectMetadata();
            metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            request.setNewObjectMetadata(metadata);
            break;
        case NONE:
            break;
    }
    return request;
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 12 with CopyObjectRequest

use of com.amazonaws.services.s3.model.CopyObjectRequest in project jackrabbit by apache.

the class S3Backend method touch.

@Override
public void touch(DataIdentifier identifier, long minModifiedDate) throws DataStoreException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        final long start = System.currentTimeMillis();
        final String key = getKeyName(identifier);
        if (minModifiedDate > 0 && minModifiedDate > getLastModified(identifier)) {
            CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
            copReq.setNewObjectMetadata(new ObjectMetadata());
            Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
            copy.waitForCompletion();
            LOG.debug("[{}] touched. time taken [{}] ms ", new Object[] { identifier, (System.currentTimeMillis() - start) });
        } else {
            LOG.trace("[{}] touch not required. time taken [{}] ms ", new Object[] { identifier, (System.currentTimeMillis() - start) });
        }
    } catch (Exception e) {
        throw new DataStoreException("Error occured in touching key [" + identifier.toString() + "]", e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Copy(com.amazonaws.services.s3.transfer.Copy) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 13 with CopyObjectRequest

use of com.amazonaws.services.s3.model.CopyObjectRequest in project jackrabbit by apache.

the class S3Backend method exists.

@Override
public boolean exists(DataIdentifier identifier, boolean touch) throws DataStoreException {
    long start = System.currentTimeMillis();
    String key = getKeyName(identifier);
    ObjectMetadata objectMetaData = null;
    boolean retVal = false;
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        objectMetaData = s3service.getObjectMetadata(bucket, key);
        if (objectMetaData != null) {
            retVal = true;
            if (touch) {
                CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
                copReq.setNewObjectMetadata(objectMetaData);
                Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
                copy.waitForCopyResult();
                LOG.debug("[{}] touched took [{}] ms. ", identifier, (System.currentTimeMillis() - start));
            }
        } else {
            retVal = false;
        }
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() == 404 || e.getStatusCode() == 403) {
            retVal = false;
        } else {
            throw new DataStoreException("Error occured to find exists for key [" + identifier.toString() + "]", e);
        }
    } catch (Exception e) {
        throw new DataStoreException("Error occured to find exists for key  " + identifier.toString(), e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.debug("exists [{}]: [{}] took [{}] ms.", new Object[] { identifier, retVal, (System.currentTimeMillis() - start) });
    return retVal;
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) AmazonServiceException(com.amazonaws.AmazonServiceException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 14 with CopyObjectRequest

use of com.amazonaws.services.s3.model.CopyObjectRequest in project jackrabbit-oak by apache.

the class S3Backend method write.

/**
     * It uploads file to Amazon S3. If file size is greater than 5MB, this
     * method uses parallel concurrent connections to upload.
     */
@Override
public void write(DataIdentifier identifier, File file) throws DataStoreException {
    String key = getKeyName(identifier);
    ObjectMetadata objectMetaData = null;
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        // check if the same record already exists
        try {
            objectMetaData = s3service.getObjectMetadata(bucket, key);
        } catch (AmazonServiceException ase) {
            if (!(ase.getStatusCode() == 404 || ase.getStatusCode() == 403)) {
                throw ase;
            }
        }
        if (objectMetaData != null) {
            long l = objectMetaData.getContentLength();
            if (l != file.length()) {
                throw new DataStoreException("Collision: " + key + " new length: " + file.length() + " old length: " + l);
            }
            LOG.debug("[{}]'s exists, lastmodified = [{}]", key, objectMetaData.getLastModified().getTime());
            CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
            copReq.setNewObjectMetadata(objectMetaData);
            Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
            try {
                copy.waitForCopyResult();
                LOG.debug("lastModified of [{}] updated successfully.", identifier);
            } catch (Exception e2) {
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
        if (objectMetaData == null) {
            try {
                // start multipart parallel upload using amazon sdk
                Upload up = tmx.upload(s3ReqDecorator.decorate(new PutObjectRequest(bucket, key, file)));
                // wait for upload to finish
                up.waitForUploadResult();
                LOG.debug("synchronous upload to identifier [{}] completed.", identifier);
            } catch (Exception e2) {
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.debug("write of [{}], length=[{}], in [{}]ms", identifier, file.length(), (System.currentTimeMillis() - start));
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) Upload(com.amazonaws.services.s3.transfer.Upload) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) AmazonServiceException(com.amazonaws.AmazonServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AmazonClientException(com.amazonaws.AmazonClientException) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 15 with CopyObjectRequest

use of com.amazonaws.services.s3.model.CopyObjectRequest in project jackrabbit-oak by apache.

the class S3RequestDecorator method decorate.

/**
     * Set encryption in {@link CopyObjectRequest}
     */
public CopyObjectRequest decorate(CopyObjectRequest request) {
    switch(getDataEncryption()) {
        case SSE_S3:
            ObjectMetadata metadata = request.getNewObjectMetadata() == null ? new ObjectMetadata() : request.getNewObjectMetadata();
            metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            request.setNewObjectMetadata(metadata);
            break;
        case NONE:
            break;
    }
    return request;
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)12 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)11 AmazonClientException (com.amazonaws.AmazonClientException)9 Copy (com.amazonaws.services.s3.transfer.Copy)8 AmazonServiceException (com.amazonaws.AmazonServiceException)7 IOException (java.io.IOException)7 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)7 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)3 Upload (com.amazonaws.services.s3.transfer.Upload)3 CopyObjectResult (com.amazonaws.services.s3.model.CopyObjectResult)2 AsyncUploadResult (org.apache.jackrabbit.core.data.AsyncUploadResult)2 ProgressEvent (com.amazonaws.event.ProgressEvent)1 ProgressListener (com.amazonaws.event.ProgressListener)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 SSECustomerKey (com.amazonaws.services.s3.model.SSECustomerKey)1 InterruptedIOException (java.io.InterruptedIOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Message (org.apache.camel.Message)1