com.amazonaws.services.s3.model.DeleteObjectsRequest Java Examples

The following examples show how to use com.amazonaws.services.s3.model.DeleteObjectsRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrimitiveS3OperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void deleteObjects(AmazonS3 service, ObjectListing listing) throws MultiObjectDeleteException, IOException {
	do {
		if (Thread.currentThread().isInterrupted()) {
			throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
		}
		List<S3ObjectSummary> objectSummaries = listing.getObjectSummaries();
		if (!objectSummaries.isEmpty()) {
			List<KeyVersion> keys = new ArrayList<KeyVersion>(objectSummaries.size());
			for (S3ObjectSummary object: objectSummaries) {
				keys.add(new KeyVersion(object.getKey()));
			}
			DeleteObjectsRequest request = new DeleteObjectsRequest(listing.getBucketName()).withKeys(keys).withQuiet(true);
			service.deleteObjects(request); // quiet
		}
		listing = service.listNextBatchOfObjects(listing);
	} while (listing.isTruncated());
}
 
Example #2
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteMultipleObjects() throws Exception {
    String blobName = "foo";
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());

    DeleteObjectsRequest request = new DeleteObjectsRequest(containerName)
            .withKeys(blobName);

    // without quiet
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    DeleteObjectsResult result = client.deleteObjects(request);
    assertThat(result.getDeletedObjects()).hasSize(1);
    assertThat(result.getDeletedObjects().iterator().next().getKey())
            .isEqualTo(blobName);

    // with quiet
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    result = client.deleteObjects(request.withQuiet(true));
    assertThat(result.getDeletedObjects()).isEmpty();
}
 
Example #3
Source File: S3FileSystemTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteThousandsOfObjectsInMultipleBuckets() throws IOException {
  S3FileSystem s3FileSystem = buildMockedS3FileSystem(s3Options());

  List<String> buckets = ImmutableList.of("bucket1", "bucket2");
  List<String> keys = new ArrayList<>();
  for (int i = 0; i < 2500; i++) {
    keys.add(String.format("key-%d", i));
  }
  List<S3ResourceId> paths = new ArrayList<>();
  for (String bucket : buckets) {
    for (String key : keys) {
      paths.add(S3ResourceId.fromComponents(bucket, key));
    }
  }

  s3FileSystem.delete(paths);

  // Should require 6 calls to delete 2500 objects in each of 2 buckets.
  verify(s3FileSystem.getAmazonS3Client(), times(6))
      .deleteObjects(any(DeleteObjectsRequest.class));
}
 
Example #4
Source File: S3Profile.java    From jobcacher-plugin with MIT License 6 votes vote down vote up
public void delete(String bucketName, String pathPrefix) {
    ObjectListing listing = null;
    do {
        listing = listing == null ? helper.client().listObjects(bucketName, pathPrefix) : helper.client().listNextBatchOfObjects(listing);

        DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName);

        List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(listing.getObjectSummaries().size());
        for (S3ObjectSummary summary : listing.getObjectSummaries()) {
            keys.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
        }
        req.withKeys(keys);

        helper.client().deleteObjects(req);
    } while (listing.isTruncated());
}
 
Example #5
Source File: TruncateBucket.java    From aws-big-data-blog with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
   System.out.println("WARNING: This will indiscriminately delete every object in a given bucket. This operation cannot be undone.");
   final String bucketName = prompt("Bucket");
   if (!"yes".equals(prompt("Are you sure you want to delete all objects in bucket " + bucketName + "? (type 'yes' to continue)"))) {
      System.out.println("Aborting...");
      return;
   }
   final TruncateBucket truncater = new TruncateBucket(bucketName);
   truncater.truncate();

   ObjectListing results;
   do {
      results = s3.listObjects(bucketName);
      final List<KeyVersion> keys = results.getObjectSummaries().stream()
            .map((k) -> new KeyVersion(k.getKey()))
            .collect(toList());
      final DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keys);
      s3.deleteObjects(deleteRequest);
   } while (results.isTruncated());

}
 
