Search in sources :

Example 21 with ListObjectsV2Request

use of software.amazon.awssdk.services.s3.model.ListObjectsV2Request in project syndesis-qe by syndesisio.

the class S3Utils method deleteS3Bucket.

public void deleteS3Bucket(String bucketName) {
    ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).build();
    ListObjectsV2Response listObjectsV2Response;
    do {
        listObjectsV2Response = s3client.listObjectsV2(listObjectsV2Request);
        for (S3Object s3Object : listObjectsV2Response.contents()) {
            s3client.deleteObject(DeleteObjectRequest.builder().bucket(bucketName).key(s3Object.key()).build());
        }
        listObjectsV2Request = ListObjectsV2Request.builder().bucket(bucketName).continuationToken(listObjectsV2Response.nextContinuationToken()).build();
    } while (listObjectsV2Response.isTruncated());
    s3client.deleteBucket(b -> b.bucket(bucketName));
}
Also used : ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 22 with ListObjectsV2Request

use of software.amazon.awssdk.services.s3.model.ListObjectsV2Request in project flyway by flyway.

the class AwsS3Scanner method scanForResources.

/**
 * Scans S3 for the resources. In AWS SDK v2, only the region that the client is configured with can be used.
 * The format of the path is expected to be {@code s3:{bucketName}/{optional prefix}}.
 *
 * @param location The location in S3 to start searching. Subdirectories are also searched.
 * @return The resources that were found.
 */
@Override
public Collection<LoadableResource> scanForResources(final Location location) {
    String bucketName = getBucketName(location);
    String prefix = getPrefix(bucketName, location.getPath());
    S3Client s3Client = S3ClientFactory.getClient();
    try {
        ListObjectsV2Request.Builder builder = ListObjectsV2Request.builder().bucket(bucketName).prefix(prefix);
        ListObjectsV2Request request = builder.build();
        ListObjectsV2Response listObjectResult = s3Client.listObjectsV2(request);
        return getLoadableResources(bucketName, listObjectResult);
    } catch (SdkClientException e) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Could not access s3 location:" + bucketName + prefix + " due to error: " + e.getMessage());
        }
        LOG.error("Skipping s3 location:" + bucketName + prefix + " due to error: " + e.getMessage());
        return Collections.emptyList();
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) S3Client(software.amazon.awssdk.services.s3.S3Client) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 23 with ListObjectsV2Request

use of software.amazon.awssdk.services.s3.model.ListObjectsV2Request in project engine by craftercms.

the class S3SiteListResolver method getSiteListFromBucketKeys.

protected Collection<String> getSiteListFromBucketKeys(String bucketName, String rootPrefix) {
    List<String> siteNames = new ArrayList<>();
    AmazonS3 client = clientBuilder.getClient();
    ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(bucketName).withPrefix(rootPrefix).withDelimiter(DELIMITER);
    ListObjectsV2Result result = client.listObjectsV2(request);
    if (CollectionUtils.isNotEmpty(result.getCommonPrefixes())) {
        result.getCommonPrefixes().stream().map(prefix -> StringUtils.stripEnd(StringUtils.removeStart(prefix, rootPrefix), DELIMITER)).forEach(siteNames::add);
    }
    return siteNames;
}
Also used : SiteListResolver(org.craftercms.engine.service.context.SiteListResolver) Collection(java.util.Collection) Bucket(com.amazonaws.services.s3.model.Bucket) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) DELIMITER(org.craftercms.engine.store.s3.S3ContentStoreAdapter.DELIMITER) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ArrayList(java.util.ArrayList) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) List(java.util.List) Matcher(java.util.regex.Matcher) SiteContextFactory(org.craftercms.engine.service.context.SiteContextFactory) AmazonS3(com.amazonaws.services.s3.AmazonS3) Required(org.springframework.beans.factory.annotation.Required) Pattern(java.util.regex.Pattern) S3ClientBuilder(org.craftercms.engine.store.s3.util.S3ClientBuilder) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI) AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ArrayList(java.util.ArrayList)

Example 24 with ListObjectsV2Request

use of software.amazon.awssdk.services.s3.model.ListObjectsV2Request in project alluxio by Alluxio.

the class S3AUnderFileSystem method getObjectListingChunk.

