Example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

List of usage examples for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials.

Prototype

public BasicAWSCredentials(String accessKey, String secretKey) 

Source Link

Document

Constructs a new BasicAWSCredentials object, with the specified AWS access key and AWS secret key.

Usage

From source file:com.att.aro.core.cloud.aws.AwsRepository.java

License:Apache License

private void constructRepo(String accessId, String secretKey, String region, String bucketName,
        ClientConfiguration config) {//from w w w  .  j a  v  a2 s. c  o m
    System.setProperty("java.net.useSystemProxies", "true");
    if (isNotBlank(accessId) && isNotBlank(secretKey) && isNotBlank(region) && isNotBlank(bucketName)) {
        try {
            AWSCredentials creds = new BasicAWSCredentials(accessId, secretKey);
            Regions regions = Regions.fromName(region);
            this.bucketName = bucketName;
            s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds))
                    .withRegion(regions).withClientConfiguration(config).build();
            transferMgr = TransferManagerBuilder.standard().withS3Client(s3Client).build();
        } catch (IllegalArgumentException ille) {
            LOGGER.error(ille.getMessage(), ille);
        } catch (Exception exp) {
            LOGGER.error(exp.getMessage(), exp);
        }
    }
}

From source file:com.bigstep.S3Sampler.java

License:Apache License

@Override
public SampleResult runTest(JavaSamplerContext context) {
    // pull parameters
    String bucket = context.getParameter("bucket");
    String object = context.getParameter("object");
    String method = context.getParameter("method");
    String local_file_path = context.getParameter("local_file_path");
    String key_id = context.getParameter("key_id");
    String secret_key = context.getParameter("secret_key");
    String proxy_host = context.getParameter("proxy_host");
    String proxy_port = context.getParameter("proxy_port");
    String endpoint = context.getParameter("endpoint");

    log.debug("runTest:method=" + method + " local_file_path=" + local_file_path + " bucket=" + bucket
            + " object=" + object);

    SampleResult result = new SampleResult();
    result.sampleStart(); // start stopwatch

    try {/*from  w ww .  j av a2  s . c  o m*/
        ClientConfiguration config = new ClientConfiguration();
        if (proxy_host != null && !proxy_host.isEmpty()) {
            config.setProxyHost(proxy_host);
        }
        if (proxy_port != null && !proxy_port.isEmpty()) {
            config.setProxyPort(Integer.parseInt(proxy_port));
        }
        //config.setProtocol(Protocol.HTTP);

        AWSCredentials credentials = new BasicAWSCredentials(key_id, secret_key);

        AmazonS3 s3Client = new AmazonS3Client(credentials, config);
        if (endpoint != null && !endpoint.isEmpty()) {
            s3Client.setEndpoint(endpoint);
        }
        ObjectMetadata meta = null;

        if (method.equals("GET")) {
            File file = new File(local_file_path);
            //meta= s3Client.getObject(new GetObjectRequest(bucket, object), file);
            S3Object s3object = s3Client.getObject(bucket, object);
            S3ObjectInputStream stream = s3object.getObjectContent();
            //while(stream.skip(1024*1024)>0);
            stream.close();
        } else if (method.equals("PUT")) {
            File file = new File(local_file_path);
            s3Client.putObject(bucket, object, file);
        }

        result.sampleEnd(); // stop stopwatch
        result.setSuccessful(true);
        if (meta != null) {
            result.setResponseMessage(
                    "OK on url:" + bucket + "/" + object + ". Length=" + meta.getContentLength());
        } else {
            result.setResponseMessage("OK on url:" + bucket + "/" + object + ".No metadata");
        }
        result.setResponseCodeOK(); // 200 code

    } catch (Exception e) {
        result.sampleEnd(); // stop stopwatch
        result.setSuccessful(false);
        result.setResponseMessage("Exception: " + e);

        // get stack trace as a String to return as document data
        java.io.StringWriter stringWriter = new java.io.StringWriter();
        e.printStackTrace(new java.io.PrintWriter(stringWriter));
        result.setResponseData(stringWriter.toString());
        result.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT);
        result.setResponseCode("500");
    }

    return result;
}

From source file:com.bloomreach.bstore.highavailability.zookeeper.ZkClient.java

License:Apache License

/**
 * Fetch the public DNS names for the corresponding private Ips. SolrCloud defaults to private ips for all
 * interactions. If you want to run HAFT locally to copy data across 2 different zookeeper clusters, then we need
 * public IP translations to access the index. This method helps achieve that.
 *
 * @return {@link #getZkClusterData()} with private Ip to Public DNS Mapping based on EC2 api.
 *///from  w w w. j  a  v  a  2  s .c o  m
public ZkClusterData translatePrivateIpToPublicHostNames() {
    AWSCredentials credentials = new BasicAWSCredentials(AwsConfigReader.fetchAccessKey(),
            AwsConfigReader.fetchSecretyKey());
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    Set<String> publicDnsNameHosts = new HashSet<String>();
    Map<String, String> privateIptoPublicHostNames = new HashMap<String, String>();

    if (allSolrNodes.isEmpty()) {
        logger.info("No valid solr hosts are found. Cannot do any mapping");
        return zkClusterData;
    }

    //Describe Filter with private-ips matching all solr nodes
    DescribeInstancesRequest request = new DescribeInstancesRequest()
            .withFilters(new Filter("private-ip-address").withValues(allSolrNodes));
    DescribeInstancesResult describeInstancesResult = ec2.describeInstances(request);
    List<Reservation> reservations = describeInstancesResult.getReservations();
    //Iterate over all instances and map their private Ip to Public Host Name
    logger.info("Fetching Public HostNames....");

    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        for (Instance instance : instances) {
            logger.info("Private to Public Name of the Host is " + instance.getPrivateIpAddress() + " => "
                    + instance.getPublicDnsName());
            publicDnsNameHosts.add(instance.getPublicDnsName());
            privateIptoPublicHostNames.put(instance.getPrivateIpAddress(), instance.getPublicDnsName());
        }

    }
    //Point all zk data to point to the public dns names
    zkClusterData.updateSolrNodes(publicDnsNameHosts);
    //Set the data in a map so that it doesn't need to get recomputed by every function needing hostnames
    zkClusterData.setPrivateIpToPublicHostNameMap(privateIptoPublicHostNames);
    return zkClusterData;

}