Example #6
Source File: pinpoint_list_endpoint_ids.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void deleteS3Objects(String s3BucketName, List<String> keys) {

        AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();

        String[] keysArray = keys.toArray(new String[keys.size()]);
        DeleteObjectsRequest request = new DeleteObjectsRequest(s3BucketName).withKeys(keysArray);

        System.out.println("Deleting the following Amazon S3 objects created by Amazon Pinpoint:");

        for (String key : keys) {
            System.out.println("\t- " + key);
        }

        try {
            s3Client.deleteObjects(request);
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }

        System.out.println("Finished deleting objects.");
    }
 
Example #7
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void uploadAndDeleteObjectsWithVersions() {
    System.out.println("Uploading and deleting objects with versions specified.");

    // Upload three sample objects.
    ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object without version ID example " + i;
        PutObjectResult putResult = S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName,
                "Object number " + i + " to be deleted.");
        // Gather the new object keys with version IDs.
        keys.add(new KeyVersion(keyName, putResult.getVersionId()));
    }

    // Delete the specified versions of the sample objects.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME)
            .withKeys(keys)
            .withQuiet(false);

    // Verify that the object versions were successfully deleted.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " objects successfully deleted");
}
 
Example #8
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static DeleteObjectsResult uploadAndDeleteObjectsWithoutVersions() {
    System.out.println("Uploading and deleting objects with no versions specified.");

    // Upload three sample objects.
    ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object with version ID example " + i;
        S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted.");
        // Gather the new object keys without version IDs.
        keys.add(new KeyVersion(keyName));
    }

    // Delete the sample objects without specifying versions.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys)
            .withQuiet(false);

    // Verify that delete markers were successfully added to the objects.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " objects successfully marked for deletion without versions.");
    return delObjRes;
}
 
Example #9
Source File: TestUtils.java    From digdag with Apache License 2.0 6 votes vote down vote up
public static void s3DeleteRecursively(AmazonS3 s3, String bucket, String prefix)
        throws Exception
{
    ListObjectsRequest request = new ListObjectsRequest()
            .withBucketName(bucket)
            .withPrefix(prefix);

    while (true) {
        ObjectListing listing = s3.listObjects(request);
        String[] keys = listing.getObjectSummaries().stream().map(S3ObjectSummary::getKey).toArray(String[]::new);
        for (String key : keys) {
            logger.info("delete s3://{}/{}", bucket, key);
        }
        retryExecutor()
                .retryIf(e -> e instanceof AmazonServiceException)
                .run(() -> s3.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys)));
        if (listing.getNextMarker() == null) {
            break;
        }
    }
}
 
Example #10
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public void deleteFileList(final S3FileTransferRequestParamsDto params)
{
    LOGGER.info("Deleting a list of objects from S3... s3BucketName=\"{}\" s3KeyCount={}", params.getS3BucketName(), params.getFiles().size());

    try
    {
        // In order to avoid a MalformedXML AWS exception, we send delete request only when we have any keys to delete.
        if (!params.getFiles().isEmpty())
        {
            // Create an S3 client.
            AmazonS3Client s3Client = getAmazonS3(params);

            try
            {
                // Build a list of keys to be deleted.
                List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
                for (File file : params.getFiles())
                {
                    keys.add(new DeleteObjectsRequest.KeyVersion(file.getPath().replaceAll("\\\\", "/")));
                }

                // Delete the keys.
                deleteKeyVersions(s3Client, params.getS3BucketName(), keys);
            }
            finally
            {
                s3Client.shutdown();
            }
        }
    }
    catch (Exception e)
    {
        throw new IllegalStateException(
            String.format("Failed to delete a list of keys from bucket \"%s\". Reason: %s", params.getS3BucketName(), e.getMessage()), e);
    }
}
 
Example #11
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a list of keys/key versions from the specified S3 bucket.
 *
 * @param s3Client the S3 client
 * @param s3BucketName the S3 bucket name
 * @param keyVersions the list of S3 keys/key versions
 */
