本文整理汇总了Java中com.amazonaws.services.s3.model.DeleteObjectsRequest类的典型用法代码示例。如果您正苦于以下问题:Java DeleteObjectsRequest类的具体用法?Java DeleteObjectsRequest怎么用?Java DeleteObjectsRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DeleteObjectsRequest类属于com.amazonaws.services.s3.model包,在下文中一共展示了DeleteObjectsRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteAll
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
public void deleteAll(String bucket) {
logger.info("delete all from bucket, bucket={}", bucket);
ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket));
while (listing != null) {
List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>(listing.getObjectSummaries().size());
for (S3ObjectSummary summary : listing.getObjectSummaries()) {
String key = summary.getKey();
logger.info("add key to deletion batch, key={}", key);
keys.add(new DeleteObjectsRequest.KeyVersion(key));
}
if (!keys.isEmpty()) {
logger.info("delete key batch");
s3.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keys));
}
if (!listing.isTruncated()) return;
listing = s3.listNextBatchOfObjects(listing);
}
}
示例2: convertToXmlByteArray
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
/**
* Converts the specified {@link DeleteObjectsRequest} object to an XML fragment that
* can be sent to Amazon S3.
*
* @param rq
* The {@link DeleteObjectsRequest}
*/
public byte[] convertToXmlByteArray(DeleteObjectsRequest rq) throws SdkClientException {
XmlWriter xml = new XmlWriter();
xml.start("Delete");
if ( rq.getQuiet() ) {
xml.start("Quiet").value("true").end();
}
for (KeyVersion keyVersion : rq.getKeys()) {
writeKeyVersion(xml, keyVersion);
}
xml.end();
return xml.getBytes();
}
示例3: remove
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
@Override
public void remove(String accessKey) throws Exception {
LogUtils.debug(LOG_TAG, "Deleting file with access key: " + accessKey);
AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName);
List<KeyVersion> keys = new ArrayList<KeyVersion>();
keys.add(new KeyVersion(accessKey));
keys.add(new KeyVersion(accessKey + "_key"));
multiObjectDeleteRequest.setKeys(keys);
s3Client.deleteObjects(multiObjectDeleteRequest);
LogUtils.debug(LOG_TAG, "Deleted file with access key: " + accessKey);
}
示例4: deletePrefix
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
static void deletePrefix(AmazonS3 s3Client, String bucketName, String prefix) {
List<DeleteObjectsRequest.KeyVersion> trashKeys = new ArrayList<>();
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
for (S3ObjectSummary summary: S3Objects.withPrefix(s3Client, bucketName, prefix)) {
trashKeys.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
if (trashKeys.size() == BULK_DELETE_SIZE) {
deleteObjectsRequest.setKeys(trashKeys);
s3Client.deleteObjects(deleteObjectsRequest);
trashKeys.clear();
}
}
if (!trashKeys.isEmpty()) {
deleteObjectsRequest.setKeys(trashKeys);
s3Client.deleteObjects(deleteObjectsRequest);
}
}
示例5: deleteAllKeysOnPartialPath
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
public void deleteAllKeysOnPartialPath(String path) {
ObjectListing objectListing = s3Client.listObjects(s3Bucket, getFullPath(path));
if (objectListing.getObjectSummaries().isEmpty()) {
return;
}
List<DeleteObjectsRequest.KeyVersion> keys = objectListing
.getObjectSummaries()
.stream()
.map(objectSummary -> new DeleteObjectsRequest.KeyVersion(objectSummary.getKey()))
.collect(Collectors.toList());
DeleteObjectsRequest request = new DeleteObjectsRequest(s3Bucket);
request.setKeys(keys);
s3Client.deleteObjects(request);
}
示例6: s3DeleteRecursively
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
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;
}
}
}
示例7: testDeleteMultipleObjects
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
@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();
}
示例8: main
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
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());
}
示例9: deleteKeyVersions
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
/**
* 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());
}
}
}
示例10: shouldBatchDeleteObjects
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
/**
* Tests if an object can be deleted
*/
@Test
public void shouldBatchDeleteObjects() {
final File uploadFile1 = new File(UPLOAD_FILE_NAME);
final File uploadFile2 = new File(UPLOAD_FILE_NAME);
final File uploadFile3 = new File(UPLOAD_FILE_NAME);
final File uploadFile4 = new File(UPLOAD_FILE_NAME);
s3Client.createBucket(BUCKET_NAME);
s3Client
.putObject(new PutObjectRequest(BUCKET_NAME, "1_" + UPLOAD_FILE_NAME, uploadFile1));
s3Client
.putObject(new PutObjectRequest(BUCKET_NAME, "2_" + UPLOAD_FILE_NAME, uploadFile2));
s3Client
.putObject(new PutObjectRequest(BUCKET_NAME, "3_" + UPLOAD_FILE_NAME, uploadFile3));
final DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(BUCKET_NAME);
final List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
keys.add(new DeleteObjectsRequest.KeyVersion("1_" + UPLOAD_FILE_NAME));
keys.add(new DeleteObjectsRequest.KeyVersion("2_" + UPLOAD_FILE_NAME));
keys.add(new DeleteObjectsRequest.KeyVersion("3_" + UPLOAD_FILE_NAME));
keys.add(new DeleteObjectsRequest.KeyVersion("4_" + UPLOAD_FILE_NAME));
multiObjectDeleteRequest.setKeys(keys);
final DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
assertThat("Response should contain 4 entries", delObjRes.getDeletedObjects().size(), is(4));
thrown.expect(AmazonS3Exception.class);
thrown.expectMessage(containsString("Status Code: 406"));
s3Client.getObject(BUCKET_NAME, UPLOAD_FILE_NAME);
}
示例11: removeValues
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void removeValues(final Collection<String> ids) throws AlexaStateException {
super.removeValues(ids);
final List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<>();
ids.forEach(id -> keys.addAll(Arrays.asList(
new DeleteObjectsRequest.KeyVersion(getUserScopedFilePath(id)),
new DeleteObjectsRequest.KeyVersion(getAppScopedFilePath(id)))));
final DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName)
.withKeys(keys);
awsClient.deleteObjects(deleteObjectsRequest);
}
示例12: main
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
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.defaultClient();
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!");
}
示例13: deleteDirectory
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
@Override
public void deleteDirectory(final S3FileTransferRequestParamsDto params)
{
LOGGER.info("Deleting keys/key versions from S3... s3KeyPrefix=\"{}\" s3BucketName=\"{}\"", params.getS3KeyPrefix(), params.getS3BucketName());
Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()), "Deleting from root directory is not allowed.");
try
{
// List S3 versions.
List<DeleteObjectsRequest.KeyVersion> keyVersions = listVersions(params);
LOGGER.info("Found keys/key versions in S3 for deletion. s3KeyCount={} s3KeyPrefix=\"{}\" s3BucketName=\"{}\"", keyVersions.size(),
params.getS3KeyPrefix(), params.getS3BucketName());
// In order to avoid a MalformedXML AWS exception, we send delete request only when we have any key versions to delete.
if (!keyVersions.isEmpty())
{
// Create an S3 client.
AmazonS3Client s3Client = getAmazonS3(params);
try
{
// Delete the key versions.
deleteKeyVersions(s3Client, params.getS3BucketName(), keyVersions);
}
finally
{
s3Client.shutdown();
}
}
}
catch (AmazonClientException e)
{
throw new IllegalStateException(String
.format("Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(),
params.getS3BucketName(), e.getMessage()), e);
}
}
示例14: deleteFileList
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
@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);
}
}
示例15: deleteObjects
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入依赖的package包/类
@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);
}