Java Code Examples for com.amazonaws.AmazonServiceException#getStatusCode()

The following examples show how to use com.amazonaws.AmazonServiceException#getStatusCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlueTestClientFactory.java    From aws-glue-data-catalog-client-for-apache-hive-metastore with Apache License 2.0 7 votes vote down vote up
private static ClientConfiguration createGatewayTimeoutRetryableConfiguration() {
  ClientConfiguration retryableConfig = new ClientConfiguration();
  RetryPolicy.RetryCondition retryCondition = new PredefinedRetryPolicies.SDKDefaultRetryCondition() {
    @Override
    public boolean shouldRetry(AmazonWebServiceRequest originalRequest, AmazonClientException exception,
                               int retriesAttempted) {
      if (super.shouldRetry(originalRequest, exception, retriesAttempted)) {
        return true;
      }
      if (exception != null && exception instanceof AmazonServiceException) {
        AmazonServiceException ase = (AmazonServiceException) exception;
        if (ase.getStatusCode() == SC_GATEWAY_TIMEOUT) {
          return true;
        }
      }
      return false;
    }
  };
  RetryPolicy retryPolicy = new RetryPolicy(retryCondition, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY,
                                                   PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY, true);
  retryableConfig.setRetryPolicy(retryPolicy);
  return retryableConfig;
}
 
Example 2
Source File: COSAPIClient.java    From stocator with Apache License 2.0 6 votes vote down vote up
private void initMultipartUploads(Configuration conf) throws IOException {
  boolean purgeExistingMultipart = Utils.getBoolean(conf, FS_COS, FS_ALT_KEYS,
      PURGE_EXISTING_MULTIPART,
      DEFAULT_PURGE_EXISTING_MULTIPART);
  long purgeExistingMultipartAge = Utils.getLong(conf, FS_COS, FS_ALT_KEYS,
      PURGE_EXISTING_MULTIPART_AGE, DEFAULT_PURGE_EXISTING_MULTIPART_AGE);

  if (purgeExistingMultipart) {
    Date purgeBefore =
        new Date(new Date().getTime() - purgeExistingMultipartAge * 1000);

    try {
      transfers.abortMultipartUploads(mBucket, purgeBefore);
    } catch (AmazonServiceException e) {
      if (e.getStatusCode() == 403) {
        LOG.debug("Failed to purging multipart uploads against {},"
            + " FS may be read only", mBucket, e);
      } else {
        throw translateException("purging multipart uploads", mBucket, e);
      }
    }
  }
}
 
Example 3
Source File: S3DaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectMetadata getObjectMetadata(final S3FileTransferRequestParamsDto params)
{
    AmazonS3Client s3Client = getAmazonS3(params);

    try
    {
        return s3Operations.getObjectMetadata(params.getS3BucketName(), params.getS3KeyPrefix(), s3Client);
    }
    catch (AmazonServiceException e)
    {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND)
        {
            return null;
        }

        throw new IllegalStateException(String
            .format("Failed to get S3 metadata for object key \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(),
                e.getMessage()), e);
    }
    finally
    {
        // Shutdown the AmazonS3Client instance to release resources.
        s3Client.shutdown();
    }
}
 
Example 4
Source File: COSAPIClient.java    From stocator with Apache License 2.0 6 votes vote down vote up
@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
  String key = pathToKey(path);
  LOG.debug("Object name to delete {}. Path {}", key, path.toString());
  try {
    mClient.deleteObject(new DeleteObjectRequest(mBucket, key));
    memoryCache.removeFileStatus(path.toString());
    return true;
  } catch (AmazonServiceException e) {
    if (e.getStatusCode() != 404) {
      throw new IOException(e);
    }
  }
  LOG.warn("Delete on {} not found. Nothing to delete");
  return false;
}
 
Example 5
Source File: S3.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
/** Test whether the bucket exists and is accessible. */
public static boolean bucketExists(AmazonS3 client, String bucketName) {
    try {
        HeadBucketRequest request = new HeadBucketRequest(bucketName);
        HeadBucketResult result = client.headBucket(request);
        return true;
    }
    catch (AmazonServiceException awsEx) {
        switch (awsEx.getStatusCode()) {
            case HttpSC.NOT_FOUND_404 :
                return false;
            case HttpSC.FORBIDDEN_403 :
                break;
            case HttpSC.MOVED_PERMANENTLY_301 : { // Moved permanently.
                System.err.println("301 Location: " + awsEx.getHttpHeaders().get(HttpNames.hLocation));
                break;
            }
        }
        throw awsEx;
    }
}
 