private void deleteKeyVersions(AmazonS3Client s3Client, String s3BucketName, List<DeleteObjectsRequest.KeyVersion> keyVersions)
{
    // Create a request to delete multiple objects in the specified bucket.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3BucketName);

    // The Multi-Object Delete request can contain a list of up to 1000 keys.
    for (int i = 0; i < keyVersions.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++)
    {
        List<DeleteObjectsRequest.KeyVersion> keysSubList =
            keyVersions.subList(i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(keyVersions.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST));
        multiObjectDeleteRequest.setKeys(keysSubList);
        try
        {
            s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client);
        }
        catch (MultiObjectDeleteException multiObjectDeleteException)
        {
            logMultiObjectDeleteException(multiObjectDeleteException);
            throw multiObjectDeleteException;
        }

        LOGGER.info("Successfully requested the deletion of the listed below keys/key versions from the S3 bucket. s3KeyCount={} s3BucketName=\"{}\"",
            keysSubList.size(), s3BucketName);

        for (DeleteObjectsRequest.KeyVersion keyVersion : keysSubList)
        {
            LOGGER.info("s3Key=\"{}\" s3VersionId=\"{}\"", keyVersion.getKey(), keyVersion.getVersion());
        }
    }
}
 
Example #12
Source File: S3UtilProgram.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static void deleteByFullPathPrefix(ParameterTool params) {
	final String bucket = params.getRequired("bucket");
	final String s3prefix = params.getRequired("s3prefix");
	String[] keys = listByFullPathPrefix(bucket, s3prefix).toArray(new String[] {});
	if (keys.length > 0) {
		DeleteObjectsRequest request = new DeleteObjectsRequest(bucket).withKeys(keys);
		AmazonS3ClientBuilder.defaultClient().deleteObjects(request);
	}
}
 
Example #13
Source File: MockS3OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client)
{
    LOGGER.debug("deleteObjects(): deleteObjectRequest.getBucketName() = " + deleteObjectsRequest.getBucketName() + ", deleteObjectRequest.getKeys() = " +
        deleteObjectsRequest.getKeys());

    List<DeletedObject> deletedObjects = new ArrayList<>();

    MockS3Bucket mockS3Bucket = mockS3Buckets.get(deleteObjectsRequest.getBucketName());

    for (KeyVersion keyVersion : deleteObjectsRequest.getKeys())
    {
        String s3ObjectKey = keyVersion.getKey();
        String s3ObjectVersion = keyVersion.getVersion();
        String s3ObjectKeyVersion = s3ObjectKey + (s3ObjectVersion != null ? s3ObjectVersion : "");

        mockS3Bucket.getObjects().remove(s3ObjectKey);

        if (mockS3Bucket.getVersions().remove(s3ObjectKeyVersion) != null)
        {
            DeletedObject deletedObject = new DeletedObject();
            deletedObject.setKey(s3ObjectKey);
            deletedObject.setVersionId(s3ObjectVersion);
            deletedObjects.add(deletedObject);
        }
    }

    return new DeleteObjectsResult(deletedObjects);
}
 
Example #14
Source File: S3Utils.java    From micro-server with Apache License 2.0 5 votes vote down vote up
/**
 * Method delete all <i>objects</i> from <i>bucketName</i> in groups by 1000
 * elements
 * 
 * @param bucketName
 * @param objects
 */
public void delete(String bucketName, List<KeyVersion> objects) {
    ReactiveSeq.fromList(objects)
               .grouped(1000)
               .forEach(l -> {
                   DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName);
                   req.setKeys(l.toList());
                   client.deleteObjects(req);
               });
}
 
Example #15
Source File: AwsSdkTest.java    From s3proxy with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteMultipleObjectsEmpty() throws Exception {
    DeleteObjectsRequest request = new DeleteObjectsRequest(containerName)
            .withKeys();

    try {
        client.deleteObjects(request);
        Fail.failBecauseExceptionWasNotThrown(AmazonS3Exception.class);
    } catch (AmazonS3Exception e) {
        assertThat(e.getErrorCode()).isEqualTo("MalformedXML");
    }
}
 
Example #16
Source File: AWSS3ServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test 
public void whenVerifyingDeleteObjects_thenCorrect() { 
    DeleteObjectsRequest request = mock(DeleteObjectsRequest.class);
    DeleteObjectsResult result = mock(DeleteObjectsResult.class);
    when(s3.deleteObjects((DeleteObjectsRequest)any())).thenReturn(result); 
    
    assertThat(service.deleteObjects(request)).isEqualTo(result);
    verify(s3).deleteObjects(request); 
}
 
