Example usage for com.amazonaws.services.s3.model AmazonS3Exception getErrorCode

List of usage examples for com.amazonaws.services.s3.model AmazonS3Exception getErrorCode

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model AmazonS3Exception getErrorCode.

Prototype

public String getErrorCode() 

Source Link

Document

Returns the AWS error code represented by this exception.

Usage

From source file:awslabs.lab21.SolutionCode.java

License:Open Source License

@Override
public void deleteBucket(AmazonS3 s3Client, String bucketName) {
    // First, try to delete the bucket.
    DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucketName);

    try {/* w  w  w  . j ava 2s.com*/
        s3Client.deleteBucket(deleteBucketRequest);
        // If we got here, no error was generated so we'll assume the bucket was deleted and return.
        return;
    } catch (AmazonS3Exception ex) {
        if (!ex.getErrorCode().equals("BucketNotEmpty")) {
            // The only other exception we're going to handle is BucketNotEmpty, so rethrow anything else.
            throw ex;
        }
    }

    // If we got here, the bucket isn't empty, so delete the contents and try again.
    List<KeyVersion> keys = new ArrayList<KeyVersion>();
    for (S3ObjectSummary obj : s3Client.listObjects(bucketName).getObjectSummaries()) {
        // Add the keys to our list of object.
        keys.add(new KeyVersion(obj.getKey()));
    }
    // Create the request to delete the objects.
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    deleteObjectsRequest.withKeys(keys);
    // Submit the delete objects request.
    s3Client.deleteObjects(deleteObjectsRequest);

    // The bucket is empty now, so attempt the delete again.
    s3Client.deleteBucket(deleteBucketRequest);
}

From source file:awslabs.lab41.Lab41.java

License:Open Source License

public void testS3Client(AmazonS3Client s3Client, String bucketName) {
    String fileName = "test-image.png";

    System.out.print("    Uploading to bucket " + bucketName + ". ");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, new File(fileName));

    try {//from   w  ww . ja  v a 2 s. c o m
        s3Client.putObject(putObjectRequest);
        System.out.println("Succeeded.");
    } catch (AmazonS3Exception ase) {
        System.out.println("Failed. [" + ase.getErrorCode() + "]");

    } catch (Exception ex) {
        System.out.println("Failed.");
    }

}

From source file:awslabs.lab41.SolutionCode.java

License:Open Source License

@Override
public void removeLabBuckets(AmazonS3Client s3Client, List<String> bucketNames) {
    for (String bucketName : bucketNames) {
        try {/* w w  w .  j a  va2 s .co m*/
            ObjectListing objectListing = s3Client
                    .listObjects(new ListObjectsRequest().withBucketName(bucketName));
            for (S3ObjectSummary s3ObjectSummary : objectListing.getObjectSummaries()) {
                DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(
                        s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());

                s3Client.deleteObject(deleteObjectRequest);
            }

            s3Client.deleteBucket(new DeleteBucketRequest(bucketName));
        } catch (AmazonS3Exception s3E) {
            if (!s3E.getErrorCode().equals("NoSuchBucket")) {
                // This error wasn't expected, so rethrow.
                throw s3E;
            }
        }
    }
}

From source file:com.amazon.aws.demo.anonymous.CustomListActivity.java

License:Open Source License

protected void startPopulateList() {
    Thread t = new Thread() {
        @Override/*from  w w  w.j av a2s  .com*/
        public void run() {
            try {
                obtainListItems();
            } catch (AmazonS3Exception e) {
                if ("ExpiredToken".equals(e.getErrorCode())) {
                    putRefreshError();
                }
            } catch (AmazonServiceException e) {
                String errorCode = e.getErrorCode();
                if ("InvalidClientTokenId".equals(errorCode) || "InvalidAccessKeyId".equals(errorCode)
                        || "ExpiredToken".equals(errorCode)) {
                    putRefreshError();
                }
            } catch (Throwable e) {
                setStackAndPost(e);
            }
        }
    };
    t.start();
}

From source file:com.amazon.aws.demo.anonymous.CustomListActivity.java

License:Open Source License

protected void getMoreItems() {
    Thread t = new Thread() {
        @Override//  ww  w  . ja v  a  2  s. c o  m
        public void run() {
            try {
                mHandler.post(postWaitingForMore);
                obtainMoreItems();
            } catch (AmazonS3Exception e) {
                if ("ExpiredToken".equals(e.getErrorCode())) {
                    putRefreshError();
                }
            } catch (AmazonServiceException e) {
                String errorCode = e.getErrorCode();
                if ("InvalidClientTokenId".equals(errorCode) || "InvalidAccessKeyId".equals(errorCode)
                        || "ExpiredToken".equals(errorCode)) {
                    putRefreshError();
                }
            } catch (Throwable e) {
                setStackAndPost(e);
            }
        }
    };
    t.start();
}

From source file:com.amazon.aws.demo.anonymous.s3.S3CreateBucket.java

License:Open Source License