Example 6
Source File: PatchStorageS3.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Override
public RDFPatch fetch(Id key) {
    String s3Key = idToKey(key);
    try {
        S3Object x = client.getObject(bucketName, s3Key);
        x.getObjectMetadata();
        S3ObjectInputStream input = x.getObjectContent();
        RDFPatch patch = RDFPatchOps.read(input);
        return patch;
    }
    catch (AmazonServiceException awsEx) {
        switch (awsEx.getStatusCode()) {
            case HttpSC.NOT_FOUND_404 :
            case HttpSC.FORBIDDEN_403 :
                return null;
            case HttpSC.MOVED_PERMANENTLY_301 : { // Moved permanently.
                System.err.println("301 Location: " + awsEx.getHttpHeaders().get(HttpNames.hLocation));
                return null;
            }
        }
        throw awsEx;
    }

}
 
Example 7
Source File: S3Backuper.java    From cassandra-backup with Apache License 2.0 6 votes vote down vote up
@Override
public FreshenResult freshenRemoteObject(final RemoteObjectReference object) throws InterruptedException {
    final String canonicalPath = ((S3RemoteObjectReference) object).canonicalPath;

    final CopyObjectRequest copyRequest = new CopyObjectRequest(request.storageLocation.bucket,
                                                                canonicalPath,
                                                                request.storageLocation.bucket,
                                                                canonicalPath).withStorageClass(StorageClass.Standard);

    try {
        // attempt to refresh existing object in the bucket via an inplace copy
        transferManager.copy(copyRequest).waitForCompletion();
        return FreshenResult.FRESHENED;

    } catch (final AmazonServiceException e) {
        // AWS S3 under certain access policies can't return NoSuchKey (404)
        // instead, it returns AccessDenied (403) — handle it the same way
        if (e.getStatusCode() != 404 && e.getStatusCode() != 403) {
            throw e;
        }

        // the freshen failed because the file/key didn't exist
        return FreshenResult.UPLOAD_REQUIRED;
    }
}
 
Example 8
Source File: StorageProviderException.java    From snowflake-jdbc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if this is an exception corresponding to a HTTP 404 error
 * returned by the storage provider
 *
 * @return true if the specified exception is an AmazonServiceException
 * instance and if it was thrown because of a 404, false otherwise.
 */
public boolean isServiceException404()
{
  if ((Exception) this instanceof AmazonServiceException)
  {
    AmazonServiceException asEx = (AmazonServiceException) ((java.lang.Exception) this);
    return (asEx.getStatusCode() == HttpStatus.SC_NOT_FOUND);
  }

  return false;
}
 
Example 9
Source File: AmazonServiceExceptionMappingService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BackgroundException map(final AmazonClientException e) {
    final StringBuilder buffer = new StringBuilder();
    if(e instanceof AmazonServiceException) {
        final AmazonServiceException failure = (AmazonServiceException) e;
        this.append(buffer, failure.getErrorMessage());
        switch(failure.getStatusCode()) {
            case HttpStatus.SC_BAD_REQUEST:
                switch(failure.getErrorCode()) {
                    case "Throttling":
                        return new RetriableAccessDeniedException(buffer.toString(), e);
                    case "AccessDeniedException":
                        return new AccessDeniedException(buffer.toString(), e);
                    case "UnrecognizedClientException":
                        return new LoginFailureException(buffer.toString(), e);
                }
            case HttpStatus.SC_FORBIDDEN:
                switch(failure.getErrorCode()) {
                    case "SignatureDoesNotMatch":
                        return new LoginFailureException(buffer.toString(), e);
                    case "InvalidAccessKeyId":
                        return new LoginFailureException(buffer.toString(), e);
                    case "InvalidClientTokenId":
                        return new LoginFailureException(buffer.toString(), e);
                    case "InvalidSecurity":
                        return new LoginFailureException(buffer.toString(), e);
                    case "MissingClientTokenId":
                        return new LoginFailureException(buffer.toString(), e);
                    case "MissingAuthenticationToken":
                        return new LoginFailureException(buffer.toString(), e);
                }
        }
        return new DefaultHttpResponseExceptionMappingService().map(new HttpResponseException(failure.getStatusCode(), buffer.toString()));
    }
    this.append(buffer, e.getMessage());
    return this.wrap(e, buffer);
}
 
