类com.amazonaws.services.s3.model.DeleteObjectsResult源码实例Demo

下面列出了怎么用com.amazonaws.services.s3.model.DeleteObjectsResult的API类实例代码及写法,或者点击链接到github查看源代码。

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");
}
 
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;
}
 
源代码3 项目: s3proxy   文件: AwsSdkTest.java
@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();
}
 
源代码4 项目: pacbot   文件: S3Uploader.java
/**
 * 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());
	}
}
 
源代码5 项目: pacbot   文件: S3Uploader.java
/**
 * 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());
	}
}
 
源代码6 项目: circus-train   文件: S3DataManipulator.java
@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());
}
 
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");
}
 
源代码8 项目: herd   文件: MockS3OperationsImpl.java
@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);
}
 
源代码9 项目: tutorials   文件: AWSS3ServiceIntegrationTest.java
@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); 
}
 
源代码10 项目: crate   文件: MockAmazonS3.java
@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);
}
 
源代码11 项目: ats-framework   文件: S3Operations.java
/**
 * Delete all objects matching given prefix. This method is preferred for efficient deletion of many files
 * 
 * @param folderPrefix empty path is expected for objects in the "root" of the bucket 
 * @param searchString what pattern to be matched. This pattern will be matched against "short file name", i.e. 
 *                     the object's ID after last path separator (&quot;/&quot;).<br />
 *                     If null it means all ( string &quot;.*&quot;). 
 * @param recursive if true searches recursively for matching in nested path levels (&quot;/&quot;)
 * 
 * @return list of deleted objects
 * @throws S3OperationException in case of an error from server
 */
@PublicAtsApi
public void deleteObjects( String folderPrefix, String searchString, boolean recursive ) {

    //Alternative but not documented in S3 API: getClient().listObjectsV2(bucket, "prefix")
    ListObjectsRequest request = new ListObjectsRequest(bucketName, folderPrefix, null, recursive
                                                                                                  ? null
                                                                                                  : "/",
                                                        null);
    int totallyDeleted = 0;
    try {
        ObjectListing objectListing = s3Client.listObjects(request);
        int i = 0;
        if (searchString == null) {
            searchString = ".*"; // any string
        }
        List<KeyVersion> keysForDelete = new ArrayList<KeyVersion>(100);
        Pattern searchStringPattern = Pattern.compile(searchString);
        while (true) {
            keysForDelete.clear();
            for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
                S3ObjectSummary objectSummary = (S3ObjectSummary) iterator.next();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("listObject[" + (++i) + "]: " + objectSummary.toString());
                }

                String[] fileTokens = objectSummary.getKey().split("/");
                String s3Object = fileTokens[fileTokens.length - 1];

                Matcher matcher = searchStringPattern.matcher(s3Object);
                if (matcher.find()) {
                    keysForDelete.add(new KeyVersion(objectSummary.getKey()));
                    //allListElements.add(new S3ObjectInfo(objectSummary));
                }
            }
            if (keysForDelete.size() > 0) {
                // delete current set / batch size
                DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keysForDelete)
                                                                                                    .withQuiet(false);
                DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
                int currentlyDeletedCount = delObjRes.getDeletedObjects().size();
                totallyDeleted = totallyDeleted + currentlyDeletedCount;

                // verify size of deleted objects
                if (keysForDelete.size() != currentlyDeletedCount) {
                    LOG.warn("The number of actually deleted objects " + currentlyDeletedCount +
                             " does not match the expected size of " + keysForDelete.size());
                } else {
                    LOG.debug("Number of deleted S3 objects in current batch is " + currentlyDeletedCount);
                }
            }

            // more objects to retrieve (1K batch size of objects)
            if (objectListing.isTruncated()) {
                objectListing = s3Client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
        LOG.info("Successfully deleted " + totallyDeleted + " objects");
    } catch (AmazonClientException e) {
        throw new S3OperationException("Error deleting multiple objects matching pattern " + searchString 
                                       + ". Number of deleted objects is " + totallyDeleted, e);
    } 
}
 
源代码12 项目: circus-train   文件: S3DataManipulator.java
private boolean successfulDeletion(DeleteObjectsResult result, int countToDelete) {
  int amountDeleted = result.getDeletedObjects().size();
  return amountDeleted == countToDelete;
}
 
源代码13 项目: herd   文件: S3OperationsImpl.java
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client)
{
    return s3Client.deleteObjects(deleteObjectsRequest);
}
 
源代码14 项目: ignite   文件: DummyS3Client.java
/** Unsupported Operation. */
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjectsReq)
    throws SdkClientException {
    throw new UnsupportedOperationException("Operation not supported");
}
 
源代码15 项目: tutorials   文件: AWSS3Service.java
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjReq) {
    return s3client.deleteObjects(delObjReq);
}
 
源代码16 项目: Scribengin   文件: AmazonS3Mock.java
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException,
    AmazonServiceException {
  // TODO Auto-generated method stub
  return null;
}
 
源代码17 项目: herd   文件: S3Operations.java
/**
 * Deletes the specified S3 objects in the specified S3 bucket.
 *
 * @param deleteObjectsRequest the request object containing all the options for deleting multiple objects in a specified bucket
 * @param s3Client the {@link AmazonS3} implementation to use
 *
 * @return the successful response to the delete object request
 */
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client);
 
 同包方法