Search in sources :

Example 91 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project deeplearning4j by deeplearning4j.

the class S3Downloader method paginate.

/**
     * Paginates through a bucket's keys invoking the listener 
     * at each key
     * @param bucket the bucket to iterate
     * @param listener the listener
     */
public void paginate(String bucket, BucketKeyListener listener) {
    AmazonS3 s3 = getClient();
    ObjectListing list = s3.listObjects(bucket);
    for (S3ObjectSummary summary : list.getObjectSummaries()) {
        if (listener != null)
            listener.onKey(s3, bucket, summary.getKey());
    }
    while (list.isTruncated()) {
        list = s3.listNextBatchOfObjects(list);
        for (S3ObjectSummary summary : list.getObjectSummaries()) {
            if (listener != null)
                listener.onKey(s3, bucket, summary.getKey());
        }
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Example 92 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project deeplearning4j by deeplearning4j.

the class S3Downloader method listObjects.

/**
     * Simple way of retrieving the listings for a bucket
     * @param bucket the bucket to retrieve listings for
     * @return the object listing for this bucket
     */
public ObjectListing listObjects(String bucket) {
    AmazonS3 s3 = getClient();
    ObjectListing list = s3.listObjects(bucket);
    return list;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3)

Example 93 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project presto by prestodb.

the class TestPrestoS3FileSystem method testReadRetryCounters.

@SuppressWarnings({ "ResultOfMethodCallIgnored", "OverlyStrongTypeCast", "ConstantConditions" })
@Test
public void testReadRetryCounters() throws Exception {
    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        int maxRetries = 2;
        MockAmazonS3 s3 = new MockAmazonS3();
        s3.setGetObjectHttpErrorCode(SC_INTERNAL_SERVER_ERROR);
        Configuration configuration = new Configuration();
        configuration.set(S3_MAX_BACKOFF_TIME, "1ms");
        configuration.set(S3_MAX_RETRY_TIME, "5s");
        configuration.setInt(S3_MAX_CLIENT_RETRIES, maxRetries);
        fs.initialize(new URI("s3n://test-bucket/"), configuration);
        fs.setS3Client(s3);
        try (FSDataInputStream inputStream = fs.open(new Path("s3n://test-bucket/test"))) {
            inputStream.read();
        } catch (Throwable expected) {
            assertInstanceOf(expected, AmazonS3Exception.class);
            assertEquals(((AmazonS3Exception) expected).getStatusCode(), SC_INTERNAL_SERVER_ERROR);
            assertEquals(PrestoS3FileSystem.getFileSystemStats().getReadRetries().getTotalCount(), maxRetries);
            assertEquals(PrestoS3FileSystem.getFileSystemStats().getGetObjectRetries().getTotalCount(), (maxRetries + 1L) * maxRetries);
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) ClientConfiguration(com.amazonaws.ClientConfiguration) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 94 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project heron by twitter.

the class S3Uploader method initialize.

@Override
public void initialize(Config config) {
    bucket = S3Context.bucket(config);
    String accessKey = S3Context.accessKey(config);
    String accessSecret = S3Context.secretKey(config);
    String awsProfile = S3Context.awsProfile(config);
    String proxy = S3Context.proxyUri(config);
    String endpoint = S3Context.uri(config);
    String customRegion = S3Context.region(config);
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    if (Strings.isNullOrEmpty(bucket)) {
        throw new RuntimeException("Missing heron.uploader.s3.bucket config value");
    }
    // by not specifying a CredentialsProvider.
    if (!Strings.isNullOrEmpty(accessKey) || !Strings.isNullOrEmpty(accessSecret)) {
        if (!Strings.isNullOrEmpty(awsProfile)) {
            throw new RuntimeException("Please provide access_key/secret_key " + "or aws_profile, not both.");
        }
        if (Strings.isNullOrEmpty(accessKey)) {
            throw new RuntimeException("Missing heron.uploader.s3.access_key config value");
        }
        if (Strings.isNullOrEmpty(accessSecret)) {
            throw new RuntimeException("Missing heron.uploader.s3.secret_key config value");
        }
        builder.setCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, accessSecret)));
    } else if (!Strings.isNullOrEmpty(awsProfile)) {
        builder.setCredentials(new ProfileCredentialsProvider(awsProfile));
    }
    if (!Strings.isNullOrEmpty(proxy)) {
        URI proxyUri;
        try {
            proxyUri = new URI(proxy);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Invalid heron.uploader.s3.proxy_uri config value: " + proxy, e);
        }
        ClientConfiguration clientCfg = new ClientConfiguration();
        clientCfg.withProtocol(Protocol.HTTPS).withProxyHost(proxyUri.getHost()).withProxyPort(proxyUri.getPort());
        if (!Strings.isNullOrEmpty(proxyUri.getUserInfo())) {
            String[] info = proxyUri.getUserInfo().split(":", 2);
            clientCfg.setProxyUsername(info[0]);
            if (info.length > 1) {
                clientCfg.setProxyPassword(info[1]);
            }
        }
        builder.setClientConfiguration(clientCfg);
    }
    s3Client = builder.withRegion(customRegion).withPathStyleAccessEnabled(true).withChunkedEncodingDisabled(true).withPayloadSigningEnabled(true).build();
    if (!Strings.isNullOrEmpty(endpoint)) {
        s3Client.setEndpoint(endpoint);
    }
    final String topologyName = Context.topologyName(config);
    final String topologyPackageLocation = Context.topologyPackageFile(config);
    pathPrefix = S3Context.pathPrefix(config);
    packageFileHandler = new File(topologyPackageLocation);
    // The path the packaged topology will be uploaded to
    remoteFilePath = generateS3Path(pathPrefix, topologyName, packageFileHandler.getName());
    // Generate the location of the backup file incase we need to revert the deploy
    previousVersionFilePath = generateS3Path(pathPrefix, topologyName, "previous_" + packageFileHandler.getName());
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 95 with Bucket

use of com.amazonaws.services.s3.model.Bucket in project ice by Netflix.

the class AwsUtils method listAllObjects.

/**
     * List all object summary with given prefix in the s3 bucket.
     * @param bucket
     * @param prefix
     * @return
     */
public static List<S3ObjectSummary> listAllObjects(String bucket, String prefix, String accountId, String assumeRole, String externalId) {
    AmazonS3Client s3Client = AwsUtils.s3Client;
    try {
        ListObjectsRequest request = new ListObjectsRequest().withBucketName(bucket).withPrefix(prefix);
        List<S3ObjectSummary> result = Lists.newLinkedList();
        if (!StringUtils.isEmpty(accountId) && !StringUtils.isEmpty(assumeRole)) {
            Credentials assumedCredentials = getAssumedCredentials(accountId, assumeRole, externalId);
            s3Client = new AmazonS3Client(new BasicSessionCredentials(assumedCredentials.getAccessKeyId(), assumedCredentials.getSecretAccessKey(), assumedCredentials.getSessionToken()), clientConfig);
        }
        ObjectListing page = null;
        do {
            if (page != null)
                request.setMarker(page.getNextMarker());
            page = s3Client.listObjects(request);
            result.addAll(page.getObjectSummaries());
        } while (page.isTruncated());
        return result;
    } finally {
        if (s3Client != AwsUtils.s3Client)
            s3Client.shutdown();
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) Credentials(com.amazonaws.services.securitytoken.model.Credentials)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)47 AmazonS3 (com.amazonaws.services.s3.AmazonS3)41 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)38 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)37 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)35 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)29 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)23 IOException (java.io.IOException)23 AmazonClientException (com.amazonaws.AmazonClientException)22 ArrayList (java.util.ArrayList)20 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)16 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)15 Test (org.junit.Test)15 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)13 S3Object (com.amazonaws.services.s3.model.S3Object)13 Path (org.apache.hadoop.fs.Path)12 Date (java.util.Date)11 Bucket (com.amazonaws.services.s3.model.Bucket)10 InputStream (java.io.InputStream)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)9