Example 10
Source File: S3StorageService.java    From kayenta with Apache License 2.0 5 votes vote down vote up
/** Check to see if the bucket exists, creating it if it is not there. */
public void ensureBucketExists(String accountName) {
  AwsNamedAccountCredentials credentials =
      accountCredentialsRepository.getRequiredOne(accountName);

  AmazonS3 amazonS3 = credentials.getAmazonS3();
  String bucket = credentials.getBucket();
  String region = credentials.getRegion();

  HeadBucketRequest request = new HeadBucketRequest(bucket);

  try {
    amazonS3.headBucket(request);
  } catch (AmazonServiceException e) {
    if (e.getStatusCode() == 404) {
      if (com.amazonaws.util.StringUtils.isNullOrEmpty(region)) {
        log.warn("Bucket {} does not exist. Creating it in default region.", bucket);
        amazonS3.createBucket(bucket);
      } else {
        log.warn("Bucket {} does not exist. Creating it in region {}.", bucket, region);
        amazonS3.createBucket(bucket, region);
      }
    } else {
      log.error("Could not create bucket {}: {}", bucket, e);
      throw e;
    }
  }
}
 
Example 11
Source File: AwsServiceHelper.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Handles the AmazonServiceException, throws corresponding exception based on status code in amazon exception.
 *
 * @param amazonServiceException the AWS exception that will be handled by this method.
 * @param message the message to include with this exception.
 *
 * @throws IllegalArgumentException the exception to be thrown with a HttpStatus.SC_BAD_REQUEST
 * @throws ObjectNotFoundException the exception to be thrown with a HttpStatus.SC_NOT_FOUND
 */
public void handleAmazonException(AmazonServiceException amazonServiceException, String message) throws IllegalArgumentException, ObjectNotFoundException
{
    if (amazonServiceException.getStatusCode() == HttpStatus.SC_BAD_REQUEST)
    {
        throw new IllegalArgumentException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException);
    }
    else if (amazonServiceException.getStatusCode() == HttpStatus.SC_NOT_FOUND)
    {
        throw new ObjectNotFoundException(message + " Reason: " + amazonServiceException.getMessage(), amazonServiceException);
    }

    throw amazonServiceException;
}
 
Example 12
Source File: Aws.java    From digdag with Apache License 2.0 5 votes vote down vote up
static boolean isDeterministicException(AmazonServiceException ex)
{
    int statusCode = ex.getStatusCode();
    switch (statusCode) {
        case HttpStatus.TOO_MANY_REQUESTS_429:
        case HttpStatus.REQUEST_TIMEOUT_408:
            return false;
        default:
            return statusCode >= 400 && statusCode < 500;
    }
}
 
Example 13
Source File: S3StorageService.java    From front50 with Apache License 2.0 5 votes vote down vote up
@Override
public void ensureBucketExists() {
  HeadBucketRequest request = new HeadBucketRequest(bucket);
  try {
    amazonS3.headBucket(request);
  } catch (AmazonServiceException e) {
    if (e.getStatusCode() == 404) {
      if (StringUtils.isNullOrEmpty(region)) {
        log.info("Creating bucket {} in default region", value("bucket", bucket));
        amazonS3.createBucket(bucket);
      } else {
        log.info(
            "Creating bucket {} in region {}", value("bucket", bucket), value("region", region));
        amazonS3.createBucket(bucket, region);
      }

      if (versioning) {
        log.info("Enabling versioning of the S3 bucket {}", value("bucket", bucket));
        BucketVersioningConfiguration configuration =
            new BucketVersioningConfiguration().withStatus("Enabled");

        SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest =
            new SetBucketVersioningConfigurationRequest(bucket, configuration);

        amazonS3.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);
      }

    } else {
      throw e;
    }
  }
}
 
