Search in sources :

Example 1 with S3ServiceException

use of org.jets3t.service.S3ServiceException in project hadoop by apache.

the class Jets3tNativeFileSystemStore method processException.

/**
   * Handle any service exception by translating it into an IOException
   * @param thrown exception
   * @param original original exception -thrown if no other translation could
   * be made
   * @param key key sought from object store or "" for undefined
   * @return an exception to throw. If isProcessingCause==true this may be null.
   */
private IOException processException(Throwable thrown, Throwable original, String key) {
    IOException result;
    if (thrown.getCause() != null) {
        // recurse down
        result = processException(thrown.getCause(), original, key);
    } else if (thrown instanceof HttpException) {
        // nested HttpException - examine error code and react
        HttpException httpException = (HttpException) thrown;
        String responseMessage = httpException.getResponseMessage();
        int responseCode = httpException.getResponseCode();
        String bucketName = "s3n://" + bucket.getName();
        String text = String.format("%s : %03d : %s", bucketName, responseCode, responseMessage);
        String filename = !key.isEmpty() ? (bucketName + "/" + key) : text;
        IOException ioe;
        switch(responseCode) {
            case 404:
                result = new FileNotFoundException(filename);
                break;
            case // invalid range
            416:
                result = new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF + ": " + filename);
                break;
            case //forbidden
            403:
                result = new AccessControlException("Permission denied" + ": " + filename);
                break;
            default:
                result = new IOException(text);
        }
        result.initCause(thrown);
    } else if (thrown instanceof S3ServiceException) {
        S3ServiceException se = (S3ServiceException) thrown;
        LOG.debug("S3ServiceException: {}: {} : {}", se.getS3ErrorCode(), se.getS3ErrorMessage(), se, se);
        if ("InvalidRange".equals(se.getS3ErrorCode())) {
            result = new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF);
        } else {
            result = new S3Exception(se);
        }
    } else if (thrown instanceof ServiceException) {
        ServiceException se = (ServiceException) thrown;
        LOG.debug("S3ServiceException: {}: {} : {}", se.getErrorCode(), se.toString(), se, se);
        result = new S3Exception(se);
    } else if (thrown instanceof IOException) {
        result = (IOException) thrown;
    } else {
        // here there is no exception derived yet.
        // this means no inner cause, and no translation made yet.
        // convert the original to an IOException -rather than just the
        // exception at the base of the tree
        result = new S3Exception(original);
    }
    return result;
}
Also used : ServiceException(org.jets3t.service.ServiceException) S3ServiceException(org.jets3t.service.S3ServiceException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) AccessControlException(org.apache.hadoop.security.AccessControlException) HttpException(org.jets3t.service.impl.rest.HttpException) IOException(java.io.IOException) S3ServiceException(org.jets3t.service.S3ServiceException)

Example 2 with S3ServiceException

use of org.jets3t.service.S3ServiceException in project hadoop by apache.

the class Jets3tNativeFileSystemStore method initialize.

@Override
public void initialize(URI uri, Configuration conf) throws IOException {
    S3Credentials s3Credentials = new S3Credentials();
    s3Credentials.initialize(uri, conf);
    try {
        AWSCredentials awsCredentials = new AWSCredentials(s3Credentials.getAccessKey(), s3Credentials.getSecretAccessKey());
        this.s3Service = new RestS3Service(awsCredentials);
    } catch (S3ServiceException e) {
        handleException(e);
    }
    multipartEnabled = conf.getBoolean("fs.s3n.multipart.uploads.enabled", false);
    multipartBlockSize = Math.min(conf.getLong("fs.s3n.multipart.uploads.block.size", 64 * 1024 * 1024), MAX_PART_SIZE);
    multipartCopyBlockSize = Math.min(conf.getLong("fs.s3n.multipart.copy.block.size", MAX_PART_SIZE), MAX_PART_SIZE);
    serverSideEncryptionAlgorithm = conf.get("fs.s3n.server-side-encryption-algorithm");
    bucket = new S3Bucket(uri.getHost());
}
Also used : S3Bucket(org.jets3t.service.model.S3Bucket) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3ServiceException(org.jets3t.service.S3ServiceException) AWSCredentials(org.jets3t.service.security.AWSCredentials)

Example 3 with S3ServiceException

use of org.jets3t.service.S3ServiceException in project hadoop by apache.

the class Jets3tNativeFileSystemStore method purge.

@Override
public void purge(String prefix) throws IOException {
    String key = "";
    try {
        S3Object[] objects = s3Service.listObjects(bucket.getName(), prefix, null);
        for (S3Object object : objects) {
            key = object.getKey();
            s3Service.deleteObject(bucket, key);
        }
    } catch (S3ServiceException e) {
        handleException(e, key);
    }
}
Also used : S3Object(org.jets3t.service.model.S3Object) S3ServiceException(org.jets3t.service.S3ServiceException)

