Search in sources :

Example 1 with RestoreObjectRequest

use of software.amazon.awssdk.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)

Example 2 with RestoreObjectRequest

use of software.amazon.awssdk.services.s3.model.RestoreObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class RestoreObject method restoreS3Object.

// snippet-start:[s3.java2.restore_object.main]
public static void restoreS3Object(S3Client s3, String bucketName, String keyName, String expectedBucketOwner) {
    try {
        RestoreRequest restoreRequest = RestoreRequest.builder().days(10).glacierJobParameters(GlacierJobParameters.builder().tier(Tier.STANDARD).build()).build();
        RestoreObjectRequest objectRequest = RestoreObjectRequest.builder().expectedBucketOwner(expectedBucketOwner).bucket(bucketName).key(keyName).restoreRequest(restoreRequest).build();
        s3.restoreObject(objectRequest);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    s3.close();
}
Also used : S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) RestoreObjectRequest(software.amazon.awssdk.services.s3.model.RestoreObjectRequest) RestoreRequest(software.amazon.awssdk.services.s3.model.RestoreRequest)

Example 3 with RestoreObjectRequest

use of software.amazon.awssdk.services.s3.model.RestoreObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class RestoreArchivedObject method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    String keyName = "*** Object key ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Create and submit a request to restore an object from Glacier for two days.
        RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, keyName, 2);
        s3Client.restoreObjectV2(requestRestore);
        // Check the restoration status of the object.
        ObjectMetadata response = s3Client.getObjectMetadata(bucketName, keyName);
        Boolean restoreFlag = response.getOngoingRestore();
        System.out.format("Restoration status: %s.\n", restoreFlag ? "in progress" : "not in progress (finished or failed)");
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) RestoreObjectRequest(com.amazonaws.services.s3.model.RestoreObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 RestoreObjectRequest (com.amazonaws.services.s3.model.RestoreObjectRequest)2 AmazonClientException (com.amazonaws.AmazonClientException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)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 File (java.io.File)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 RestoreObjectRequest (software.amazon.awssdk.services.s3.model.RestoreObjectRequest)1 RestoreRequest (software.amazon.awssdk.services.s3.model.RestoreRequest)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1