Example 14
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Set<CloudAccessConfig> getAccessConfigByInstanceProfile(AmazonIdentityManagement client) {
    LOGGER.info("Get all Instance profiles from Amazon");
    String queryFailedMessage = "Could not get instance profiles from Amazon: ";
    try {
        boolean finished = false;
        String marker = null;
        Set<InstanceProfile> instanceProfiles = new LinkedHashSet<>();
        while (!finished) {
            ListInstanceProfilesRequest listInstanceProfilesRequest = new ListInstanceProfilesRequest();
            listInstanceProfilesRequest.setMaxItems(fetchMaxItems);
            if (isNotEmpty(marker)) {
                listInstanceProfilesRequest.setMarker(marker);
            }
            LOGGER.debug("About to fetch instance profiles...");
            ListInstanceProfilesResult listInstanceProfilesResult = client.listInstanceProfiles(listInstanceProfilesRequest);
            List<InstanceProfile> fetchedInstanceProfiles = listInstanceProfilesResult.getInstanceProfiles();
            instanceProfiles.addAll(fetchedInstanceProfiles);
            if (listInstanceProfilesResult.isTruncated()) {
                marker = listInstanceProfilesResult.getMarker();
            } else {
                finished = true;
            }
        }
        LOGGER.debug("The total of {} instance profile(s) has fetched.", instanceProfiles.size());
        return instanceProfiles.stream().map(this::instanceProfileToCloudAccessConfig).collect(Collectors.toSet());
    } catch (AmazonServiceException ase) {
        if (ase.getStatusCode() == UNAUTHORIZED) {
            LOGGER.error("Could not get instance profiles because the user does not have enough permission.", ase);
            throw new CloudConnectorException(ase.getMessage(), ase);
        } else {
            LOGGER.info(queryFailedMessage, ase);
            throw new CloudConnectorException(ase.getMessage(), ase);
        }
    } catch (Exception e) {
        LOGGER.warn(queryFailedMessage, e);
        throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e);
    }
}
 
Example 15
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Set<CloudAccessConfig> getAccessConfigByRole(AmazonIdentityManagement client) {
    LOGGER.info("Get all Roles from Amazon");
    String queryFailedMessage = "Could not get roles from Amazon: ";
    try {
        boolean finished = false;
        String marker = null;
        List<Role> roles = new LinkedList<>();
        while (!finished) {
            ListRolesRequest listRolesRequest = new ListRolesRequest();
            listRolesRequest.setMaxItems(fetchMaxItems);
            if (isNotEmpty(marker)) {
                listRolesRequest.setMarker(marker);
            }
            LOGGER.debug("About to fetch roles...");
            ListRolesResult listRolesResult = client.listRoles(listRolesRequest);
            roles.addAll(listRolesResult.getRoles());
            if (listRolesResult.isTruncated()) {
                marker = listRolesResult.getMarker();
            } else {
                finished = true;
            }
        }
        return roles.stream().map(this::roleToCloudAccessConfig).collect(Collectors.toSet());
    } catch (AmazonServiceException ase) {
        if (ase.getStatusCode() == UNAUTHORIZED) {
            String policyMessage = "Could not get roles because the user does not have enough permission. ";
            LOGGER.error(policyMessage + ase.getMessage(), ase);
            throw new CloudUnauthorizedException(ase.getErrorMessage(), ase);
        } else {
            LOGGER.info(queryFailedMessage + ase.getMessage(), ase);
            throw new CloudConnectorException(ase.getMessage(), ase);
        }
    } catch (Exception e) {
        LOGGER.warn(queryFailedMessage + e.getMessage(), e);
        throw new CloudConnectorException(e.getMessage(), e);
    }
}
 
Example 16
Source File: AmazonSQSMessagingClientWrapper.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Create generic error message for <code>AmazonServiceException</code>. Message include
 * Action, RequestId, HTTPStatusCode, and AmazonErrorCode.
 */
private String logAndGetAmazonServiceException(AmazonServiceException ase, String action) {
    String errorMessage = "AmazonServiceException: " + action + ". RequestId: " + ase.getRequestId() +
                          "\nHTTPStatusCode: " + ase.getStatusCode() + " AmazonErrorCode: " +
                          ase.getErrorCode();
    LOG.error(errorMessage, ase);
    return errorMessage;
}
 
