Search in sources :

Example 1 with RestoreObjectRequest

use of com.amazonaws.services.s3.model.RestoreObjectRequest in project herd by FINRAOS.

the class MockS3OperationsImpl method restoreObject.

@Override
public void restoreObject(RestoreObjectRequest requestRestore, AmazonS3 s3Client) {
    if (requestRestore.getKey().endsWith(MockAwsOperationsHelper.AMAZON_THROTTLING_EXCEPTION)) {
        AmazonServiceException throttlingException = new AmazonServiceException("test throttling exception");
        throttlingException.setErrorCode("ThrottlingException");
        throw throttlingException;
    } else if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(requestRestore.getBucketName())) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(S3Operations.ERROR_CODE_NO_SUCH_BUCKET);
        amazonServiceException.setStatusCode(404);
        throw amazonServiceException;
    } else if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(requestRestore.getBucketName())) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(S3Operations.ERROR_CODE_ACCESS_DENIED);
        amazonServiceException.setStatusCode(403);
        throw amazonServiceException;
    } else if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(requestRestore.getBucketName()) || requestRestore.getKey().endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException(S3Operations.ERROR_CODE_INTERNAL_ERROR);
    } else {
        MockS3Bucket mockS3Bucket = getOrCreateBucket(requestRestore.getBucketName());
        MockS3Object mockS3Object = mockS3Bucket.getObjects().get(requestRestore.getKey());
        if (mockS3Object == null) {
            AmazonServiceException amazonServiceException = new AmazonServiceException(S3Operations.ERROR_CODE_NO_SUCH_KEY);
            amazonServiceException.setStatusCode(404);
            throw amazonServiceException;
        }
        // Get object metadata.
        ObjectMetadata objectMetadata = mockS3Object.getObjectMetadata();
        // Fail if the object is not in Glacier.
        if (!StorageClass.Glacier.toString().equals(objectMetadata.getStorageClass())) {
            AmazonServiceException amazonServiceException = new AmazonServiceException("object is not in Glacier");
            throw amazonServiceException;
        }
        // Fail if the object is already being restored.
        if (objectMetadata.getOngoingRestore()) {
            AmazonServiceException amazonServiceException = new AmazonServiceException("object is already being restored");
            throw amazonServiceException;
        }
        // Update the object metadata to indicate that there is an ongoing restore request.
        objectMetadata.setOngoingRestore(true);
    }
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 2 with RestoreObjectRequest

use of com.amazonaws.services.s3.model.RestoreObjectRequest in project herd by FINRAOS.

the class S3DaoImpl method restoreObjects.

@Override
public void restoreObjects(final S3FileTransferRequestParamsDto params, int expirationInDays) {
    LOGGER.info("Restoring a list of objects in S3... s3KeyPrefix=\"{}\" s3BucketName=\"{}\" s3KeyCount={}", params.getS3KeyPrefix(), params.getS3BucketName(), params.getFiles().size());
    if (!CollectionUtils.isEmpty(params.getFiles())) {
        // Initialize a key value pair for the error message in the catch block.
        String key = params.getFiles().get(0).getPath().replaceAll("\\\\", "/");
        try {
            // Create an S3 client.
            AmazonS3Client s3Client = getAmazonS3(params);
            // Create a restore object request.
            RestoreObjectRequest requestRestore = new RestoreObjectRequest(params.getS3BucketName(), null, expirationInDays);
            // Make Bulk as default glacier retrieval option
            requestRestore.setGlacierJobParameters(new GlacierJobParameters().withTier(GLACIER_RETRIEVAL_OPTION));
            try {
                for (File file : params.getFiles()) {
                    key = file.getPath().replaceAll("\\\\", "/");
                    ObjectMetadata objectMetadata = s3Operations.getObjectMetadata(params.getS3BucketName(), key, s3Client);
                    // Request a restore for objects that are not already being restored.
                    if (BooleanUtils.isNotTrue(objectMetadata.getOngoingRestore())) {
                        requestRestore.setKey(key);
                        s3Operations.restoreObject(requestRestore, s3Client);
                    }
                }
            } finally {
                s3Client.shutdown();
            }
        } catch (Exception e) {
            throw new IllegalStateException(String.format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. Reason: %s", key, params.getS3BucketName(), e.getMessage()), e);
        }
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) GlacierJobParameters(com.amazonaws.services.s3.model.GlacierJobParameters) RestoreObjectRequest(com.amazonaws.services.s3.model.RestoreObjectRequest) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 GlacierJobParameters (com.amazonaws.services.s3.model.GlacierJobParameters)1 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)1 RestoreObjectRequest (com.amazonaws.services.s3.model.RestoreObjectRequest)1 File (java.io.File)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1