本文整理汇总了Java中com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion方法的典型用法代码示例。如果您正苦于以下问题:Java DeleteObjectsRequest.KeyVersion方法的具体用法?Java DeleteObjectsRequest.KeyVersion怎么用?Java DeleteObjectsRequest.KeyVersion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.s3.model.DeleteObjectsRequest
的用法示例。
在下文中一共展示了DeleteObjectsRequest.KeyVersion方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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);
}
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}
}
示例7: 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);
}
}
示例8: 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());
}
}
}
示例9: deleteObjects
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入方法依赖的package包/类
private static void deleteObjects(AmazonS3 s3, String s3BucketName, List<S3ObjectSummary> objSummaries, List<DeleteObjectsRequest.KeyVersion> keys, int accum){
Logger.log(new Throwable(), "deleting " + (objSummaries.size() - accum) + " objects...");
DeleteObjectsRequest deleteResquests = new DeleteObjectsRequest(s3BucketName);
deleteResquests.setKeys(keys);
s3.deleteObjects(deleteResquests);
}
示例10: listVersions
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入方法依赖的package包/类
@Override
public List<DeleteObjectsRequest.KeyVersion> listVersions(final S3FileTransferRequestParamsDto params)
{
Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()), "Listing of S3 key versions from root directory is not allowed.");
AmazonS3Client s3Client = getAmazonS3(params);
List<DeleteObjectsRequest.KeyVersion> keyVersions = new ArrayList<>();
try
{
ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(params.getS3BucketName()).withPrefix(params.getS3KeyPrefix());
VersionListing versionListing;
do
{
versionListing = s3Operations.listVersions(listVersionsRequest, s3Client);
for (S3VersionSummary versionSummary : versionListing.getVersionSummaries())
{
keyVersions.add(new DeleteObjectsRequest.KeyVersion(versionSummary.getKey(), versionSummary.getVersionId()));
}
listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker());
listVersionsRequest.setVersionIdMarker(versionListing.getNextVersionIdMarker());
}
while (versionListing.isTruncated());
}
catch (AmazonS3Exception amazonS3Exception)
{
if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(amazonS3Exception.getErrorCode()))
{
throw new IllegalArgumentException("The specified bucket '" + params.getS3BucketName() + "' does not exist.", amazonS3Exception);
}
throw new IllegalStateException("Error accessing S3", amazonS3Exception);
}
catch (AmazonClientException e)
{
throw new IllegalStateException(String
.format("Failed to list keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: %s", params.getS3KeyPrefix(), params.getS3BucketName(),
e.getMessage()), e);
}
finally
{
// Shutdown the AmazonS3Client instance to release resources.
s3Client.shutdown();
}
return keyVersions;
}
示例11: listVersions
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入方法依赖的package包/类
/**
* Lists all S3 versions matching the S3 key prefix in the given bucket (S3 bucket name). The S3 bucket name and S3 key prefix that identify the S3 versions
* to get listed are taken from the S3 file transfer request parameters DTO.
*
* @param params the S3 file transfer request parameters
*
* @return the list of all S3 versions that match the prefix in the given bucket
*/
public List<DeleteObjectsRequest.KeyVersion> listVersions(final S3FileTransferRequestParamsDto params);
示例12: deleteBucket
import com.amazonaws.services.s3.model.DeleteObjectsRequest; //导入方法依赖的package包/类
public static void deleteBucket(String s3BucketName) throws TalesException{
try{
AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(Config.getAWSAccessKeyId(), Config.getAWSSecretAccessKey()));
if(s3.doesBucketExist(s3BucketName)) {
// gets all the s3 objects
Logger.log(new Throwable(), "getting the list of all the objects saved in the bucket");
ObjectListing current = s3.listObjects(s3BucketName);
List<S3ObjectSummary> objSummaries = current.getObjectSummaries();
while (current.isTruncated()) {
current = s3.listNextBatchOfObjects(current);
objSummaries.addAll(current.getObjectSummaries());
}
// deletes the files
int i = 0;
int accum = 0;
List<DeleteObjectsRequest.KeyVersion> keys = new ArrayList<DeleteObjectsRequest.KeyVersion>();
for(S3ObjectSummary objSummary: objSummaries){
keys.add(new DeleteObjectsRequest.KeyVersion(objSummary.getKey()));
i++;
accum++;
if(i > 500){
i = 0;
deleteObjects(s3, s3BucketName, objSummaries, keys, accum);
keys.clear();
}
}
if(keys.size() > 0){
deleteObjects(s3, s3BucketName, objSummaries, keys, accum);
}
// deletes the bucket
s3.deleteBucket(s3BucketName);
}else{
Logger.log(new Throwable(), "bucket not found");
}
}catch(Exception e){
throw new TalesException(new Throwable(), e);
}
}