Example 17
Source File: S3Client.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean hasKey(String bucketName, String key) throws AmazonClientException, AmazonServiceException {
  try {
    s3Client.getObjectMetadata(bucketName, key); 
  } catch(AmazonServiceException e) {
    if(e.getStatusCode() == 404) return false;
    throw e ;
  }
  return true;
}
 
Example 18
Source File: TracingHandler.java    From aws-xray-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public void afterError(Request<?> request, Response<?> response, Exception e) {
    if (isSubsegmentDuplicate(recorder.getCurrentSubsegmentOptional(), request)) {
        Optional<Subsegment> currentSubsegmentOptional = recorder.getCurrentSubsegmentOptional();
        if (!currentSubsegmentOptional.isPresent()) {
            return;
        }
        Subsegment currentSubsegment = currentSubsegmentOptional.get();

        int statusCode = -1;

        if (null != response) {
            statusCode = response.getHttpResponse().getStatusCode();
        } else {
            if (e instanceof AmazonServiceException) {
                AmazonServiceException ase = (AmazonServiceException) e;
                statusCode = ase.getStatusCode();
                // The S3 client will throw and re-swallow AmazonServiceExceptions if they have these status codes. Customers
                // will never see the exceptions in their application code but they still travel through our
                // TracingHandler#afterError method. We special case these status codes in order to prevent addition of the
                // full exception object to the current subsegment. Instead, we'll just add any exception error message to the
                // current subsegment's cause's message.
                if ((304 == statusCode || 412 == statusCode) && S3_SERVICE_NAME.equals(ase.getServiceName())) {
                    populateAndEndSubsegment(currentSubsegment, request, response, ase);
                    return;
                }
                if (RetryUtils.isThrottlingException(ase)) {
                    currentSubsegment.setError(true);
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        if (-1 != statusCode) {
            int statusCodePrefix = statusCode / 100;
            if (4 == statusCodePrefix) {
                currentSubsegment.setError(true);
                if (429 == statusCode) {
                    currentSubsegment.setThrottle(true);
                }
            }
        }

        currentSubsegment.addException(e);

        if (e instanceof AmazonServiceException) {
            populateAndEndSubsegment(currentSubsegment, request, response, (AmazonServiceException) e);
        } else {
            populateAndEndSubsegment(currentSubsegment, request, response);
        }
    }
}
 
Example 19
Source File: ThreddsS3ClientImpl.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ObjectListing listObjects(S3URI s3uri) {
  ListObjectsRequest listObjectsRequest =
      new ListObjectsRequest().withBucketName(s3uri.getBucket()).withDelimiter(S3URI.S3_DELIMITER);

  if (s3uri.getKey() != null) {
    listObjectsRequest.setPrefix(s3uri.getKeyWithTrailingDelimiter());
  }

  try {
    ObjectListing objectListing = s3Client.listObjects(listObjectsRequest);
    logger.info(String.format("Downloaded S3 listing '%s'", s3uri));

    // On S3 it is possible for a prefix to be a valid object (unlike a
    // filesystem where a node is either a file OR a directory). We
    // exclude self here so that the "directory" isn't treated as a file
    // by some of the caching mechanism.
    S3ObjectSummary self = null;
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
      if (Objects.equals(objectSummary.getKey(), s3uri.getKeyWithTrailingDelimiter())) {
        self = objectSummary;
        break;
      }
    }
    objectListing.getObjectSummaries().remove(self);

    if (objectListing.getObjectSummaries().isEmpty() && objectListing.getCommonPrefixes().isEmpty()) {
      // There are no empty directories in a S3 hierarchy.
      logger.debug(String.format("In bucket '%s', the key '%s' does not denote an existing virtual directory.",
          s3uri.getBucket(), s3uri.getKey()));
      return null;
    } else {
      return objectListing;
    }
  } catch (AmazonServiceException e) {
    if (e.getStatusCode() == 404) {
      logger.debug(String.format("No S3 bucket named '%s' exists.", s3uri.getBucket()));
      return null;
    } else {
      throw e;
    }
  }
}
 
Example 20
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private boolean networkDoesNotExist(AmazonServiceException e) {
    return e.getStatusCode() == NOT_FOUND && e.getErrorMessage().contains("does not exist");
}