From source file:com.blubb.andcw.AndCWActivity.java

License:Apache License

private AWSCredentials getAWSCredentials() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    if (prefs.getString("access_key", "XX").equals("XX"))
        return null;
    return new BasicAWSCredentials(prefs.getString("access_key", "XX"), prefs.getString("secret_key", "XX"));
}

From source file:com.boxupp.dao.AwsProjectDAOManager.java

License:Apache License

private String getPrivateHostName(String instanceID, String accessKeyId, String secretKey,
        String instanceRegion) {/*from ww  w .j a v  a 2s.co  m*/
    String privateHostName = null;
    BasicAWSCredentials cred = new BasicAWSCredentials(accessKeyId, secretKey);
    AmazonEC2Client ec2Client = new AmazonEC2Client(cred);
    try {
        ec2Client.setRegion(Region.getRegion(Regions.fromName(instanceRegion)));
        ArrayList<String> instanceIds = new ArrayList<String>();
        instanceIds.add(instanceID);
        DescribeInstancesRequest req = new DescribeInstancesRequest();
        req.setInstanceIds(instanceIds);
        DescribeInstancesResult result = ec2Client.describeInstances(req);
        Instance instance = result.getReservations().get(0).getInstances().get(0);
        privateHostName = instance.getPrivateDnsName();
    } catch (AmazonServiceException amazonServiceException) {
        logger.info("Error while fecthing instance info from aws " + amazonServiceException.getMessage());
    } catch (Exception exception) {
        logger.info("Error while fecthing instance info from aws " + exception.getMessage());
    }
    return privateHostName;

}

From source file:com.boxupp.dao.AwsProjectDAOManager.java

License:Apache License

public StatusBean authenticateAwsCredentials(JsonNode awsCredentials) {
    StatusBean statusBean = new StatusBean();
    String keyPair = awsCredentials.get("awsKeyPair").asText();
    String privateKeyPath = awsCredentials.get("privateKeyPath").asText();
    try {/*from   ww  w.  java2  s.  co  m*/
        checkIfPrivateFileExists(privateKeyPath);
        BasicAWSCredentials cred = new BasicAWSCredentials(awsCredentials.get("awsAccessKeyId").asText(),
                awsCredentials.get("awsSecretAccessKey").asText());
        AmazonEC2Client ec2Client = new AmazonEC2Client(cred);
        statusBean = validateKeyPair(ec2Client, keyPair);
    } catch (AmazonServiceException amazonServiceException) {
        statusBean.setStatusCode(1);
        statusBean.setStatusMessage("Error while authenticating aws credentials");
        logger.info("invalid aws credentials " + amazonServiceException.getMessage());
    } catch (FileNotFoundException fileNotFoundException) {
        statusBean.setStatusCode(1);
        statusBean.setStatusMessage("Private Key file not found at entered path");
        logger.info("Private Key file not found at entered path " + fileNotFoundException.getMessage());
    } catch (Exception exception) {
        statusBean.setStatusCode(1);
        statusBean.setStatusMessage("Error while authenticating aws credentials");
        logger.info("invalid aws credentials " + exception.getMessage());
    }
    return statusBean;
}

From source file:com.boxupp.dao.AwsProjectDAOManager.java

License:Apache License

public StatusBean validateMachineAmi(JsonNode machineInfo) {
    StatusBean statusBean = new StatusBean();
    String projectID = machineInfo.get("projectID").asText();
    String machineAMI = machineInfo.get("machineAMI").asText();
    String machineRegion = machineInfo.get("machineRegion").asText();
    AwsProjectCredentialsBean awsCredentials = retireveAwsProjectCredentials(projectID);
    BasicAWSCredentials cred = new BasicAWSCredentials(awsCredentials.getAwsAccessKeyId(),
            awsCredentials.getAwsSecretAccessKey());
    AmazonEC2Client ec2Client = new AmazonEC2Client(cred);
    try {// www  .j  av a2  s .c o m
        DescribeImagesRequest imageRequest = new DescribeImagesRequest();
        Collection<String> imageID = new ArrayList<String>();
        imageID.add(machineAMI);
        imageRequest.withImageIds(imageID);
        ec2Client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(machineRegion)));
        DescribeImagesResult imagesResult = ec2Client.describeImages(imageRequest);
        statusBean.setData(null);
        statusBean.setStatusCode(0);
        statusBean.setStatusMessage("AMI Id Validated");
    } catch (Exception exception) {
        statusBean.setData(null);
        statusBean.setStatusCode(1);
        statusBean
                .setStatusMessage("Ami Id " + machineAMI + " doesnot exists in " + machineRegion + " region ");
    }

    return statusBean;
}

From source file:com.brianmcmichael.sagu.SAGU.java

License:Open Source License

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;
}

From source file:com.brianmcmichael.sagu.SAGU.java

License:Open Source License

@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);/*from w  ww .  ja va2s  .  c o m*/
    }
    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 {
                                Thread.sleep(100L); // why?
                            } 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();
        }
    }
}

From source file:com.brianmcmichael.sagu.SimpleGlacierUploader.java

License:Open Source License

public 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;
}