Search in sources :

Example 66 with AmazonClientException

use of com.amazonaws.AmazonClientException in project aws-doc-sdk-examples by awsdocs.

the class StepFunctionsSample method main.

public static void main(String[] args) throws Exception {
    /*
         * The ProfileCredentialsProvider will return your [default]
         * credential profile by reading from the credentials file located at
         * (~/.aws/credentials).
         *
         * It is possible to use another profile with:
         *  credentialsProvider = new ProfileCredentialsProvider("your-profile")
         */
    ProfileCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
    try {
        credentialsProvider.getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles " + "file. Please make sure that your credentials file is " + "at the correct location (~/.aws/credentials), and is " + "in valid format.", e);
    }
    Regions region = Regions.US_EAST_1;
    AWSStepFunctions sfnClient = AWSStepFunctionsClientBuilder.standard().withCredentials(credentialsProvider).withRegion(region).build();
    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon Step Functions");
    System.out.println("===========================================\n");
    try {
        System.out.println("Listing state machines");
        ListStateMachinesResult listStateMachinesResult = sfnClient.listStateMachines(new ListStateMachinesRequest());
        List<StateMachineListItem> stateMachines = listStateMachinesResult.getStateMachines();
        System.out.println("State machines count: " + stateMachines.size());
        if (!stateMachines.isEmpty()) {
            stateMachines.forEach(sm -> {
                System.out.println("\t- Name: " + sm.getName());
                System.out.println("\t- Arn: " + sm.getStateMachineArn());
                ListExecutionsRequest listRequest = new ListExecutionsRequest().withStateMachineArn(sm.getStateMachineArn());
                ListExecutionsResult listExecutionsResult = sfnClient.listExecutions(listRequest);
                List<ExecutionListItem> executions = listExecutionsResult.getExecutions();
                System.out.println("\t- Total: " + executions.size());
                executions.forEach(ex -> {
                    System.out.println("\t\t-Start: " + ex.getStartDate());
                    System.out.println("\t\t-Stop: " + ex.getStopDate());
                    System.out.println("\t\t-Name: " + ex.getName());
                    System.out.println("\t\t-Status: " + ex.getStatus());
                    System.out.println();
                });
            });
        }
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means" + " your request made it to Amazon Step Functions, but was" + " rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means " + "the client encountered a serious internal problem while " + "trying to communicate with Step Functions, such as not " + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}
Also used : ListStateMachinesRequest(com.amazonaws.services.stepfunctions.model.ListStateMachinesRequest) AmazonClientException(com.amazonaws.AmazonClientException) Regions(com.amazonaws.regions.Regions) ListStateMachinesResult(com.amazonaws.services.stepfunctions.model.ListStateMachinesResult) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) ListExecutionsResult(com.amazonaws.services.stepfunctions.model.ListExecutionsResult) AWSStepFunctions(com.amazonaws.services.stepfunctions.AWSStepFunctions) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) StateMachineListItem(com.amazonaws.services.stepfunctions.model.StateMachineListItem) ExecutionListItem(com.amazonaws.services.stepfunctions.model.ExecutionListItem) ListExecutionsRequest(com.amazonaws.services.stepfunctions.model.ListExecutionsRequest)

Example 67 with AmazonClientException

use of com.amazonaws.AmazonClientException 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 68 with AmazonClientException

use of com.amazonaws.AmazonClientException in project crate by crate.

the class MockAmazonS3 method putObject.

