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

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

Introduction

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

Prototype

public int getStatusCode() 

Source Link

Document

Returns the HTTP status code that was returned with this service exception.

Usage

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();/*from w  ww . ja v  a2  s .  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.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  a 2  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.emc.ecs.sync.target.S3Target.java

License:Open Source License

@Override
public void filter(SyncObject obj) {
    try {/*  w w w .j  av  a 2  s.  c o  m*/
        // skip the root of the bucket since it obviously exists
        if ("".equals(rootKey + obj.getRelativePath())) {
            log.debug("Target is bucket root; skipping");
            return;
        }

        // some sync objects lazy-load their metadata (i.e. AtmosSyncObject)
        // since this may be a timed operation, ensure it loads outside of other timed operations
        if (!(obj instanceof S3ObjectVersion) || !((S3ObjectVersion) obj).isDeleteMarker())
            obj.getMetadata();

        // Compute target key
        String targetKey = getTargetKey(obj);
        obj.setTargetIdentifier(AwsS3Util.fullPath(bucketName, targetKey));

        if (includeVersions) {
            ListIterator<S3ObjectVersion> sourceVersions = s3Source.versionIterator((S3SyncObject) obj);
            ListIterator<S3ObjectVersion> targetVersions = versionIterator(obj);

            boolean newVersions = false, replaceVersions = false;
            if (force) {
                replaceVersions = true;
            } else {

                // special workaround for bug where objects are listed, but they have no versions
                if (sourceVersions.hasNext()) {

                    // check count and etag/delete-marker to compare version chain
                    while (sourceVersions.hasNext()) {
                        S3ObjectVersion sourceVersion = sourceVersions.next();

                        if (targetVersions.hasNext()) {
                            S3ObjectVersion targetVersion = targetVersions.next();

                            if (sourceVersion.isDeleteMarker()) {

                                if (!targetVersion.isDeleteMarker())
                                    replaceVersions = true;
                            } else {

                                if (targetVersion.isDeleteMarker())
                                    replaceVersions = true;

                                else if (!sourceVersion.getETag().equals(targetVersion.getETag()))
                                    replaceVersions = true; // different checksum
                            }

                        } else if (!replaceVersions) { // source has new versions, but existing target versions are ok
                            newVersions = true;
                            sourceVersions.previous(); // back up one
                            putIntermediateVersions(sourceVersions, targetKey); // add any new intermediary versions (current is added below)
                        }
                    }

                    if (targetVersions.hasNext())
                        replaceVersions = true; // target has more versions

                    if (!newVersions && !replaceVersions) {
                        log.info("Source and target versions are the same. Skipping {}", obj.getRelativePath());
                        return;
                    }
                }
            }

            // something's off; must delete all versions of the object
            if (replaceVersions) {
                log.info(
                        "[{}]: version history differs between source and target; re-placing target version history with that from source.",
                        obj.getRelativePath());

                // collect versions in target
                List<DeleteObjectsRequest.KeyVersion> deleteVersions = new ArrayList<>();
                while (targetVersions.hasNext())
                    targetVersions.next(); // move cursor to end
                while (targetVersions.hasPrevious()) { // go in reverse order
                    S3ObjectVersion version = targetVersions.previous();
                    deleteVersions.add(new DeleteObjectsRequest.KeyVersion(targetKey, version.getVersionId()));
                }

                // batch delete all versions in target
                log.debug("[{}]: deleting all versions in target", obj.getRelativePath());
                s3.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(deleteVersions));

                // replay version history in target
                while (sourceVersions.hasPrevious())
                    sourceVersions.previous(); // move cursor to beginning
                putIntermediateVersions(sourceVersions, targetKey);
            }

        } else { // normal sync (no versions)
            Date sourceLastModified = obj.getMetadata().getModificationTime();
            long sourceSize = obj.getMetadata().getContentLength();

            // Get target metadata.
            ObjectMetadata destMeta = null;
            try {
                destMeta = s3.getObjectMetadata(bucketName, targetKey);
            } catch (AmazonS3Exception e) {
                if (e.getStatusCode() != 404)
                    throw new RuntimeException("Failed to check target key '" + targetKey + "' : " + e, e);
            }

            if (!force && obj.getFailureCount() == 0 && destMeta != null) {

                // Check overwrite
                Date destLastModified = destMeta.getLastModified();
                long destSize = destMeta.getContentLength();

                if (destLastModified.equals(sourceLastModified) && sourceSize == destSize) {
                    log.info("Source and target the same.  Skipping {}", obj.getRelativePath());
                    return;
                }
                if (destLastModified.after(sourceLastModified)) {
                    log.info("Target newer than source.  Skipping {}", obj.getRelativePath());
                    return;
                }
            }
        }

        // at this point we know we are going to write the object
        // Put [current object version]
        if (obj instanceof S3ObjectVersion && ((S3ObjectVersion) obj).isDeleteMarker()) {

            // object has version history, but is currently deleted
            log.debug("[{}]: deleting object in target to replicate delete marker in source.",
                    obj.getRelativePath());
            s3.deleteObject(bucketName, targetKey);
        } else {
            putObject(obj, targetKey);

            // if object has new metadata after the stream (i.e. encryption checksum), we must update S3 again
            if (obj.requiresPostStreamMetadataUpdate()) {
                log.debug("[{}]: updating metadata after sync as required", obj.getRelativePath());
                CopyObjectRequest cReq = new CopyObjectRequest(bucketName, targetKey, bucketName, targetKey);
                cReq.setNewObjectMetadata(AwsS3Util.s3MetaFromSyncMeta(obj.getMetadata()));
                s3.copyObject(cReq);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to store object: " + e, e);
    }
}

