Search in sources :

Example 81 with AmazonClientException

use of com.amazonaws.AmazonClientException in project xm-ms-entity by xm-online.

the class AmazonS3Template method save.

/**
 * Save a file using authenticated session credentials.
 *
 * @param key is the name of the file to save in the bucket
 * @param inputStream is the file that will be saved
 */
public void save(String key, InputStream inputStream) throws IOException {
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType(URLConnection.guessContentTypeFromStream(inputStream));
    PutObjectRequest request = new PutObjectRequest(bucket, key, inputStream, metadata);
    request.setCannedAcl(CannedAccessControlList.PublicRead);
    request.getRequestClientOptions().setReadLimit(Integer.MAX_VALUE);
    Upload upload = getTransferManager().upload(request);
    try {
        upload.waitForUploadResult();
    } catch (AmazonClientException ex) {
        throw new IOException(ex);
    } catch (InterruptedException ex) {
        // reset interrupted status
        Thread.currentThread().interrupt();
        // continue interrupt
        throw new IllegalStateException(ex);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) Upload(com.amazonaws.services.s3.transfer.Upload) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 82 with AmazonClientException

use of com.amazonaws.AmazonClientException in project alluxio by Alluxio.

the class S3AUnderFileSystem method copyObject.

@Override
protected boolean copyObject(String src, String dst) {
    LOG.debug("Copying {} to {}", src, dst);
    // Retry copy for a few times, in case some AWS internal errors happened during copy.
    int retries = 3;
    for (int i = 0; i < retries; i++) {
        try {
            CopyObjectRequest request = new CopyObjectRequest(mBucketName, src, mBucketName, dst);
            if (mUfsConf.getBoolean(PropertyKey.UNDERFS_S3_SERVER_SIDE_ENCRYPTION_ENABLED)) {
                ObjectMetadata meta = new ObjectMetadata();
                meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
                request.setNewObjectMetadata(meta);
            }
            mManager.copy(request).waitForCopyResult();
            return true;
        } catch (AmazonClientException | InterruptedException e) {
            LOG.error("Failed to copy file {} to {}", src, dst, e);
            if (i != retries - 1) {
                LOG.error("Retrying copying file {} to {}", src, dst);
            }
        }
    }
    LOG.error("Failed to copy file {} to {}, after {} retries", src, dst, retries);
    return false;
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 83 with AmazonClientException

use of com.amazonaws.AmazonClientException in project alluxio by Alluxio.

the class S3AUnderFileSystem method getObjectListingChunkV1.

// Get next chunk of listing result.
private ObjectListing getObjectListingChunkV1(ListObjectsRequest request) throws IOException {
    ObjectListing result;
    try {
        // Query S3 for the next batch of objects.
        result = mClient.listObjects(request);
        // Advance the request continuation token to the next set of objects.
        request.setMarker(result.getNextMarker());
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
    return result;
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) IOException(java.io.IOException)

Example 84 with AmazonClientException

use of com.amazonaws.AmazonClientException in project alluxio by Alluxio.

the class S3AUnderFileSystem method deleteObjects.

@Override
protected List<String> deleteObjects(List<String> keys) throws IOException {
    if (!mUfsConf.getBoolean(PropertyKey.UNDERFS_S3_BULK_DELETE_ENABLED)) {
        return super.deleteObjects(keys);
    }
    Preconditions.checkArgument(keys != null && keys.size() <= getListingChunkLengthMax());
    try {
        List<DeleteObjectsRequest.KeyVersion> keysToDelete = new ArrayList<>();
        for (String key : keys) {
            keysToDelete.add(new DeleteObjectsRequest.KeyVersion(key));
        }
        DeleteObjectsResult deletedObjectsResult = mClient.deleteObjects(new DeleteObjectsRequest(mBucketName).withKeys(keysToDelete));
        List<String> deletedObjects = new ArrayList<>();
        for (DeleteObjectsResult.DeletedObject deletedObject : deletedObjectsResult.getDeletedObjects()) {
            deletedObjects.add(deletedObject.getKey());
        }
        return deletedObjects;
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) IOException(java.io.IOException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 85 with AmazonClientException

use of com.amazonaws.AmazonClientException in project alluxio by Alluxio.

the class S3AUnderFileSystem method createEmptyObject.

@Override
public boolean createEmptyObject(String key) {
    try {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(0);
        meta.setContentMD5(DIR_HASH);
        meta.setContentType(Mimetypes.MIMETYPE_OCTET_STREAM);
        mClient.putObject(new PutObjectRequest(mBucketName, key, new ByteArrayInputStream(new byte[0]), meta));
        return true;
    } catch (AmazonClientException e) {
        LOG.error("Failed to create object: {}", key, e);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)202 IOException (java.io.IOException)70 AmazonServiceException (com.amazonaws.AmazonServiceException)32 ArrayList (java.util.ArrayList)32 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)23 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)19 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)17 HashMap (java.util.HashMap)16 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)14 Test (org.junit.Test)14 SienaException (siena.SienaException)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)11 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)11 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 InterruptedIOException (java.io.InterruptedIOException)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)9 File (java.io.File)9