com.leverno.ysbos.archive.AwsArchiver.java Source code

Java tutorial

Introduction

Here is the source code for com.leverno.ysbos.archive.AwsArchiver.java

Source

/**
 * Code to backup files to Glacier
 *
 * <p/>
 * Copyright (C) 2012  Werner van Rensburg
 * <p/>
 * <p/>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <p/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.leverno.ysbos.archive;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.glacier.AmazonGlacierClient;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.UploadResult;
import com.leverno.ysbos.common.Constants;
import org.apache.commons.io.FileUtils;

public class AwsArchiver implements Archiver {

    private final AWSCredentials credentials;
    private final AmazonGlacierClient client;

    public AwsArchiver() {
        credentials = AwsUtil.getCredentials();
        client = AwsUtil.getClient(credentials);
    }

    @Override
    public List<String> archive(List<File> filesToArchive) {
        List<String> result = new ArrayList<String>();

        for (File file : filesToArchive) {
            UploadResult uploadResult = uploadFileToGlacier(file);
            result.add(uploadResult.getArchiveId());
        }

        return result;
    }

    public UploadResult uploadFileToGlacier(File archiveFile) {
        ArchiveTransferManager atm = new ArchiveTransferManager(client, credentials);

        UploadResult upload = null;
        try {
            upload = atm.upload(Constants.vault, getArchiveDescription(archiveFile), archiveFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return upload;
    }

    /**
     * Returns a description in JSON format containing the
     * <ul>
     * <li>file path</li>
     * <li>file checksum</li>
     * </ul>
     *
     * @param archiveFile
     * @return the description of the archive in JSON markup
     */
    private String getArchiveDescription(File archiveFile) throws IOException {
        return String.format("{%s:%s,%s:%s", quotedString("path"), quotedString(archiveFile.getAbsolutePath()),
                quotedString("checksum_CRC32"), quotedString(Long.toString(FileUtils.checksumCRC32(archiveFile))));
    }

    private String quotedString(String input) {
        return String.format("\"%s\"", input);
    }

    //    public void createVault() {
    //        AWSCredentials credentials = new PropertiesCredentials(AwsArchiver.class.getResourceAsStream("/home/vern/.geheim/was.properties"));
    //        AmazonGlacierClient client = new AmazonGlacierClient(credentials);
    //        client.setEndpoint("https://glacier.us-east-1.amazonaws.com/");
    //
    //        String s = "kiekies";
    //        CreateVaultRequest request = new CreateVaultRequest()
    //                .withAccountId("-")
    //                .withVaultName(s);
    //        CreateVaultResult result = client.createVault(request);
    //    }

}