Example usage for com.amazonaws.services.s3 AmazonS3Client doesObjectExist

List of usage examples for com.amazonaws.services.s3 AmazonS3Client doesObjectExist

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client doesObjectExist.

Prototype

@Override
    public boolean doesObjectExist(String bucketName, String objectName)
            throws AmazonServiceException, SdkClientException 

Source Link

Usage

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

public boolean exists(String srcKey) {
    final S3Configuration s3config = s3ConfigurationService.lookupS3Configuration();
    final AmazonS3Client s3Client = getAmazonS3Client(s3config);
    final String bucketName = s3config.getDefaultBucketName();

    return s3Client.doesObjectExist(bucketName, srcKey);
}

From source file:org.broadleafcommerce.vendor.amazon.s3.S3FileServiceProvider.java

License:Apache License

private void copyOrMoveObjectImpl(String srcKey, String destKey, boolean move,
        boolean checkAndSucceedIfAlreadyMoved) {
    final S3Configuration s3config = s3ConfigurationService.lookupS3Configuration();
    final AmazonS3Client s3Client = getAmazonS3Client(s3config);
    final String bucketName = s3config.getDefaultBucketName();
    // copy/*from  ww w . ja v a  2s  .  co m*/
    final CopyObjectRequest objToCopy = new CopyObjectRequest(bucketName, srcKey, bucketName, destKey);

    if ((s3config.getStaticAssetFileExtensionPattern() != null)
            && s3config.getStaticAssetFileExtensionPattern().matcher(getExtension(destKey)).matches()) {
        objToCopy.setCannedAccessControlList(CannedAccessControlList.PublicRead);
    }
    try {
        s3Client.copyObject(objToCopy);
    } catch (AmazonS3Exception s3e) {
        if (s3e.getStatusCode() == 404 && checkAndSucceedIfAlreadyMoved) {
            // it's not in the srcKey. Check if something is at the destKey
            if (s3Client.doesObjectExist(bucketName, destKey)) {
                final String msg = String.format("src(%s) doesn't exist but dest(%s) does, so assuming success",
                        srcKey, destKey);
                LOG.warn(msg);
                return;
            } else {
                final String msg = String.format("neither src(%s) or dest(%s) exist", srcKey, destKey);
                throw new RuntimeException(msg);
            }
        }
    } catch (AmazonClientException e) {
        throw new RuntimeException("Unable to copy object from: " + srcKey + " to: " + destKey, e);
    }

    if (move) {
        // delete the old ones in sandbox folder (those with srcKey)
        DeleteObjectRequest objToDelete = new DeleteObjectRequest(bucketName, srcKey);
        try {
            s3Client.deleteObject(objToDelete);
        } catch (AmazonClientException e) {
            //throw new RuntimeException("Moving objects to production folder but unable to delete old object: " + srcKey, e);
            LOG.error("Moving objects to production folder but unable to delete old object: " + srcKey, e);
        }
    }
}