public void wireSubmitButton() {
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override//  w  ww .j a va  2  s  .c  om
        public void onClick(View v) {
            bucketName.setVisibility(View.INVISIBLE);
            try {
                S3.createBucket(bucketName.getText().toString());
                finish();
            } catch (AmazonS3Exception e) {
                if ("ExpiredToken".equals(e.getErrorCode())) {
                    putRefreshError();
                }
            } catch (Throwable e) {
                setStackAndPost(e);
            }
        }
    });
}

From source file:com.carrotgarden.nexus.aws.s3.publish.amazon.AmazonServiceProvider.java

License:BSD License

@Override
public boolean load(final String path, final File file) {

    reporter.requestLoadCount.inc();//w  w w .  ja  v  a2s  .  c  o  m
    reporter.requestTotalCount.inc();

    try {

        final GetObjectRequest request = //
                new GetObjectRequest(mavenBucket(), mavenRepoKey(path));

        final ObjectMetadata result = client.getObject(request, file);

        reporter.fileLoadCount.inc();
        reporter.fileLoadSize.inc(file.length());
        reporter.fileLoadWatch.add(file);

        setAvailable(true, null);

        return true;

    } catch (final AmazonS3Exception e) {

        switch (e.getStatusCode()) {
        case 404:
            log.error("path={} code={}", path, e.getErrorCode());
            break;
        default:
            setAvailable(true, e);
            break;
        }
        return false;

    } catch (final Exception e) {

        setAvailable(false, e);

        return false;

    }

}

From source file:com.clicktravel.infrastructure.persistence.aws.s3.S3FileStore.java

License:Apache License

@Override
public void delete(final FilePath filePath) {
    checkInitialization();//from   w w w .  j a va2s.  c o m
    try {
        amazonS3Client.deleteObject(bucketNameForFilePath(filePath), filePath.filename());
    } catch (final AmazonS3Exception e) {
        if (missingItemErrorCodes.contains(e.getErrorCode())) {
            throw new NonExistentItemException(
                    "Item does not exist" + filePath.directory() + "->" + filePath.filename());
        }
        throw e;
    }
}

From source file:com.cloudbees.demo.beesshop.service.AmazonS3FileStorageService.java

License:Apache License

public void checkConfiguration() throws RuntimeException {
    if (Strings.isNullOrEmpty(amazonS3BucketName)) {
        throw new RuntimeException("Amazon S3 bucket name is not defined");
    }/*from w  w  w . j  a  v  a2  s .c  om*/
    try {
        if (!amazonS3.doesBucketExist(amazonS3BucketName)) {
            throw new RuntimeException("Bucket '" + amazonS3BucketName + "' not found for user '"
                    + awsCredentials.getAWSAccessKeyId() + "'");
        }
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 403 && "SignatureDoesNotMatch".equals(e.getErrorCode())) {
            throw new RuntimeException(
                    "Invalid credentials AWSAccessKeyId='" + awsCredentials.getAWSAccessKeyId()
                            + "', AWSSecretKey=**** to access bucket '" + amazonS3BucketName + "'",
                    e);
        } else {
            throw e;
        }
    }
}

From source file:com.zero_x_baadf00d.play.module.aws.s3.AmazonS3ModuleImpl.java

License:Open Source License

/**
 * Create a simple instance of {@code S3Module}.
 *
 * @param lifecycle     The application life cycle
 * @param configuration The application configuration
 * @since 16.03.13//from w w w  .  j  a  v a2  s .c o  m
 */
@Inject
public AmazonS3ModuleImpl(final ApplicationLifecycle lifecycle, final Configuration configuration) {
    final String accessKey = configuration.getString(AmazonS3ModuleImpl.AWS_ACCESS_KEY);
    final String secretKey = configuration.getString(AmazonS3ModuleImpl.AWS_SECRET_KEY);
    this.s3Bucket = configuration.getString(AmazonS3ModuleImpl.AWS_S3_BUCKET);
    if ((accessKey != null) && (secretKey != null) && (this.s3Bucket != null)) {
        final AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        this.amazonS3 = new AmazonS3Client(awsCredentials);
        this.amazonS3.setEndpoint(configuration.getString(AmazonS3ModuleImpl.AWS_ENDPOINT));
        if (configuration.getBoolean(AmazonS3ModuleImpl.AWS_WITHPATHSTYLE, false)) {
            this.amazonS3.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
        }
        try {
            this.amazonS3.createBucket(this.s3Bucket);
        } catch (AmazonS3Exception e) {
            if (e.getErrorCode().compareTo("BucketAlreadyOwnedByYou") != 0
                    && e.getErrorCode().compareTo("AccessDenied") != 0) {
                throw e;
            }
        } finally {
            Logger.info("Using S3 Bucket: " + this.s3Bucket);
        }
    } else {
        throw new RuntimeException("S3Module is not properly configured");
    }
    lifecycle.addStopHook(() -> CompletableFuture.completedFuture(null));
}