com.amazonaws.services.s3.model.RestoreObjectRequest Java Examples

The following examples show how to use com.amazonaws.services.s3.model.RestoreObjectRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AmazonS3FileSystem.java    From iaf with Apache License 2.0 6 votes vote down vote up
public String restoreObject(String fileName) throws SenderException {
	Boolean restoreFlag;
	try {
		bucketDoesNotExist(bucketName);
		fileDoesNotExist(bucketName, fileName);
		RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, fileName, expirationInDays).withTier(tier);
		s3Client.restoreObjectV2(requestRestore);
		log.debug("Object with fileName [" + fileName + "] and bucketName [" + bucketName + "] restored from Amazon S3 Glacier");

		ObjectMetadata response = s3Client.getObjectMetadata(bucketName, fileName);
		restoreFlag = response.getOngoingRestore();
		System.out.format("Restoration status: %s.\n", restoreFlag ? "in progress" : "not in progress (finished or failed)");

	} catch (AmazonServiceException e) {
		log.error("Failed to perform [restore] action, and restore object with fileName [" + fileName + "] from Amazon S3 Glacier");
		throw new SenderException("Failed to perform [restore] action, and restore object with fileName [" + fileName + "] from Amazon S3 Glacier");
	}

	String prefix = "Restoration status: %s.\n";
	return restoreFlag ? prefix + "in progress" : prefix + "not in progress (finished or failed)";
}
 
Example #2
Source File: S3OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public void restoreObject(RestoreObjectRequest requestRestore, AmazonS3 s3Client)
{
    s3Client.restoreObject(requestRestore);
}
 
Example #3
Source File: S3DaoImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public void restoreObjects(final S3FileTransferRequestParamsDto params, int expirationInDays, String archiveRetrievalOption)
{
    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 the default archive retrieval option if the option is not provided
            requestRestore.setGlacierJobParameters(new GlacierJobParameters().withTier(
                StringUtils.isNotEmpty(archiveRetrievalOption) ? archiveRetrievalOption : Tier.Bulk.toString()));

            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);

                        try
                        {
                            // Try the S3 restore operation on this file.
                            s3Operations.restoreObject(requestRestore, s3Client);
                        }
                        catch (AmazonS3Exception amazonS3Exception)
                        {
                            // If this exception has a status code of 409, log the information and continue to the next file.
                            if (amazonS3Exception.getStatusCode() == HttpStatus.SC_CONFLICT)
                            {
                                LOGGER.info("Restore already in progress for file with s3Key=\"{}\".", key);
                            }
                            // Else, we need to propagate the exception to the next level of try/catch block.
                            else
                            {
                                throw new Exception(amazonS3Exception);
                            }
                        }
                    }
                }
            }
            finally
            {
                s3Client.shutdown();
            }
        }
        catch (Exception e)
        {
            if (StringUtils.contains(e.getMessage(), "Retrieval option is not supported by this storage class"))
            {
                throw new IllegalArgumentException(String
                    .format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. Reason: %s", key, params.getS3BucketName(),
                        e.getMessage()), e);
            }
            else
            {
                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);
            }
        }
    }
}
 
Example #4
Source File: S3DaoImplTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testRestoreObjectsInDeepArchiveWithExpeditedArchiveRetrievalOption()
{
    List<File> files = Collections.singletonList(new File(TEST_FILE));

    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    s3FileTransferRequestParamsDto.setFiles(files);

    // Create a retry policy.
    RetryPolicy retryPolicy =
        new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);

    // Create an Object Metadata with DeepArchive storage class.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setOngoingRestore(false);
    objectMetadata.setHeader(Headers.STORAGE_CLASS, StorageClass.DeepArchive);

    ArgumentCaptor<AmazonS3Client> s3ClientCaptor = ArgumentCaptor.forClass(AmazonS3Client.class);
    ArgumentCaptor<String> s3BucketNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);

    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.getObjectMetadata(s3BucketNameCaptor.capture(), keyCaptor.capture(), s3ClientCaptor.capture())).thenReturn(objectMetadata);

    doThrow(new AmazonServiceException("Retrieval option is not supported by this storage class")).when(s3Operations)
        .restoreObject(any(RestoreObjectRequest.class), any(AmazonS3.class));

    try
    {
        s3DaoImpl.restoreObjects(s3FileTransferRequestParamsDto, EXPIRATION_IN_DAYS, Tier.Expedited.toString());
        fail();
    }
    catch (IllegalArgumentException e)
    {
        assertEquals(String.format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. " +
                "Reason: Retrieval option is not supported by this storage class (Service: null; Status Code: 0; Error Code: null; Request ID: null)",
            TEST_FILE, S3_BUCKET_NAME), e.getMessage());
    }
}
 
