Search in sources :

Example 1 with ArchiveTransferManager

use of com.amazonaws.services.glacier.transfer.ArchiveTransferManager in project SAGU by brianmcmichael.

the class AmazonDownloadRequest method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtDownload) {
        archiveId = jtfDownloadField.getText().trim();
        if ((archiveId.equals(""))) {
            JOptionPane.showMessageDialog(null, "Enter the Archive ID of the file to be requested.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            SwingWorker<Object, Void> downloadWorker = new SwingWorker<Object, Void>() {

                private String archiveId = jtfDownloadField.getText().trim();

                @Override
                protected Void doInBackground() throws Exception {
                    // Create dumb progressbar
                    JFrame downloadFrame = new JFrame("Downloading");
                    {
                        downloadFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        final JProgressBar dumJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);
                        dumJProgressBar.setIndeterminate(true);
                        downloadFrame.add(dumJProgressBar, BorderLayout.NORTH);
                        downloadFrame.setSize(300, 60);
                    }
                    centerDefineFrame(downloadFrame, 300, 50);
                    try {
                        String vaultName = dlVault;
                        FileDialog fd = new FileDialog(new Frame(), "Save Archive As...", FileDialog.SAVE);
                        fd.setFile("Save Archive As...");
                        fd.setDirectory(System.getProperty("user.dir"));
                        fd.setLocation(50, 50);
                        fd.setVisible(true);
                        String filePath = "" + fd.getDirectory() + System.getProperty("file.separator") + fd.getFile();
                        File outFile = new File(filePath);
                        if (outFile != null) {
                            downloadFrame.setTitle("Downloading " + outFile.toString());
                            downloadFrame.setVisible(true);
                            final Endpoint endpoint = Endpoint.getByIndex(locationChoice);
                            AmazonSQSClient dlSQS = new AmazonSQSClient(dlCredentials);
                            AmazonSNSClient dlSNS = new AmazonSNSClient(dlCredentials);
                            dlSQS.setEndpoint(endpoint.getSQSEndpoint());
                            dlSNS.setEndpoint(endpoint.getSNSEndpoint());
                            // ArchiveTransferManager atm = new
                            // ArchiveTransferManager(dlClient,
                            // dlCredentials);
                            ArchiveTransferManager atm = new ArchiveTransferManager(dlClient, dlSQS, dlSNS);
                            atm.download("-", vaultName, archiveId, outFile);
                            JOptionPane.showMessageDialog(null, "Sucessfully downloaded " + outFile.toString(), "Success", JOptionPane.INFORMATION_MESSAGE);
                            downloadFrame.setVisible(false);
                        }
                    } catch (AmazonServiceException k) {
                        JOptionPane.showMessageDialog(null, "The server returned an error. Wait 24 hours after submitting an archive to attempt a download. Also check that correct location of archive has been set on the previous page.", "Error", JOptionPane.ERROR_MESSAGE);
                        System.out.println("" + k);
                        downloadFrame.setVisible(false);
                    } catch (AmazonClientException i) {
                        JOptionPane.showMessageDialog(null, "Client Error. Check that all fields are correct. Archive not downloaded.", "Error", JOptionPane.ERROR_MESSAGE);
                        downloadFrame.setVisible(false);
                    } catch (Exception j) {
                        JOptionPane.showMessageDialog(null, "Archive not found. Unspecified Error.", "Error", JOptionPane.ERROR_MESSAGE);
                        downloadFrame.setVisible(false);
                    }
                    return null;
                }
            };
            downloadWorker.execute();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            this.setVisible(false);
            dispose();
        }
    } else if (e.getSource() == jbtBack) {
        this.setVisible(false);
        dispose();
    } else {
        JOptionPane.showMessageDialog(this, "Please choose a valid action.");
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) ArchiveTransferManager(com.amazonaws.services.glacier.transfer.ArchiveTransferManager) Endpoint(com.brianmcmichael.sagu.Endpoint) AmazonServiceException(com.amazonaws.AmazonServiceException) File(java.io.File)