Example #17
Source File: MockAmazonS3.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest request) throws SdkClientException {
    assertThat(request.getBucketName(), equalTo(bucket));

    final List<DeleteObjectsResult.DeletedObject> deletions = new ArrayList<>();
    for (DeleteObjectsRequest.KeyVersion key : request.getKeys()) {
        if (blobs.remove(key.getKey()) != null) {
            DeleteObjectsResult.DeletedObject deletion = new DeleteObjectsResult.DeletedObject();
            deletion.setKey(key.getKey());
            deletions.add(deletion);
        }
    }
    return new DeleteObjectsResult(deletions);
}
 
Example #18
Source File: S3UtilProgram.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void deleteByFullPathPrefix(ParameterTool params) {
	final String bucket = params.getRequired("bucket");
	final String s3prefix = params.getRequired("s3prefix");
	String[] keys = listByFullPathPrefix(bucket, s3prefix).toArray(new String[] {});
	if (keys.length > 0) {
		DeleteObjectsRequest request = new DeleteObjectsRequest(bucket).withKeys(keys);
		AmazonS3ClientBuilder.defaultClient().deleteObjects(request);
	}
}
 
Example #19
Source File: S3FileSystem.java    From beam with Apache License 2.0 5 votes vote down vote up
private void delete(String bucket, Collection<String> keys) throws IOException {
  checkArgument(
      keys.size() <= MAX_DELETE_OBJECTS_PER_REQUEST,
      "only %s keys can be deleted per request, but got %s",
      MAX_DELETE_OBJECTS_PER_REQUEST,
      keys.size());
  List<KeyVersion> deleteKeyVersions =
      keys.stream().map(KeyVersion::new).collect(Collectors.toList());
  DeleteObjectsRequest request = new DeleteObjectsRequest(bucket).withKeys(deleteKeyVersions);
  try {
    amazonS3.get().deleteObjects(request);
  } catch (AmazonClientException e) {
    throw new IOException(e);
  }
}
 
Example #20
Source File: DeleteObjects.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "To run this example, supply the name of an S3 bucket and at least\n" +
            "one object name (key) to delete.\n" +
            "\n" +
            "Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String bucket_name = args[0];
    String[] object_keys = Arrays.copyOfRange(args, 1, args.length);

    System.out.println("Deleting objects from S3 bucket: " + bucket_name);
    for (String k : object_keys) {
        System.out.println(" * " + k);
    }

    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name)
                .withKeys(object_keys);
        s3.deleteObjects(dor);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
 
Example #21
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void multiObjectVersionedDeleteRemoveDeleteMarkers(DeleteObjectsResult response) {
    List<KeyVersion> keyList = new ArrayList<KeyVersion>();
    for (DeletedObject deletedObject : response.getDeletedObjects()) {
        // Note that the specified version ID is the version ID for the delete marker.
        keyList.add(new KeyVersion(deletedObject.getKey(), deletedObject.getDeleteMarkerVersionId()));
    }
    // Create a request to delete the delete markers.
    DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keyList);

    // Delete the delete markers, leaving the objects intact in the bucket.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(deleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " delete markers successfully deleted");
}
 
Example #22
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Delete files.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param folder the folder
 */
private void deleteFiles(AmazonS3 s3client,String s3Bucket,String folder){
	
	String[] keys = listKeys(s3client,s3Bucket,folder);
	DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3Bucket).withKeys((keys));
	
	try{
		DeleteObjectsResult result = s3client.deleteObjects(multiObjectDeleteRequest);
		log.debug("Files Deleted " +result.getDeletedObjects().stream().map(obj->obj.getKey()).collect(Collectors.toList()));
	}catch(Exception e){
		log.error("Delete Failed",e);
		ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
	}
}
 
Example #23
Source File: S3CleanupExtension.java    From teamcity-s3-artifact-storage-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private Integer deleteChunk(@NotNull final String pathPrefix,
                            @NotNull final String bucketName,
                            @NotNull final AmazonS3 client,
                            @NotNull final List<String> part,
                            @NotNull final Supplier<String> info) throws Exception {
  final List<DeleteObjectsRequest.KeyVersion> objectKeys = part.stream().map(path -> new DeleteObjectsRequest.KeyVersion(pathPrefix + path)).collect(Collectors.toList());
  final DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(objectKeys);
  return executeWithNewThreadName(info.get(),
                                  () -> doUnderContextClassLoader(S3Util.class.getClassLoader(), () -> client.deleteObjects(deleteObjectsRequest).getDeletedObjects().size()));
}
 