From source file:com.facebook.presto.hive.PrestoS3FileSystem.java

License:Apache License

private ObjectMetadata getS3ObjectMetadata(Path path) throws IOException {
    try {/*from   w w w  .j a va2s.co m*/
        return s3.getObjectMetadata(uri.getHost(), keyFromPath(path));
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            return null;
        }
        throw new IOException(e);
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
}

From source file:com.facebook.presto.rakam.S3BackupStore.java

License:Apache License

@Override
public boolean shardExists(java.util.UUID uuid) {
    try {/* ww w .j a  v  a 2 s. c o m*/
        s3Client.getObjectMetadata(config.getS3Bucket(), uuid.toString());
        return true;
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            return false;
        } else {
            throw e;
        }
    }
}

From source file:com.greglturnquist.springagram.fileservice.s3.FileService.java

License:Apache License

public void saveFile(InputStream input, long length, String filename) throws IOException {

    try {/*  ww  w  .  ja  v a 2 s. c o m*/
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(length);
        this.s3Client.putObject(this.bucket, filename, input, metadata);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 301) {
            updateEndpoint(e);
            saveFile(input, length, filename);
        }
    }
}

From source file:com.greglturnquist.springagram.fileservice.s3.FileService.java

License:Apache License

public Resource[] findAll() throws IOException {

    Resource[] results = new Resource[0];
    try {/* w w w. j  a  v  a2s  . c o m*/
        results = this.resourcePatternResolver.getResources(s3ify(this.bucket) + "/" + "*");
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 301) {
            updateEndpoint(e);
            results = this.findAll();
        }
    }
    return results;
}

From source file:com.ibm.stocator.fs.cos.COSAPIClient.java

License:Apache License