Example 2 with ArchiveTransferManager

use of com.amazonaws.services.glacier.transfer.ArchiveTransferManager in project glacier-cli by carlossg.

the class Glacier method download.

public void download(String vaultName, String archiveId, String downloadFilePath) {
    String msg = "Downloading " + archiveId + " from Glacier vault " + vaultName;
    System.out.println(msg);
    sqsClient = new AmazonSQSClient(credentials);
    sqsClient.setEndpoint("https://sqs." + region + ".amazonaws.com");
    snsClient = new AmazonSNSClient(credentials);
    snsClient.setEndpoint("https://sns." + region + ".amazonaws.com");
    try {
        ArchiveTransferManager atm = new ArchiveTransferManager(client, sqsClient, snsClient);
        atm.download(vaultName, archiveId, new File(downloadFilePath));
    } catch (Exception e) {
        throw new RuntimeException("Error " + msg, e);
    }
}
Also used : ArchiveTransferManager(com.amazonaws.services.glacier.transfer.ArchiveTransferManager) AmazonSQSClient(com.amazonaws.services.sqs.AmazonSQSClient) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient) File(java.io.File) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException)

Example 3 with ArchiveTransferManager

use of com.amazonaws.services.glacier.transfer.ArchiveTransferManager in project glacier-cli by carlossg.

the class Glacier method upload.

// ================
// Archive commands
// ================
public void upload(String vaultName, String archive) {
    String msg = "Uploading " + archive + " to Glacier vault " + vaultName;
    System.out.println(msg);
    try {
        ArchiveTransferManager atm = new ArchiveTransferManager(client, credentials);
        UploadResult result = atm.upload(vaultName, archive, new File(archive));
        System.out.println("Uploaded " + archive + ": " + result.getArchiveId());
    } catch (Exception e) {
        throw new RuntimeException("Error " + msg, e);
    }
}
Also used : ArchiveTransferManager(com.amazonaws.services.glacier.transfer.ArchiveTransferManager) UploadResult(com.amazonaws.services.glacier.transfer.UploadResult) File(java.io.File) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException)

Example 4 with ArchiveTransferManager

use of com.amazonaws.services.glacier.transfer.ArchiveTransferManager in project SAGU by brianmcmichael.