Example #24
Source File: S3DataManipulator.java    From circus-train with Apache License 2.0 5 votes vote down vote up
@Override
public boolean delete(String path) {
  log.info("Deleting all data at location: {}", path);
  AmazonS3URI uri = toAmazonS3URI(URI.create(path));
  String bucket = uri.getBucket();

  List<KeyVersion> keysToDelete = getKeysToDelete(bucket, uri.getKey());
  log.debug("Deleting keys: {}", keysToDelete.stream().map(k -> k.getKey()).collect(Collectors.toList()));

  DeleteObjectsResult result = s3Client.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keysToDelete));
  return successfulDeletion(result, keysToDelete.size());
}
 
Example #25
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Delete files.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param folder the folder
 */
private void deleteFiles(AmazonS3 s3client,String s3Bucket,String folder){
	
	String[] keys = listKeys(s3client,s3Bucket,folder);
	DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3Bucket).withKeys((keys));
	
	try{
		DeleteObjectsResult result = s3client.deleteObjects(multiObjectDeleteRequest);
		log.debug("Files Deleted " +result.getDeletedObjects().stream().map(obj->obj.getKey()).collect(Collectors.toList()));
	}catch(Exception e){
		log.error("Delete Failed",e);
		ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
	}
}
 
Example #26
Source File: TruncateBucket.java    From aws-big-data-blog with Apache License 2.0 4 votes vote down vote up
private void delete(Collection<String> keyBuffer) {
   final String[] keyArray = keyBuffer.toArray(new String[keyBuffer.size()]);
   final DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keyArray);
   s3.deleteObjects(deleteRequest);
}
 
Example #27
Source File: S3BlobContainer.java    From crate with Apache License 2.0 4 votes vote down vote up
private static DeleteObjectsRequest bulkDelete(String bucket, List<String> blobs) {
    return new DeleteObjectsRequest(bucket).withKeys(blobs.toArray(Strings.EMPTY_ARRAY)).withQuiet(true);
}
 
Example #28
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException,
    AmazonServiceException {
  // TODO Auto-generated method stub
  return null;
}
 
Example #29
Source File: AWSS3Service.java    From tutorials with MIT License 4 votes vote down vote up
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjReq) {
    return s3client.deleteObjects(delObjReq);
}
 
Example #30
Source File: S3Application.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    //set-up the client
    AmazonS3 s3client = AmazonS3ClientBuilder
      .standard()
      .withCredentials(new AWSStaticCredentialsProvider(credentials))
      .withRegion(Regions.US_EAST_2)
      .build();
    
    AWSS3Service awsService = new AWSS3Service(s3client);

    bucketName = "baeldung-bucket";

    //creating a bucket
    if(awsService.doesBucketExist(bucketName)) {
        System.out.println("Bucket name is not available."
          + " Try again with a different Bucket name.");
        return;
    }
    awsService.createBucket(bucketName);
    
    //list all the buckets
    for(Bucket s : awsService.listBuckets() ) {
        System.out.println(s.getName());
    }
    
    //deleting bucket
    awsService.deleteBucket("baeldung-bucket-test2");
    
    //uploading object
    awsService.putObject(
      bucketName, 
      "Document/hello.txt",
      new File("/Users/user/Document/hello.txt")
    );

    //listing objects
    ObjectListing objectListing = awsService.listObjects(bucketName);
    for(S3ObjectSummary os : objectListing.getObjectSummaries()) {
        System.out.println(os.getKey());
    }

    //downloading an object
    S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt");
    S3ObjectInputStream inputStream = s3object.getObjectContent();
    FileUtils.copyInputStreamToFile(inputStream, new File("/Users/user/Desktop/hello.txt"));
    
    //copying an object
    awsService.copyObject(
      "baeldung-bucket", 
      "picture/pic.png", 
      "baeldung-bucket2", 
      "Document/picture.png"
    );
    
    //deleting an object
    awsService.deleteObject(bucketName, "Document/hello.txt");

    //deleting multiple objects
    String objkeyArr[] = {
      "Document/hello2.txt", 
      "Document/picture.png"
    };
    
    DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("baeldung-bucket")
      .withKeys(objkeyArr);
    awsService.deleteObjects(delObjReq);
}