Khan
Khan

Reputation: 1518

Set data retrieval option on S3 bucket for glacier storage class

My question is about data I have stored in S3 bucket that has the storage class glacier. I would like to retrieve it with the fastest option available, but I can't find a suitable method to achieve that. It seems like the default request is using the standard retrieval option.

In the aws docs, I found a nice way to restore the data.

From here I have this code example:

import java.io.IOException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.RestoreObjectRequest;

public class RestoreArchivedObject {

    public static void main(String[] args) throws IOException {
        String clientRegion = "*** Client 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();
        }
    }
}

That is nice and works. But i dont find in the docs how to set the Retrieval option if i want to choose between:

  1. Bulk retrieval 5-12 hours
  2. Expedited trieval 1-5 min
  3. Standard trieval 3-5 hours

here is the RestoreObjectRequest class from aws docs. There i can see a function setType(String type) Sets the restore request type. But there is no description about setting one of the mentioned options(1-3). Would be nice if someone can tell me if this is possible to set with java sdk aws.

EDIT:

Here i can read that setTier(String tier) should do it. The data access tier to use when restoring the archive. Standard is the default.

Type: Enum

Valid values: Expedited | Standard | Bulk

Ancestors: RestoreRequest And now i have an error message if i change the default Request to:

     RestoreObjectRequest requestRestore = new RestoreObjectRequest(bucketName, keyName, 2).withTier("Standard");

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The XML you provided was not well-formed or did not validate against our published schema

using somehow an old version of java sdk

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.11.386</version>
</dependency>

Upvotes: 2

Views: 1395

Answers (1)

FerranJr
FerranJr

Reputation: 332

Probably late to the party, but I found myself into the same situation ... and I guess something is wrong on the Java SDK.

I could achieve the expected result by defining the GlacierJobParameters with the Tier you required and then adding the GlacierJobParameters to the restore request.

A little Scala snippet which seems to then generate a valid XML for S3

val glacierJobParameters = (new GlacierJobParameters).withTier(tier)

val restoreObjectRequest =
  new RestoreObjectRequest(bucketName.value, key.value, expirationInDays)
    .withGlacierJobParameters(glacierJobParameters)

update After checking https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOSTrestore.html I can see that the withTier set directly on the RestoreObjectRequest would create a valid XML when doing a SELECT query, but in your case and my one we require the GlacierJobParameters.

Upvotes: 1

Related Questions