Search in sources :

Example 41 with ClientConfiguration

use of com.amazonaws.ClientConfiguration in project cloudstack by apache.

the class S3Utils method getTransferManager.

public static TransferManager getTransferManager(final ClientOptions clientOptions) {
    if (TRANSFERMANAGER_ACCESSKEY_MAP.containsKey(clientOptions.getAccessKey())) {
        return TRANSFERMANAGER_ACCESSKEY_MAP.get(clientOptions.getAccessKey());
    }
    final AWSCredentials basicAWSCredentials = new BasicAWSCredentials(clientOptions.getAccessKey(), clientOptions.getSecretKey());
    final ClientConfiguration configuration = new ClientConfiguration();
    if (clientOptions.isHttps() != null) {
        configuration.setProtocol(clientOptions.isHttps() ? HTTPS : HTTP);
    }
    if (clientOptions.getConnectionTimeout() != null) {
        configuration.setConnectionTimeout(clientOptions.getConnectionTimeout());
    }
    if (clientOptions.getMaxErrorRetry() != null) {
        configuration.setMaxErrorRetry(clientOptions.getMaxErrorRetry());
    }
    if (clientOptions.getSocketTimeout() != null) {
        configuration.setSocketTimeout(clientOptions.getSocketTimeout());
    }
    if (clientOptions.getUseTCPKeepAlive() != null) {
        configuration.setUseTcpKeepAlive(clientOptions.getUseTCPKeepAlive());
    }
    if (clientOptions.getConnectionTtl() != null) {
        configuration.setConnectionTTL(clientOptions.getConnectionTtl());
    }
    if (clientOptions.getSigner() != null) {
        configuration.setSignerOverride(clientOptions.getSigner());
    }
    LOGGER.debug(format("Creating S3 client with configuration: [protocol: %1$s, signer: %2$s, connectionTimeOut: %3$s, maxErrorRetry: %4$s, socketTimeout: %5$s, useTCPKeepAlive: %6$s, connectionTtl: %7$s]", configuration.getProtocol(), configuration.getSignerOverride(), configuration.getConnectionTimeout(), configuration.getMaxErrorRetry(), configuration.getSocketTimeout(), clientOptions.getUseTCPKeepAlive(), clientOptions.getConnectionTtl()));
    final AmazonS3Client client = new AmazonS3Client(basicAWSCredentials, configuration);
    if (isNotBlank(clientOptions.getEndPoint())) {
        LOGGER.debug(format("Setting the end point for S3 client with access key %1$s to %2$s.", clientOptions.getAccessKey(), clientOptions.getEndPoint()));
        client.setEndpoint(clientOptions.getEndPoint());
    }
    TRANSFERMANAGER_ACCESSKEY_MAP.put(clientOptions.getAccessKey(), new TransferManager(client));
    return TRANSFERMANAGER_ACCESSKEY_MAP.get(clientOptions.getAccessKey());
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 42 with ClientConfiguration

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

ClientConfiguration (com.amazonaws.ClientConfiguration)42 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)17 AWSCredentials (com.amazonaws.auth.AWSCredentials)13 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)5 AmazonDynamoDBClient (com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient)4 Configuration (org.apache.hadoop.conf.Configuration)4 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)3 File (java.io.File)3 Test (org.testng.annotations.Test)3 AmazonClientException (com.amazonaws.AmazonClientException)2 Protocol (com.amazonaws.Protocol)2 InstanceProfileCredentialsProvider (com.amazonaws.auth.InstanceProfileCredentialsProvider)2 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 S3ClientOptions (com.amazonaws.services.s3.S3ClientOptions)2 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)2 AmazonSimpleDB (com.amazonaws.services.simpledb.AmazonSimpleDB)2 AmazonSimpleDBClient (com.amazonaws.services.simpledb.AmazonSimpleDBClient)2 URI (java.net.URI)2 Date (java.util.Date)2