/** * 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("ListObjectsV2Request").add("Bucket", bucket()).add("Delimiter", delimiter()) .add("EncodingType", encodingTypeAsString()).add("MaxKeys", maxKeys()).add("Prefix", prefix()) .add("ContinuationToken", continuationToken()).add("FetchOwner", fetchOwner()).add("StartAfter", startAfter()) .add("RequestPayer", requestPayerAsString()).build(); }
@Override public ListObjectsV2Request build() { return new ListObjectsV2Request(this); }
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(); } }
@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(); }
return listObjectsV2(ListObjectsV2Request.builder().applyMutation(listObjectsV2Request).build());
/** * <p> * Returns some or all (up to 1000) of the objects in a bucket. You can use the request parameters as selection * criteria to return a subset of the objects in a bucket. Note: ListObjectsV2 is the revised List Objects API and * we recommend you use this revised API for new application development. * </p> * <br/> * <p> * This is a convenience which creates an instance of the {@link ListObjectsV2Request.Builder} avoiding the need to * create one manually via {@link ListObjectsV2Request#builder()} * </p> * * @param listObjectsV2Request * A {@link Consumer} that will call methods on {@link ListObjectsV2Request.Builder} to create a request. * @return Result of the ListObjectsV2 operation returned by the service. * @throws NoSuchBucketException * The specified bucket does not exist. * @throws SdkException * Base class for all exceptions that can be thrown by the SDK (both service and client). Can be used for * catch all scenarios. * @throws SdkClientException * If any client side error occurs such as an IO related failure, failure to get credentials, etc. * @throws S3Exception * Base class for all service exceptions. Unknown exceptions will be thrown as an instance of this type. * @sample S3Client.ListObjectsV2 */ default ListObjectsV2Response listObjectsV2(Consumer<ListObjectsV2Request.Builder> listObjectsV2Request) throws NoSuchBucketException, AwsServiceException, SdkClientException, S3Exception { return listObjectsV2(ListObjectsV2Request.builder().applyMutation(listObjectsV2Request).build()); }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ListObjectsV2Request)) { return false; } ListObjectsV2Request other = (ListObjectsV2Request) obj; return Objects.equals(bucket(), other.bucket()) && Objects.equals(delimiter(), other.delimiter()) && Objects.equals(encodingTypeAsString(), other.encodingTypeAsString()) && Objects.equals(maxKeys(), other.maxKeys()) && Objects.equals(prefix(), other.prefix()) && Objects.equals(continuationToken(), other.continuationToken()) && Objects.equals(fetchOwner(), other.fetchOwner()) && Objects.equals(startAfter(), other.startAfter()) && Objects.equals(requestPayerAsString(), other.requestPayerAsString()); }
return listObjectsV2Paginator(ListObjectsV2Request.builder().applyMutation(listObjectsV2Request).build());
@Override public int hashCode() { int hashCode = 1; hashCode = 31 * hashCode + Objects.hashCode(bucket()); hashCode = 31 * hashCode + Objects.hashCode(delimiter()); hashCode = 31 * hashCode + Objects.hashCode(encodingTypeAsString()); hashCode = 31 * hashCode + Objects.hashCode(maxKeys()); hashCode = 31 * hashCode + Objects.hashCode(prefix()); hashCode = 31 * hashCode + Objects.hashCode(continuationToken()); hashCode = 31 * hashCode + Objects.hashCode(fetchOwner()); hashCode = 31 * hashCode + Objects.hashCode(startAfter()); hashCode = 31 * hashCode + Objects.hashCode(requestPayerAsString()); return hashCode; }
return listObjectsV2Paginator(ListObjectsV2Request.builder().applyMutation(listObjectsV2Request).build());
public <T> Optional<T> getValueForField(String fieldName, Class<T> clazz) { switch (fieldName) { case "Bucket": return Optional.ofNullable(clazz.cast(bucket())); case "Delimiter": return Optional.ofNullable(clazz.cast(delimiter())); case "EncodingType": return Optional.ofNullable(clazz.cast(encodingTypeAsString())); case "MaxKeys": return Optional.ofNullable(clazz.cast(maxKeys())); case "Prefix": return Optional.ofNullable(clazz.cast(prefix())); case "ContinuationToken": return Optional.ofNullable(clazz.cast(continuationToken())); case "FetchOwner": return Optional.ofNullable(clazz.cast(fetchOwner())); case "StartAfter": return Optional.ofNullable(clazz.cast(startAfter())); case "RequestPayer": return Optional.ofNullable(clazz.cast(requestPayerAsString())); default: return Optional.empty(); } }
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); } }
@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")); }
@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)); }