/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. */ @Override public String toString() { return ToString.builder("ListObjectsV2Response").add("IsTruncated", isTruncated()).add("Contents", contents()) .add("Name", name()).add("Prefix", prefix()).add("Delimiter", delimiter()).add("MaxKeys", maxKeys()) .add("CommonPrefixes", commonPrefixes()).add("EncodingType", encodingTypeAsString()).add("KeyCount", keyCount()) .add("ContinuationToken", continuationToken()).add("NextContinuationToken", nextContinuationToken()) .add("StartAfter", startAfter()).build(); }
@Override public ListObjectsV2Response build() { return new ListObjectsV2Response(this); }
/** * Returns an iterable to iterate through the paginated {@link ListObjectsV2Response#contents()} member. The * returned iterable is used to iterate through the results across all response pages and not a single page. * * This method is useful if you are interested in iterating over the paginated member in the response pages instead * of the top level pages. Similar to iteration over pages, this method internally makes service calls to get the * next list of results until the iteration stops or there are no more results. */ public final SdkIterable<S3Object> contents() { Function<ListObjectsV2Response, Iterator<S3Object>> getIterator = response -> { if (response != null && response.contents() != null) { return response.contents().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsIterable.builder().pagesIterable(this).itemIteratorFunction(getIterator).build(); }
public List<S3Object> listAll(final String bucketName) { final ListObjectsV2Response listObjectsV2Response = s3Client.listObjectsV2(ListObjectsV2Request.builder().bucket(bucketName).build()); if (listObjectsV2Response.keyCount() > 0) { return listObjectsV2Response.contents(); } else { return Collections.emptyList(); } }
private static SdkResponse modifyListObjectsV2Response(ListObjectsV2Response response) { return response.toBuilder() .delimiter(urlDecode(response.delimiter())) .prefix(urlDecode(response.prefix())) .startAfter(urlDecode(response.startAfter())) .contents(decodeContents(response.contents())) .build(); }
public void deleteAllObjectsWithPrefixInBucket(final String bucketName, final String prefix) { final ListObjectsV2Response listObjectResponse = s3Client.listObjectsV2(ListObjectsV2Request.builder().bucket(bucketName).build()); if (listObjectResponse.keyCount() > 0) { DeleteObjectsRequest deleteObjectsRequest = DeleteObjectsRequest.builder() .bucket(bucketName) .delete(builder() .objects(convertS3ObjectsToObjectIdentifiers(listObjectResponse, prefix)) .build()) .build(); final DeleteObjectsResponse deleteObjectsResponse = s3Client.deleteObjects(deleteObjectsRequest); LOG.debug("deleteAllObjectsWithPrefixInBucket in bucket {} with prefix {}: {}", bucketName, prefix, deleteObjectsResponse); } else { LOG.debug("deleteAllObjectsWithPrefixInBucket listObjects found no keys in bucket {} with prefix {}: {}", bucketName, prefix, listObjectResponse); } }
@Override public ListObjectsV2Response listObjectsV2(final ListObjectsV2Request listObjectsV2Request) throws S3Exception { final Collection<S3Object> s3Objects = bucketsWithContents.get(listObjectsV2Request.bucket()) .values() .stream() .map(bucketItem -> S3Object.builder() .key(bucketItem.getName()) .size((long) bucketItem.getData().length) .lastModified(bucketItem.getLastModified()) .build()) .collect(Collectors.toList()); return ListObjectsV2Response.builder() .contents(s3Objects) .keyCount(s3Objects.size()) .build(); }
/** * Returns an iterable to iterate through the paginated {@link ListObjectsV2Response#commonPrefixes()} member. The * returned iterable is used to iterate through the results across all response pages and not a single page. * * This method is useful if you are interested in iterating over the paginated member in the response pages instead * of the top level pages. Similar to iteration over pages, this method internally makes service calls to get the * next list of results until the iteration stops or there are no more results. */ public final SdkIterable<CommonPrefix> commonPrefixes() { Function<ListObjectsV2Response, Iterator<CommonPrefix>> getIterator = response -> { if (response != null && response.commonPrefixes() != null) { return response.commonPrefixes().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsIterable.builder().pagesIterable(this).itemIteratorFunction(getIterator).build(); }
private List<ObjectIdentifier> convertS3ObjectsToObjectIdentifiers(final ListObjectsV2Response listObjectsV2Response, final String prefix) { return listObjectsV2Response.contents() .stream() .filter(o -> o.key() != null && o.key().startsWith(prefix)) .map(o -> ObjectIdentifier.builder().key(o.key()).build()).collect(toList()); } }
/** * Returns a publisher that can be used to get a stream of data. You need to subscribe to the publisher to request * the stream of data. The publisher has a helper forEach method that takes in a {@link java.util.function.Consumer} * and then applies that consumer to each response returned by the service. */ public final SdkPublisher<CommonPrefix> commonPrefixes() { Function<ListObjectsV2Response, Iterator<CommonPrefix>> getIterator = response -> { if (response != null && response.commonPrefixes() != null) { return response.commonPrefixes().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsPublisher.builder().nextPageFetcher(new ListObjectsV2ResponseFetcher()) .iteratorFunction(getIterator).isLastPage(isLastPage).build(); }
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(isTruncated()); hashCode = 31 * hashCode + Objects.hashCode(contents()); hashCode = 31 * hashCode + Objects.hashCode(name()); hashCode = 31 * hashCode + Objects.hashCode(prefix()); hashCode = 31 * hashCode + Objects.hashCode(delimiter()); hashCode = 31 * hashCode + Objects.hashCode(maxKeys()); hashCode = 31 * hashCode + Objects.hashCode(commonPrefixes()); hashCode = 31 * hashCode + Objects.hashCode(encodingTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(keyCount()); hashCode = 31 * hashCode + Objects.hashCode(continuationToken()); hashCode = 31 * hashCode + Objects.hashCode(nextContinuationToken()); hashCode = 31 * hashCode + Objects.hashCode(startAfter()); return hashCode; }
/** * Returns a publisher that can be used to get a stream of data. You need to subscribe to the publisher to request * the stream of data. The publisher has a helper forEach method that takes in a {@link java.util.function.Consumer} * and then applies that consumer to each response returned by the service. */ public final SdkPublisher<S3Object> contents() { Function<ListObjectsV2Response, Iterator<S3Object>> getIterator = response -> { if (response != null && response.contents() != null) { return response.contents().iterator(); } return Collections.emptyIterator(); }; return PaginatedItemsPublisher.builder().nextPageFetcher(new ListObjectsV2ResponseFetcher()) .iteratorFunction(getIterator).isLastPage(isLastPage).build(); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ListObjectsV2Response)) { return false; } ListObjectsV2Response other = (ListObjectsV2Response) obj; return Objects.equals(isTruncated(), other.isTruncated()) && Objects.equals(contents(), other.contents()) && Objects.equals(name(), other.name()) && Objects.equals(prefix(), other.prefix()) && Objects.equals(delimiter(), other.delimiter()) && Objects.equals(maxKeys(), other.maxKeys()) && Objects.equals(commonPrefixes(), other.commonPrefixes()) && Objects.equals(encodingTypeAsString(), other.encodingTypeAsString()) && Objects.equals(keyCount(), other.keyCount()) && Objects.equals(continuationToken(), other.continuationToken()) && Objects.equals(nextContinuationToken(), other.nextContinuationToken()) && Objects.equals(startAfter(), other.startAfter()); }
@Test public void shouldListObjectsInBucket() { // given PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket("someBucket") .key("someObject") .build(); RequestBody requestBody = RequestBody.fromString("content"); testee.putObject(putObjectRequest, requestBody); // when ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket("someBucket") .build(); ListObjectsV2Response listObjectsV2Response = testee.listObjectsV2(listObjectsV2Request); //then assertThat(listObjectsV2Response.contents().size(), is(1)); assertThat(listObjectsV2Response.contents().get(0).key(), is("someObject")); }
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "IsTruncated": return Optional.ofNullable(clazz.cast(isTruncated())); case "Contents": return Optional.ofNullable(clazz.cast(contents())); case "Name": return Optional.ofNullable(clazz.cast(name())); case "Prefix": return Optional.ofNullable(clazz.cast(prefix())); case "Delimiter": return Optional.ofNullable(clazz.cast(delimiter())); case "MaxKeys": return Optional.ofNullable(clazz.cast(maxKeys())); case "CommonPrefixes": return Optional.ofNullable(clazz.cast(commonPrefixes())); case "EncodingType": return Optional.ofNullable(clazz.cast(encodingTypeAsString())); case "KeyCount": return Optional.ofNullable(clazz.cast(keyCount())); case "ContinuationToken": return Optional.ofNullable(clazz.cast(continuationToken())); case "NextContinuationToken": return Optional.ofNullable(clazz.cast(nextContinuationToken())); case "StartAfter": return Optional.ofNullable(clazz.cast(startAfter())); default: return Optional.empty(); } }
@Test public void deleteShouldRemoveItemsFromBucket() { // given PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket("someBucket") .key("someObject") .build(); RequestBody requestBody = RequestBody.fromString("content"); testee.putObject(putObjectRequest, requestBody); testee.deleteObjects(DeleteObjectsRequest.builder().bucket("someBucket").delete(Delete.builder().objects (ObjectIdentifier.builder().key("someObject").build()).build()).build()); // when ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() .bucket("someBucket") .build(); ListObjectsV2Response listObjectsV2Response = testee.listObjectsV2(listObjectsV2Request); //then assertThat(listObjectsV2Response.contents().size(), is(0)); }