@Override
public PutObjectResult putObject(final PutObjectRequest request) throws AmazonClientException {
    assertThat(request.getBucketName(), equalTo(bucket));
    assertThat(request.getMetadata().getSSEAlgorithm(), serverSideEncryption ? equalTo("AES256") : nullValue());
    assertThat(request.getCannedAcl(), notNullValue());
    assertThat(request.getCannedAcl().toString(), cannedACL != null ? equalTo(cannedACL) : equalTo("private"));
    assertThat(request.getStorageClass(), storageClass != null ? equalTo(storageClass) : equalTo("STANDARD"));
    final String blobName = request.getKey();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        Streams.copy(request.getInputStream(), out);
        blobs.put(blobName, out.toByteArray());
    } catch (IOException e) {
        throw new AmazonClientException(e);
    }
    return new PutObjectResult();
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) AmazonClientException(com.amazonaws.AmazonClientException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 69 with AmazonClientException

use of com.amazonaws.AmazonClientException in project SAGU by brianmcmichael.

the class DeleteArchiveFrame method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtDelete) {
        if ((jtfDeleteField.getText().trim().equals(""))) {
            JOptionPane.showMessageDialog(null, "Enter the Archive ID of the file to be deleted.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                String archiveId = jtfDeleteField.getText().trim();
                // Banish the extra chars printed in early logs.
                String sendThis = archiveId.replaceAll("[^\\p{Print}]", "");
                String vaultName = deleteVault;
                // Delete the archive.
                deleteClient.deleteArchive(new DeleteArchiveRequest().withVaultName(vaultName).withArchiveId(sendThis));
                JOptionPane.showMessageDialog(null, "Deleted archive successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
            } catch (AmazonServiceException k) {
                JOptionPane.showMessageDialog(null, "The server returned an error. Wait 24 hours after submitting an archive to attempt a delete. Also check that correct location of archive has been set on the previous page.", "Error", JOptionPane.ERROR_MESSAGE);
                System.out.println("" + k);
            } catch (AmazonClientException i) {
                JOptionPane.showMessageDialog(null, "Client Error. Check that all fields are correct. Archive not deleted.", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (Exception j) {
                JOptionPane.showMessageDialog(null, "Archive not deleted. Unspecified Error.", "Error", JOptionPane.ERROR_MESSAGE);
            }
            jtfDeleteField.setText("");
            jtfDeleteField.requestFocus();
        }
    } else if (e.getSource() == jbtBack) {
        this.setVisible(false);
        dispose();
    } else {
        JOptionPane.showMessageDialog(this, "Please choose a valid action.");
    }
}
Also used : DeleteArchiveRequest(com.amazonaws.services.glacier.model.DeleteArchiveRequest) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException)

Example 70 with AmazonClientException

use of com.amazonaws.AmazonClientException in project SAGU by brianmcmichael.

the class InventoryRequest method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtInventoryRequest) {
        SwingWorker<Object, Void> inventoryWorker = new SwingWorker<Object, Void>() {

            @Override
            protected Object doInBackground() throws Exception {
                // Create dumb progressbar
                Date d = new Date();
                JFrame inventoryFrame = new JFrame("Waiting for inventory");
                {
                    inventoryFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(d);
                    cal.add(Calendar.MINUTE, 250);
                    String doneString = cal.getTime().toString();
                    JLabel doneTimeLabel = new JLabel("<html><body>Inventory of vault " + irVault + " requested.<br>Estimated completion by " + doneString + "</html></body>");
                    final JProgressBar dumJProgressBar = new JProgressBar(JProgressBar.HORIZONTAL);
                    dumJProgressBar.setIndeterminate(true);
                    inventoryFrame.add(dumJProgressBar, BorderLayout.NORTH);
                    inventoryFrame.add(doneTimeLabel, BorderLayout.CENTER);
                    inventoryFrame.setBackground(wc);
                    inventoryFrame.setSize(300, 60);
                }
                centerDefineFrame(inventoryFrame, 500, 100);
                inventoryFrame.setVisible(true);
                try {
                    JobParameters jParameters = new JobParameters().withType("inventory-retrieval");
                    InitiateJobRequest initJobRequest = new InitiateJobRequest().withVaultName(irVault).withJobParameters(jParameters);
                    InitiateJobResult initJobResult = irClient.initiateJob(initJobRequest);
                    String thisJobId = initJobResult.getJobId();
                    Thread.sleep(12600000);
                    Boolean success = waitForJob(irClient, irVault, thisJobId);
                    while (!success) {
                        Thread.sleep(WAIT_TIME);
                        success = waitForJob(irClient, irVault, thisJobId);
                    }
                    GetJobOutputRequest gjoRequest = new GetJobOutputRequest().withVaultName(irVault).withJobId(thisJobId);
                    GetJobOutputResult gjoResult = irClient.getJobOutput(gjoRequest);
                    Format formatter = new SimpleDateFormat("yyyyMMMdd_HHmmss");
                    String fileDate = formatter.format(d);
                    String fileName = irVault + fileDate + ".txt";
                    String filePath = "" + CUR_DIR + System.getProperty("file.separator") + fileName;
                    FileWriter fileStream = new FileWriter(filePath);
                    BufferedWriter out = new BufferedWriter(fileStream);
                    BufferedReader in = new BufferedReader(new InputStreamReader(gjoResult.getBody()));
                    String inputLine;
                    while ((inputLine = in.readLine()) != null) {
                        out.write(inputLine);
                    }
                    out.close();
                    inventoryFrame.setVisible(false);
                    JOptionPane.showMessageDialog(null, "Successfully exported " + irVault + " inventory to " + filePath.toString(), "Saved", JOptionPane.INFORMATION_MESSAGE);
                    return null;
                } catch (AmazonServiceException k) {
                    JOptionPane.showMessageDialog(null, "The server returned an error. Files will not be inventoried for 24 hours after upload. Also check that correct location of vault has been set on the previous page.", "Error", JOptionPane.ERROR_MESSAGE);
                    System.out.println("" + k);
                    inventoryFrame.setVisible(false);
                } catch (AmazonClientException i) {
                    JOptionPane.showMessageDialog(null, "Client Error. Check that all fields are correct. Inventory not requested.", "Error", JOptionPane.ERROR_MESSAGE);
                    inventoryFrame.setVisible(false);
                } catch (Exception j) {
                    JOptionPane.showMessageDialog(null, "Inventory not found. Unspecified Error.", "Error", JOptionPane.ERROR_MESSAGE);
                    inventoryFrame.setVisible(false);
                }
                return null;
            }
        };
        inventoryWorker.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 : FileWriter(java.io.FileWriter) AmazonClientException(com.amazonaws.AmazonClientException) BufferedWriter(java.io.BufferedWriter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) InputStreamReader(java.io.InputStreamReader) Calendar(java.util.Calendar) Date(java.util.Date) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) BufferedReader(java.io.BufferedReader) AmazonServiceException(com.amazonaws.AmazonServiceException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)202 IOException (java.io.IOException)70 AmazonServiceException (com.amazonaws.AmazonServiceException)32 ArrayList (java.util.ArrayList)32 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)23 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)19 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)17 HashMap (java.util.HashMap)16 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)14 Test (org.junit.Test)14 SienaException (siena.SienaException)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)11 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)11 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 InterruptedIOException (java.io.InterruptedIOException)10 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)9 File (java.io.File)9