Example #5
Source File: S3DaoImplTest.java    From herd with Apache License 2.0 4 votes vote down vote up
private void testRestoreObjectsWithS3Exception(String exceptionMessage, int statusCode)
{
    List<File> files = Collections.singletonList(new File(TEST_FILE));

    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    s3FileTransferRequestParamsDto.setFiles(files);

    // Create a retry policy.
    RetryPolicy retryPolicy =
        new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);

    // Create an Object Metadata
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setOngoingRestore(false);
    objectMetadata.setHeader(Headers.STORAGE_CLASS, StorageClass.DeepArchive);

    ArgumentCaptor<AmazonS3Client> s3ClientCaptor = ArgumentCaptor.forClass(AmazonS3Client.class);
    ArgumentCaptor<String> s3BucketNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<RestoreObjectRequest> requestStoreCaptor = ArgumentCaptor.forClass(RestoreObjectRequest.class);

    // Create an Amazon S3 Exception
    AmazonS3Exception amazonS3Exception = new AmazonS3Exception(exceptionMessage);
    amazonS3Exception.setStatusCode(statusCode);

    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.getObjectMetadata(s3BucketNameCaptor.capture(), keyCaptor.capture(), s3ClientCaptor.capture())).thenReturn(objectMetadata);
    doThrow(amazonS3Exception).when(s3Operations).restoreObject(requestStoreCaptor.capture(), s3ClientCaptor.capture());

    try
    {
        // Call the method under test.
        s3DaoImpl.restoreObjects(s3FileTransferRequestParamsDto, EXPIRATION_IN_DAYS, Tier.Standard.toString());

        // If this is not a restore already in progress exception message (409) then we should have caught an exception.
        // Else if this is a restore already in progress message (409) then continue as usual.
        if (!exceptionMessage.equals(RESTORE_ALREADY_IN_PROGRESS_EXCEPTION_MESSAGE))
        {
            // Should not be here. Fail!
            fail();
        }
        else
        {
            RestoreObjectRequest requestStore = requestStoreCaptor.getValue();
            assertEquals(S3_BUCKET_NAME, s3BucketNameCaptor.getValue());
            assertEquals(TEST_FILE, keyCaptor.getValue());

            // Verify Bulk option is used when the option is not provided
            assertEquals(StringUtils.isNotEmpty(Tier.Standard.toString())
                ? Tier.Standard.toString() : Tier.Bulk.toString(), requestStore.getGlacierJobParameters().getTier());
        }
    }
    catch (IllegalStateException illegalStateException)
    {
        assertEquals(String.format("Failed to initiate a restore request for \"%s\" key in \"%s\" bucket. " +
                "Reason: com.amazonaws.services.s3.model.AmazonS3Exception: %s " +
                "(Service: null; Status Code: %s; Error Code: null; Request ID: null; S3 Extended Request ID: null), S3 Extended Request ID: null",
            TEST_FILE, S3_BUCKET_NAME, exceptionMessage, statusCode), illegalStateException.getMessage());
    }

    // Verify the external calls
    verify(retryPolicyFactory).getRetryPolicy();
    verify(s3Operations).getObjectMetadata(anyString(), anyString(), any(AmazonS3Client.class));
    verify(s3Operations).restoreObject(any(RestoreObjectRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
 
Example #6
Source File: S3DaoImplTest.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Run restore objects method
 *
 * @param archiveRetrievalOption the archive retrieval option
 */
private void runRestoreObjects(String archiveRetrievalOption, StorageClass storageClass)
{
    List<File> files = Collections.singletonList(new File(TEST_FILE));

    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);
    s3FileTransferRequestParamsDto.setFiles(files);

    // Create a retry policy.
    RetryPolicy retryPolicy =
        new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);

    // Create an Object Metadata
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setOngoingRestore(false);
    objectMetadata.setHeader(Headers.STORAGE_CLASS, storageClass);

    ArgumentCaptor<AmazonS3Client> s3ClientCaptor = ArgumentCaptor.forClass(AmazonS3Client.class);
    ArgumentCaptor<String> s3BucketNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<RestoreObjectRequest> requestStoreCaptor = ArgumentCaptor.forClass(RestoreObjectRequest.class);

    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.getObjectMetadata(s3BucketNameCaptor.capture(), keyCaptor.capture(), s3ClientCaptor.capture())).thenReturn(objectMetadata);
    doNothing().when(s3Operations).restoreObject(requestStoreCaptor.capture(), s3ClientCaptor.capture());

    s3DaoImpl.restoreObjects(s3FileTransferRequestParamsDto, EXPIRATION_IN_DAYS, archiveRetrievalOption);

    RestoreObjectRequest requestStore = requestStoreCaptor.getValue();
    assertEquals(S3_BUCKET_NAME, s3BucketNameCaptor.getValue());
    assertEquals(TEST_FILE, keyCaptor.getValue());

    // Verify Bulk option is used when the option is not provided
    assertEquals(StringUtils.isNotEmpty(archiveRetrievalOption)
        ? archiveRetrievalOption : Tier.Bulk.toString(), requestStore.getGlacierJobParameters().getTier());

    // Verify the external calls
    verify(retryPolicyFactory).getRetryPolicy();
    verify(s3Operations).getObjectMetadata(anyString(), anyString(), any(AmazonS3Client.class));
    verify(s3Operations).restoreObject(any(RestoreObjectRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}
 
Example #7
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public void restoreObject(RestoreObjectRequest req) {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #8
Source File: RestoreObject.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void run() {
    String message = null;
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    File file = new File(what);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration());
    if (endpoint.contains("amazonaws.com")) {
        String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
        if (aws_endpoint.contains("US")) {
            s3Client.setEndpoint("https://s3.amazonaws.com");
        } else if (aws_endpoint.contains("us-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("eu-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("ap-")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("sa-east-1")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else {
            s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
        }
    } else {
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
    }

    try {
        RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucket, what, 2);
        s3Client.restoreObject(requestRestore);

        GetObjectMetadataRequest requestCheck = new GetObjectMetadataRequest(bucket, what);
        ObjectMetadata response = s3Client.getObjectMetadata(requestCheck);

        Boolean restoreFlag = response.getOngoingRestore();
        mainFrame.jTextArea1.append("\nRestoration in progress. Please try to access the file again in a few hours.");
        calibrate();
    } catch (AmazonS3Exception amazonS3Exception) {
        mainFrame.jTextArea1.append("\nAn Amazon S3 error occurred. Exception: %s" + amazonS3Exception.toString());
        calibrate();
    } catch (Exception ex) {
        mainFrame.jTextArea1.append("\nException: %s" + ex.toString());
        calibrate();
    }

    calibrate();
}
 
Example #9
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void restoreObject(RestoreObjectRequest request) throws AmazonServiceException {
  // TODO Auto-generated method stub

}
 
Example #10
Source File: S3Operations.java    From herd with Apache License 2.0 2 votes vote down vote up
/**
 * Requests to restore an object, which was transitioned to Amazon Glacier from Amazon S3 when it was expired, into Amazon S3 again.
 *
 * @param requestRestore the request object containing all the options for restoring an object
 * @param s3Client the {@link AmazonS3} implementation to use
 */
public void restoreObject(RestoreObjectRequest requestRestore, AmazonS3 s3Client);