the class SAGU method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    String accessString = getAccessKey();
    String secretString = getSecretKey();
    String vaultString = getVaultName();
    int regionInt = getServerRegion();
    if (e.getSource() == newVaultButton && checkAWSFields()) {
        AmazonGlacierClient newVaultClient = makeClient(accessString, secretString, regionInt);
        AddVaultFrame avf = new AddVaultFrame(newVaultClient, regionInt);
        avf.setVisible(true);
    }
    if (e.getSource() == vaultSelector) {
        if (vaultSelector.getSelectedItem() != null) {
            if (vaultSelector.getSelectedIndex() == 0) {
                vaultField.setText("");
            } else {
                vaultField.setText(vaultSelector.getSelectedItem().toString());
            }
        }
    }
    if (e.getSource() == loginButton) {
        repopulateVaults(accessString, secretString);
    }
    if (e.getSource() == exitApplicationMnu) {
        System.exit(0);
    }
    if (e.getSource() == updateMnu || e.getSource() == checkUpdateButton) {
        JHyperlinkLabel.OpenURI(URL_STRING);
    }
    if (e.getSource() == saveFileMnu) {
        FileDialog fd = new FileDialog(new Frame(), "Save...", FileDialog.SAVE);
        fd.setFile("Glacier.txt");
        fd.setDirectory(appProperties.getDir());
        fd.setLocation(50, 50);
        fd.setVisible(true);
        String filePath = "" + fd.getDirectory() + System.getProperty("file.separator") + fd.getFile();
        File outFile = new File(filePath);
        if (!outFile.equals("") && !outFile.equals("null")) {
            try {
                FileReader fr = new FileReader(getLogFile(0, appProperties));
                BufferedReader br = new BufferedReader(fr);
                FileWriter saveFile = new FileWriter(outFile.toString());
                int count = 0;
                boolean moreLines = true;
                String ln1;
                String ln2;
                String ln3;
                while (moreLines) {
                    ln1 = br.readLine();
                    ln2 = br.readLine();
                    ln3 = br.readLine();
                    if (ln1 == null) {
                        ln1 = "";
                    }
                    if (ln2 == null) {
                        ln2 = "";
                    }
                    if (ln3 == null) {
                        ln3 = "";
                    }
                    saveFile.write(ln1);
                    saveFile.write("\r\n");
                    saveFile.write(ln2);
                    saveFile.write("\r\n");
                    saveFile.write(ln3);
                    saveFile.write("\r\n");
                    count++;
                    if (ln3.equals("")) {
                        moreLines = false;
                        br.close();
                        saveFile.close();
                        JOptionPane.showMessageDialog(null, "Successfully exported " + count + " archive records to " + outFile.toString(), "Export", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            } catch (FileNotFoundException e1) {
                JOptionPane.showMessageDialog(null, "Unable to locate Glacier.log", "Error", JOptionPane.ERROR_MESSAGE);
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    if (e.getSource() == viewLog || e.getSource() == logButton) {
        File f = getLogFile(logTypes.getSelectedIndex(), appProperties);
        if (f.exists()) {
            JHyperlinkLabel.OpenURI("" + f.toURI());
        } else {
            JOptionPane.showMessageDialog(null, "Log file " + f.getName() + " does not exist.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    if (e.getSource() == deleteArchiveMnu) {
        if (checkAllFields()) {
            AmazonGlacierClient newDeleteClient = makeClient(accessString, secretString, regionInt);
            DeleteArchiveFrame daf = new DeleteArchiveFrame(newDeleteClient, vaultString, regionInt);
            daf.setVisible(true);
        }
    }
    if (e.getSource() == inventoryRequestButton) {
        if (checkAllFields()) {
            AmazonGlacierClient newInventoryClient = makeClient(accessString, secretString, regionInt);
            InventoryRequest ir = new InventoryRequest(newInventoryClient, vaultString, regionInt);
            ir.setVisible(true);
        }
    }
    if (e.getSource() == downloadRequestButton || e.getSource() == downloadFileMnu) {
        if (checkAllFields()) {
            AmazonGlacierClient newDownloadClient = makeClient(accessString, secretString, regionInt);
            BasicAWSCredentials credentials = new BasicAWSCredentials(accessString, secretString);
            AmazonDownloadRequest adr = new AmazonDownloadRequest(newDownloadClient, vaultString, regionInt, credentials);
            adr.setVisible(true);
        }
    }
    if (e.getSource() == aboutMnu) {
        JOptionPane.showMessageDialog(null, format(ABOUT_PATTERN, versionNumber), "About", JOptionPane.INFORMATION_MESSAGE);
    }
    if (e.getSource() == clearButton) {
        ddText.setText("");
        uploadButton.setText("Select Files");
        multiFiles = null;
    }
    if (e.getSource() == locationChoice) {
        repopulateVaults(accessString, secretString);
    }
    if (e.getSource() == selectFileButton) {
        int returnVal = fc.showOpenDialog(SAGU.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            if (fc.getSelectedFile().isFile()) {
                File[] thisFile = new File[1];
                thisFile[0] = fc.getSelectedFile();
                try {
                    ddText.append(thisFile[0].getCanonicalPath() + "\n");
                } catch (java.io.IOException f) {
                }
                if (multiFiles != null) {
                    multiFiles = SAGUUtils.concatFileArrays(multiFiles, thisFile);
                } else {
                    multiFiles = thisFile;
                }
            } else {
                JOptionPane.showMessageDialog(null, NO_DIRECTORIES_ERROR, "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    if (e.getSource() == uploadButton) {
        if ((checkAllFields()) && (checkForFile())) {
            SwingWorker<Object, Void> uploadWorker = new SwingWorker<Object, Void>() {

                @Override
                protected Object doInBackground() throws Exception {
                    String accessString = getAccessKey();
                    String secretString = getSecretKey();
                    String vaultName = getVaultName();
                    File[] uploadFileBatch = multiFiles;
                    // work out exactly how much we are going to upload
                    // so we can support a second total upload progress bar
                    long totalSize = 0;
                    long uploadedSize = 0;
                    for (File f : uploadFileBatch) {
                        totalSize += f.length();
                    }
                    int locInt = getServerRegion();
                    multiFiles = null;
                    clearFile();
                    UploadWindow uw = new UploadWindow();
                    if (uploadFileBatch.length > 0) {
                        ArrayList<String> uploadList = new ArrayList<String>();
                        for (int i = 0; i < uploadFileBatch.length; i++) {
                            try {
                                // why?
                                Thread.sleep(100L);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                            ClientConfiguration config = new ClientConfiguration();
                            config.setSocketTimeout(SOCKET_TIMEOUT);
                            config.setMaxErrorRetry(MAX_RETRIES);
                            BasicAWSCredentials credentials = new BasicAWSCredentials(accessString, secretString);
                            client = new AmazonGlacierClient(credentials, config);
                            final Endpoint endpoint = Endpoint.getByIndex(locInt);
                            client.setEndpoint(endpoint.getGlacierEndpoint());
                            String locationUpped = endpoint.name();
                            String thisFile = uploadFileBatch[i].getCanonicalPath();
                            final String description = SAGUUtils.pathToDescription(thisFile);
                            try {
                                ArchiveTransferManager atm = new ArchiveTransferManager(client, credentials);
                                String fileLength = Long.toString(uploadFileBatch[i].length());
                                uw.setTitle("(" + (i + 1) + "/" + uploadFileBatch.length + ")" + " Uploading: " + thisFile);
                                UploadResult result = atm.upload(null, vaultName, description, uploadFileBatch[i], new OneFileProgressListener(uw, uploadFileBatch[i].length()));
                                uw.addToFinishedFiles(thisFile + "\n");
                                uploadedSize += uploadFileBatch[i].length();
                                int percentage = (int) (((double) uploadedSize / totalSize) * 100);
                                uw.updateAllFilesProgress(percentage);
                                final LogWriter logWriter;
                                // write to file
                                if (logCheckMenuItem.isSelected()) {
                                    String treeHash = TreeHashGenerator.calculateTreeHash(uploadFileBatch[i]);
                                    try {
                                        logWriter = new LogWriter(appProperties);
                                        try {
                                            String thisResult = result.getArchiveId();
                                            logWriter.logUploadedFile(vaultName, locationUpped, thisFile, fileLength, treeHash, thisResult);
                                            uploadList.add("Successfully uploaded " + thisFile + " to vault " + vaultName + " at " + locationUpped + ". Bytes: " + fileLength + ". ArchiveID Logged.\n");
                                        } catch (IOException c) {
                                            JOptionPane.showMessageDialog(null, LOG_WRITE_ERROR, "IO Error", JOptionPane.ERROR_MESSAGE);
                                            uw.dispose();
                                            System.exit(1);
                                        }
                                    } catch (IOException ex) {
                                        JOptionPane.showMessageDialog(null, LOG_CREATION_ERROR, "IO Error", JOptionPane.ERROR_MESSAGE);
                                        uw.dispose();
                                        System.exit(1);
                                    }
                                } else {
                                    JOptionPane.showMessageDialog(null, "Upload Complete!\nArchive ID: " + result.getArchiveId() + "\nIt may take some time for Amazon to update the inventory.", "Uploaded", JOptionPane.INFORMATION_MESSAGE);
                                    multiFiles = null;
                                    uw.dispose();
                                }
                                clearFile();
                            } catch (Exception h) {
                                if (logCheckMenuItem.isSelected()) {
                                    writeToErrorLog(h, thisFile);
                                }
                                JOptionPane.showMessageDialog(null, "" + h, "Error", JOptionPane.ERROR_MESSAGE);
                                uw.dispose();
                            }
                        }
                        StringBuilder sb = new StringBuilder();
                        for (int j = 0; j < uploadFileBatch.length; j++) {
                            sb.append(uploadList.get(j));
                        }
                        uw.dispose();
                        // Move the actual results string to a JTextArea
                        JTextArea uploadCompleteMsg = new JTextArea("Upload Complete! \n" + sb);
                        uploadCompleteMsg.setLineWrap(true);
                        uploadCompleteMsg.setWrapStyleWord(true);
                        uploadCompleteMsg.setEditable(false);
                        // Put the JTextArea in a JScollPane and present that in the JOptionPane
                        JScrollPane uploadCompleteScroll = new JScrollPane(uploadCompleteMsg);
                        uploadCompleteScroll.setPreferredSize(new Dimension(500, 400));
                        JOptionPane.showMessageDialog(null, uploadCompleteScroll, "Uploaded", JOptionPane.INFORMATION_MESSAGE);
                        // Close the JProgressBar
                        multiFiles = null;
                        clearFile();
                    } else {
                        JOptionPane.showMessageDialog(null, "This wasn't supposed to happen.", "Bug!", JOptionPane.ERROR_MESSAGE);
                        uw.dispose();
                    }
                    return null;
                }

                private void writeToErrorLog(Exception h, String thisFile) {
                    String thisError = h.toString();
                    Writer errorOutputLog = null;
                    try {
                        errorOutputLog = new BufferedWriter(new FileWriter(getLogFile(4, appProperties), true));
                    } catch (Exception badLogCreate) {
                        JOptionPane.showMessageDialog(null, LOG_CREATION_ERROR, "IO Error", JOptionPane.ERROR_MESSAGE);
                        System.exit(1);
                    }
                    try {
                        Date d = new Date();
                        errorOutputLog.write(System.getProperty("line.separator"));
                        errorOutputLog.write("" + d.toString() + ": \"" + thisFile + "\" *ERROR* " + thisError);
                        errorOutputLog.write(System.getProperty("line.separator"));
                    } catch (Exception badLogWrite) {
                        JOptionPane.showMessageDialog(null, LOG_WRITE_ERROR, "IO Error", JOptionPane.ERROR_MESSAGE);
                        System.exit(1);
                    }
                }
            };
            uploadWorker.execute();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ArchiveTransferManager(com.amazonaws.services.glacier.transfer.ArchiveTransferManager) java.io(java.io) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Date(java.util.Date) AmazonGlacierClient(com.amazonaws.services.glacier.AmazonGlacierClient) UploadResult(com.amazonaws.services.glacier.transfer.UploadResult) LogWriter.getLogFile(com.brianmcmichael.sagu.LogWriter.getLogFile) ClientConfiguration(com.amazonaws.ClientConfiguration)

Aggregations

ArchiveTransferManager (com.amazonaws.services.glacier.transfer.ArchiveTransferManager)4 File (java.io.File)3 UploadResult (com.amazonaws.services.glacier.transfer.UploadResult)2 AmazonSNSClient (com.amazonaws.services.sns.AmazonSNSClient)2 AmazonSQSClient (com.amazonaws.services.sqs.AmazonSQSClient)2 IOException (java.io.IOException)2 UnrecognizedOptionException (org.apache.commons.cli.UnrecognizedOptionException)2 JsonParseException (org.codehaus.jackson.JsonParseException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ClientConfiguration (com.amazonaws.ClientConfiguration)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AmazonGlacierClient (com.amazonaws.services.glacier.AmazonGlacierClient)1 Endpoint (com.brianmcmichael.sagu.Endpoint)1 LogWriter.getLogFile (com.brianmcmichael.sagu.LogWriter.getLogFile)1 java.io (java.io)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1