@Override
@Nullable
protected ObjectListingChunk getObjectListingChunk(String key, boolean recursive) throws IOException {
    String delimiter = recursive ? "" : PATH_SEPARATOR;
    key = PathUtils.normalizePath(key, PATH_SEPARATOR);
    // In case key is root (empty string) do not normalize prefix.
    key = key.equals(PATH_SEPARATOR) ? "" : key;
    if (mUfsConf.isSet(PropertyKey.UNDERFS_S3_LIST_OBJECTS_V1) && mUfsConf.getBoolean(PropertyKey.UNDERFS_S3_LIST_OBJECTS_V1)) {
        ListObjectsRequest request = new ListObjectsRequest().withBucketName(mBucketName).withPrefix(key).withDelimiter(delimiter).withMaxKeys(getListingChunkLength(mUfsConf));
        ObjectListing result = getObjectListingChunkV1(request);
        if (result != null) {
            return new S3AObjectListingChunkV1(request, result);
        }
    } else {
        ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(mBucketName).withPrefix(key).withDelimiter(delimiter).withMaxKeys(getListingChunkLength(mUfsConf));
        ListObjectsV2Result result = getObjectListingChunk(request);
        if (result != null) {
            return new S3AObjectListingChunk(request, result);
        }
    }
    return null;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) Nullable(javax.annotation.Nullable)

Example 25 with ListObjectsV2Request

use of software.amazon.awssdk.services.s3.model.ListObjectsV2Request in project druid by druid-io.

the class S3DataSegmentMover method selfCheckingMove.

/**
 * Copies an object and after that checks that the object is present at the target location, via a separate API call.
 * If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check
 * is added after it was observed that S3 may report a successful move, and the object is not found at the target
 * location.
 */
private void selfCheckingMove(String s3Bucket, String targetS3Bucket, String s3Path, String targetS3Path, String copyMsg) throws IOException, SegmentLoadingException {
    if (s3Bucket.equals(targetS3Bucket) && s3Path.equals(targetS3Path)) {
        log.info("No need to move file[s3://%s/%s] onto itself", s3Bucket, s3Path);
        return;
    }
    final ServerSideEncryptingAmazonS3 s3Client = this.s3ClientSupplier.get();
    if (s3Client.doesObjectExist(s3Bucket, s3Path)) {
        final ListObjectsV2Result listResult = s3Client.listObjectsV2(new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Path).withMaxKeys(1));
        // keyCount is still zero.
        if (listResult.getObjectSummaries().size() == 0) {
            // should never happen
            throw new ISE("Unable to list object [s3://%s/%s]", s3Bucket, s3Path);
        }
        final S3ObjectSummary objectSummary = listResult.getObjectSummaries().get(0);
        if (objectSummary.getStorageClass() != null && StorageClass.fromValue(StringUtils.toUpperCase(objectSummary.getStorageClass())).equals(StorageClass.Glacier)) {
            throw new AmazonServiceException(StringUtils.format("Cannot move file[s3://%s/%s] of storage class glacier, skipping.", s3Bucket, s3Path));
        } else {
            log.info("Moving file %s", copyMsg);
            final CopyObjectRequest copyRequest = new CopyObjectRequest(s3Bucket, s3Path, targetS3Bucket, targetS3Path);
            if (!config.getDisableAcl()) {
                copyRequest.setAccessControlList(S3Utils.grantFullControlToBucketOwner(s3Client, targetS3Bucket));
            }
            s3Client.copyObject(copyRequest);
            if (!s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
                throw new IOE("After copy was reported as successful the file doesn't exist in the target location [%s]", copyMsg);
            }
            deleteWithRetriesSilent(s3Bucket, s3Path);
            log.debug("Finished moving file %s", copyMsg);
        }
    } else {
        // ensure object exists in target location
        if (s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
            log.info("Not moving file [s3://%s/%s], already present in target location [s3://%s/%s]", s3Bucket, s3Path, targetS3Bucket, targetS3Path);
        } else {
            throw new SegmentLoadingException("Unable to move file %s, not present in either source or target location", copyMsg);
        }
    }
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) SegmentLoadingException(org.apache.druid.segment.loading.SegmentLoadingException) AmazonServiceException(com.amazonaws.AmazonServiceException) ISE(org.apache.druid.java.util.common.ISE) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOE(org.apache.druid.java.util.common.IOE)

Aggregations

ListObjectsV2Request (com.amazonaws.services.s3.model.ListObjectsV2Request)15 ListObjectsV2Result (com.amazonaws.services.s3.model.ListObjectsV2Result)14 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)12 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)9 Date (java.util.Date)8 Test (org.junit.Test)8 ListObjectsV2Request (software.amazon.awssdk.services.s3.model.ListObjectsV2Request)8 S3Object (software.amazon.awssdk.services.s3.model.S3Object)8 ArrayList (java.util.ArrayList)6 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 IOException (java.io.IOException)4 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws.s3.S3TestUtils.buildMockedS3FileSystem)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 Collection (java.util.Collection)3 List (java.util.List)3 Pattern (java.util.regex.Pattern)3 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem)3 S3Client (software.amazon.awssdk.services.s3.S3Client)3 HeadObjectRequest (software.amazon.awssdk.services.s3.model.HeadObjectRequest)3 HeadObjectResponse (software.amazon.awssdk.services.s3.model.HeadObjectResponse)3