@Override
public FileStatus getFileStatus(String hostName, Path path, String msg)
        throws IOException, FileNotFoundException {
    FileStatus res = null;/*w w  w . j  a v  a 2 s.  c  o m*/
    FileStatus cached = memoryCache.getFileStatus(path.toString());
    if (cached != null) {
        return cached;
    }
    LOG.trace("getFileStatus(start) for {}, hostname: {}", path, hostName);
    /*
     * The requested path is equal to hostName. HostName is equal to
     * hostNameScheme, thus the container. Therefore we have no object to look
     * for and we return the FileStatus as a directory. Containers have to
     * lastModified.
     */
    if (path.toString().equals(hostName) || (path.toString().length() + 1 == hostName.length())) {
        LOG.trace("getFileStatus(completed) {}", path);
        res = new FileStatus(0L, true, 1, mBlockSize, 0L, path);
        memoryCache.putFileStatus(path.toString(), res);
        return res;
    }
    if (path.toString().contains(HADOOP_TEMPORARY)) {
        LOG.debug("getFileStatus on temp object {}. Return not found", path.toString());
        throw new FileNotFoundException("Not found " + path.toString());
    }
    String key = pathToKey(hostName, path);
    LOG.debug("getFileStatus: on original key {}", key);
    try {
        FileStatus fileStatus = null;
        try {
            fileStatus = getFileStatusKeyBased(key, path);
        } catch (AmazonS3Exception e) {
            if (e.getStatusCode() != 404) {
                throw new IOException(e);
            }
        }
        if (fileStatus != null) {
            LOG.trace("getFileStatus(completed) {}", path);
            memoryCache.putFileStatus(path.toString(), fileStatus);
            return fileStatus;
        }
        // means key returned not found. Trying to call get file status on key/
        // probably not needed this call
        if (!key.endsWith("/")) {
            String newKey = key + "/";
            try {
                LOG.debug("getFileStatus: original key not found. Alternative key {}", key);
                fileStatus = getFileStatusKeyBased(newKey, path);
            } catch (AmazonS3Exception e) {
                if (e.getStatusCode() != 404) {
                    throw new IOException(e);
                }
            }

            if (fileStatus != null) {
                LOG.trace("getFileStatus(completed) {}", path);
                memoryCache.putFileStatus(path.toString(), fileStatus);
                return fileStatus;
            } else {
                // if here: both key and key/ returned not found.
                // trying to see if pseudo directory of the form
                // a/b/key/d/e (a/b/key/ doesn't exists by itself)
                // perform listing on the key
                LOG.debug("getFileStatus: Modifined key {} not found. Trying to lisr", key);
                key = maybeAddTrailingSlash(key);
                ListObjectsRequest request = new ListObjectsRequest();
                request.setBucketName(mBucket);
                request.setPrefix(key);
                request.setDelimiter("/");
                request.setMaxKeys(1);

                ObjectListing objects = mClient.listObjects(request);
                if (!objects.getCommonPrefixes().isEmpty() || !objects.getObjectSummaries().isEmpty()) {
                    LOG.trace("getFileStatus(completed) {}", path);
                    res = new FileStatus(0, true, 1, 0, 0, path);
                    memoryCache.putFileStatus(path.toString(), res);
                    return res;
                } else if (key.isEmpty()) {
                    LOG.debug("Found root directory");
                    LOG.trace("getFileStatus(completed) {}", path);
                    res = new FileStatus(0, true, 1, 0, 0, path);
                    memoryCache.putFileStatus(path.toString(), res);
                    return res;
                }
            }
        }
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 403) {
            throw new IOException(e);
        }
    } catch (Exception e) {
        LOG.debug("Not found {}", path.toString());
        LOG.warn(e.getMessage());
        throw new FileNotFoundException("Not found " + path.toString());
    }
    throw new FileNotFoundException("Not found " + path.toString());
}

From source file:com.images3.data.impl.ImageContentAccessImplS3.java

License:Apache License

@Override
public File selectImageContent(ImageIdentity id, AmazonS3Bucket bucket) {
    File imageContent = new File(generateFilePath(id));
    if (imageContent.exists()) {
        return imageContent;
    }/*from w  ww. j a  v a  2s  .c om*/
    AmazonS3 client = clients.getClient(bucket);
    try {
        client.getObject(new GetObjectRequest(bucket.getName(), generateS3ObjectKey(id)), imageContent);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            throw new NoSuchEntityFoundException("ImageContent", generateS3ObjectKey(id),
                    "No such image content found.");
        }
        throw new RuntimeException(e);
    }
    return imageContent;
}

From source file:com.netflix.exhibitor.core.config.s3.S3ConfigProvider.java

License:Apache License

private boolean isNotFoundError(AmazonS3Exception e) {
    return (e.getStatusCode() == 404) || (e.getStatusCode() == 403);
}