Example usage for com.amazonaws.services.glacier.model DescribeJobResult getStatusCode

List of usage examples for com.amazonaws.services.glacier.model DescribeJobResult getStatusCode

Introduction

In this page you can find the example usage for com.amazonaws.services.glacier.model DescribeJobResult getStatusCode.

Prototype


public String getStatusCode() 

Source Link

Document

The status code can be InProgress, Succeeded, or Failed, and indicates the status of the job.

Usage

From source file:maebackup.MaeBackup.java

License:Open Source License

public static void download(String filename, String jobid) {
    try {//from w w w  .  j a va 2 s.co  m
        System.out.println("Starting download...");
        ClientConfiguration config = new ClientConfiguration();
        config.setProtocol(Protocol.HTTPS);
        AmazonGlacierClient client = new AmazonGlacierClient(credentials, config);
        client.setEndpoint(endpoint);

        if (jobid == null || jobid == "") {
            String archiveid;
            // Yes, this will screw up on actual 138-character file names, but... yeah.
            if (filename.length() == 138) {
                archiveid = filename;
            } else {
                File listfile = new File(cachedir, "archives.lst");
                Map<File, String> filemap = loadHashes(listfile);
                archiveid = filemap.get(filename);
                if (archiveid == null) {
                    System.err.println("Error: Could not find archive ID for file " + filename);
                    System.exit(1);
                    return;
                }
            }

            InitiateJobResult result = client.initiateJob(new InitiateJobRequest(vaultname,
                    new JobParameters().withType("archive-retrieval").withArchiveId(archiveid)));
            jobid = result.getJobId();
            System.out.println("Started download job as ID " + jobid);
        } else {
            DescribeJobResult djres = client.describeJob(new DescribeJobRequest(vaultname, jobid));
            if (!djres.getStatusCode().equals("Succeeded")) {
                System.out.println("Job is not listed as Succeeded. It is: " + djres.getStatusCode());
                System.out.println(djres.getStatusMessage());
                System.exit(2);
            }
            long size = djres.getArchiveSizeInBytes();
            long chunks = size / chunksize;
            while (chunks > 10000) {
                chunksize <<= 1;
                chunks = size / chunksize;
            }
            RandomAccessFile raf = new RandomAccessFile(filename, "w");
            raf.setLength(size);
            byte[] buffer = new byte[chunksize];

            for (int x = 0; x < chunks; x++) {
                try {
                    System.out.println("Downloading chunk " + x + " of " + chunks);
                    String range = "bytes " + (x * chunksize) + "-" + ((x + 1) * chunksize - 1) + "/*";

                    GetJobOutputResult gjores = client
                            .getJobOutput(new GetJobOutputRequest(vaultname, jobid, range));

                    gjores.getBody().read(buffer);

                    MessageDigest md = MessageDigest.getInstance("SHA-256");
                    md.update(buffer, 0, chunksize);

                    byte[] hash = md.digest();

                    StringBuffer sb = new StringBuffer();
                    for (byte b : hash) {
                        sb.append(String.format("%02x", b));
                    }
                    if (!sb.toString().equalsIgnoreCase(gjores.getChecksum())) {
                        System.err.println("Error: Chunk " + x + " does not match SHA-256. Retrying.");
                        x--;
                        continue;
                    }

                    raf.seek(x * chunksize);
                    raf.write(buffer);
                } catch (Exception e) {
                    System.err.println("Error: Exception while downloading chunk " + x + ". Retrying.");
                    x--;
                }
            }

            if (size > chunks * chunksize) {
                do {
                    try {
                        System.out.println("Downloading final partial chunk");
                        String range = "bytes " + (chunks * chunksize) + "-" + (size - 1) + "/*";

                        GetJobOutputResult gjores = client
                                .getJobOutput(new GetJobOutputRequest(vaultname, jobid, range));

                        int bytes = gjores.getBody().read(buffer);

                        MessageDigest md = MessageDigest.getInstance("SHA-256");
                        md.update(buffer, 0, bytes);

                        byte[] hash = md.digest();

                        StringBuffer sb = new StringBuffer();
                        for (byte b : hash) {
                            sb.append(String.format("%02x", b));
                        }
                        if (!sb.toString().equalsIgnoreCase(gjores.getChecksum())) {
                            System.err.println("Error: Final chunk does not match SHA-256. Retrying.");
                            continue;
                        }

                        raf.seek(chunks * chunksize);
                        raf.write(buffer, 0, bytes);
                    } catch (Exception e) {
                        System.err.println("Error: Exception while downloading final chunk. Retrying.");
                        continue;
                    }
                } while (false);
            }
            raf.close();

            String treehash = TreeHashGenerator.calculateTreeHash(new File(filename));
            if (!treehash.equalsIgnoreCase(djres.getSHA256TreeHash())) {
                System.err.println("Error: File failed final tree hash check.");
                System.exit(3);
            }

            System.out.println("Download complete.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:maebackup.MaeBackup.java

License:Open Source License

public static void list(String arg) {
    try {//from  w w  w  .ja v a 2 s  . c om
        System.out.println("Listing Glacier vault...");
        ClientConfiguration config = new ClientConfiguration();
        config.setProtocol(Protocol.HTTPS);
        AmazonGlacierClient client = new AmazonGlacierClient(credentials, config);
        client.setEndpoint(endpoint);

        if (arg == null || arg == "") {
            InitiateJobResult result = client.initiateJob(
                    new InitiateJobRequest(vaultname, new JobParameters().withType("inventory-retrieval")));
            String jobid = result.getJobId();
            System.out.println("Started inventory retrival job as ID " + jobid);
        } else {
            DescribeJobResult djres = client.describeJob(new DescribeJobRequest(vaultname, arg));
            if (!djres.getStatusCode().equals("Succeeded")) {
                System.out.println("Job is not listed as Succeeded. It is: " + djres.getStatusCode());
                System.out.println(djres.getStatusMessage());
                System.exit(2);
            }

            GetJobOutputResult gjores = client
                    .getJobOutput(new GetJobOutputRequest().withVaultName(vaultname).withJobId(arg));
            byte[] buffer = new byte[1024];
            int bytes;
            while ((bytes = gjores.getBody().read(buffer)) > 0) {
                System.out.write(buffer, 0, bytes);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:opendap.aws.glacier.Download.java

License:Open Source License

public boolean jobCompleted() throws IOException {

    _log.debug("archiveRetrievalJobCompleted() - BEGIN ");

    if (!_started)
        throw new IOException("Glacier retrieval job has NOT been started!");

    AmazonGlacierAsyncClient client = new AmazonGlacierAsyncClient(getCredentials());
    client.setEndpoint(getEndpointUrl());

    InitiateJobResult initiateJobResult = getInitiateJobResult();

    long timeRemaining = estimatedTimeRemaining();

    _log.debug("archiveRetrievalJobCompleted() - Estimated Time Remaining: {} seconds  jobId: {}",
            timeRemaining, initiateJobResult.getJobId());

    DescribeJobRequest djr = new DescribeJobRequest(getVaultName(), initiateJobResult.getJobId());

    DescribeJobResult describeJobResult = client.describeJob(djr);

    _log.debug("archiveRetrievalJobCompleted() - DescribeJobResult: {}", describeJobResult.toString());
    _log.debug("archiveRetrievalJobCompleted() - DescribeJobResult.isCompleted(): {}",
            describeJobResult.isCompleted());
    _log.debug("archiveRetrievalJobCompleted() - DescribeJobResult.status(): {}",
            describeJobResult.getStatusCode());

    _log.debug("archiveRetrievalJobCompleted() - END ");

    return describeJobResult.isCompleted();

}