Search in sources :

Example 41 with BasicAWSCredentials

use of com.amazonaws.auth.BasicAWSCredentials in project SAGU by brianmcmichael.

the class SAGU method makeClient.

private AmazonGlacierClient makeClient(String accessorString, String secretiveString, int regionIndex) {
    BasicAWSCredentials credentials = new BasicAWSCredentials(accessorString, secretiveString);
    client = new AmazonGlacierClient(credentials);
    client.setEndpoint(Endpoint.getByIndex(regionIndex).getGlacierEndpoint());
    return client;
}
Also used : AmazonGlacierClient(com.amazonaws.services.glacier.AmazonGlacierClient) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 42 with BasicAWSCredentials

use of com.amazonaws.auth.BasicAWSCredentials 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

BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)42 AWSCredentials (com.amazonaws.auth.AWSCredentials)25 ClientConfiguration (com.amazonaws.ClientConfiguration)16 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)10 AmazonClientException (com.amazonaws.AmazonClientException)5 InstanceProfileCredentialsProvider (com.amazonaws.auth.InstanceProfileCredentialsProvider)4 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)3 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)3 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)3 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 AmazonSimpleDBClient (com.amazonaws.services.simpledb.AmazonSimpleDBClient)3 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 StaticCredentialsProvider (com.amazonaws.internal.StaticCredentialsProvider)2 Region (com.amazonaws.regions.Region)2 AmazonCloudWatchClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchClient)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 AmazonGlacierClient (com.amazonaws.services.glacier.AmazonGlacierClient)2 AmazonKinesisClient (com.amazonaws.services.kinesis.AmazonKinesisClient)2