Example 4 with S3ServiceException

use of org.jets3t.service.S3ServiceException in project OpenTripPlanner by opentripplanner.

the class DegreeGridNEDTileSource method getPathToTile.

private File getPathToTile(int x, int y) {
    File path = new File(cacheDirectory, formatLatLon(x, y) + ".tiff");
    if (path.exists()) {
        return path;
    } else {
        path.getParentFile().mkdirs();
        if (awsAccessKey == null || awsSecretKey == null) {
            throw new RuntimeException("Cannot download NED tiles from S3: awsAccessKey or awsSecretKey properties are not set");
        }
        log.info("Downloading NED degree tile " + path);
        // download the file from S3.
        AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
        try {
            S3Service s3Service = new RestS3Service(awsCredentials);
            String key = formatLatLon(x, y) + ".tiff";
            S3Object object = s3Service.getObject(awsBucketName, key);
            InputStream istream = object.getDataInputStream();
            FileOutputStream ostream = new FileOutputStream(path);
            byte[] buffer = new byte[4096];
            while (true) {
                int read = istream.read(buffer);
                if (read == -1) {
                    break;
                }
                ostream.write(buffer, 0, read);
            }
            ostream.close();
            istream.close();
        } catch (S3ServiceException e) {
            path.deleteOnExit();
            throw new RuntimeException(e);
        } catch (ServiceException e) {
            path.deleteOnExit();
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            path.deleteOnExit();
            throw new RuntimeException(e);
        } catch (IOException e) {
            path.deleteOnExit();
            throw new RuntimeException(e);
        }
        return path;
    }
}
Also used : InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) S3ServiceException(org.jets3t.service.S3ServiceException) IOException(java.io.IOException) AWSCredentials(org.jets3t.service.security.AWSCredentials) ServiceException(org.jets3t.service.ServiceException) S3ServiceException(org.jets3t.service.S3ServiceException) FileOutputStream(java.io.FileOutputStream) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) File(java.io.File) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Service(org.jets3t.service.S3Service)

Example 5 with S3ServiceException

use of org.jets3t.service.S3ServiceException in project druid by druid-io.

the class S3DataSegmentPullerTest method testGZUncompressRetries.

@Test
public void testGZUncompressRetries() throws ServiceException, IOException, SegmentLoadingException {
    final String bucket = "bucket";
    final String keyPrefix = "prefix/dir/0";
    final RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    final byte[] value = bucket.getBytes("utf8");
    final File tmpFile = temporaryFolder.newFile("gzTest.gz");
    try (OutputStream outputStream = new GZIPOutputStream(new FileOutputStream(tmpFile))) {
        outputStream.write(value);
    }
    S3Object object0 = new S3Object();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    object0.setDataInputStream(new FileInputStream(tmpFile));
    File tmpDir = temporaryFolder.newFolder("gzTestDir");
    S3ServiceException exception = new S3ServiceException();
    exception.setErrorCode("NoSuchKey");
    exception.setResponseCode(404);
    EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(null).once();
    EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))).andThrow(exception).once();
    EasyMock.expect(s3Client.getObjectDetails(EasyMock.eq(object0.getBucketName()), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    EasyMock.expect(s3Client.getObject(EasyMock.eq(bucket), EasyMock.eq(object0.getKey()))).andReturn(object0).once();
    S3DataSegmentPuller puller = new S3DataSegmentPuller(s3Client);
    EasyMock.replay(s3Client);
    FileUtils.FileCopyResult result = puller.getSegmentFiles(new S3DataSegmentPuller.S3Coords(bucket, object0.getKey()), tmpDir);
    EasyMock.verify(s3Client);
    Assert.assertEquals(value.length, result.size());
    File expected = new File(tmpDir, "renames-0");
    Assert.assertTrue(expected.exists());
    Assert.assertEquals(value.length, expected.length());
}
Also used : FileUtils(io.druid.java.util.common.FileUtils) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) S3ServiceException(org.jets3t.service.S3ServiceException) Date(java.util.Date) FileInputStream(java.io.FileInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) File(java.io.File) Test(org.junit.Test)

Aggregations

S3ServiceException (org.jets3t.service.S3ServiceException)10 S3Object (org.jets3t.service.model.S3Object)5 S3Service (org.jets3t.service.S3Service)4 RestS3Service (org.jets3t.service.impl.rest.httpclient.RestS3Service)4 AWSCredentials (org.jets3t.service.security.AWSCredentials)3 Test (org.junit.Test)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ServiceException (org.jets3t.service.ServiceException)2 S3Bucket (org.jets3t.service.model.S3Bucket)2 Variables (org.pentaho.di.core.variables.Variables)2 FileUtils (io.druid.java.util.common.FileUtils)1 ActionEvent (java.awt.event.ActionEvent)1 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1