下面列出了com.amazonaws.services.s3.model.DeleteVersionRequest#com.amazonaws.services.s3.model.DeleteObjectRequest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private boolean deleteObject(String key)
{
try {
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(getBucketName(uri), key);
if (requesterPaysEnabled) {
// TODO use deleteObjectRequest.setRequesterPays() when https://github.com/aws/aws-sdk-java/issues/1219 is fixed
// currently the method exists, but is ineffective (doesn't set the required HTTP header)
deleteObjectRequest.putCustomRequestHeader(Headers.REQUESTER_PAYS_HEADER, Constants.REQUESTER_PAYS);
}
s3.deleteObject(deleteObjectRequest);
return true;
}
catch (AmazonClientException e) {
return false;
}
}
private void productionFileDeletion (String fileName, String s3Folder){
String bucketName = this.bucketAWS + s3Folder;
try {
this.amazonS3.deleteObject(new DeleteObjectRequest(bucketName, fileName));
System.out.println("S3 DELETION: File " + fileName + " deleted successfully");
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException.");
System.out.println("Error Message: " + ace.getMessage());
}
}
private void productionFileDeletion (String fileName, String s3Folder){
String bucketName = this.bucketAWS + s3Folder;
try {
this.amazonS3.deleteObject(new DeleteObjectRequest(bucketName, fileName));
System.out.println("S3 DELETION: File " + fileName + " deleted successfully");
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException.");
System.out.println("Error Message: " + ace.getMessage());
}
}
@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
String key = pathToKey(path);
LOG.debug("Object name to delete {}. Path {}", key, path.toString());
try {
mClient.deleteObject(new DeleteObjectRequest(mBucket, key));
memoryCache.removeFileStatus(path.toString());
return true;
} catch (AmazonServiceException e) {
if (e.getStatusCode() != 404) {
throw new IOException(e);
}
}
LOG.warn("Delete on {} not found. Nothing to delete");
return false;
}
@Test
public void testDeleteObjectSimple() throws IOException {
runner.setProperty(DeleteS3Object.REGION, "us-west-2");
runner.setProperty(DeleteS3Object.BUCKET, "test-bucket");
final Map<String, String> attrs = new HashMap<>();
attrs.put("filename", "delete-key");
runner.enqueue(new byte[0], attrs);
runner.run(1);
runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
ArgumentCaptor<DeleteObjectRequest> captureRequest = ArgumentCaptor.forClass(DeleteObjectRequest.class);
Mockito.verify(mockS3Client, Mockito.times(1)).deleteObject(captureRequest.capture());
DeleteObjectRequest request = captureRequest.getValue();
assertEquals("test-bucket", request.getBucketName());
assertEquals("delete-key", request.getKey());
Mockito.verify(mockS3Client, Mockito.never()).deleteVersion(Mockito.any(DeleteVersionRequest.class));
}
@Test
public void testDeleteVersionSimple() {
runner.setProperty(DeleteS3Object.REGION, "us-west-2");
runner.setProperty(DeleteS3Object.BUCKET, "test-bucket");
runner.setProperty(DeleteS3Object.VERSION_ID, "test-version");
final Map<String, String> attrs = new HashMap<>();
attrs.put("filename", "test-key");
runner.enqueue(new byte[0], attrs);
runner.run(1);
runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
ArgumentCaptor<DeleteVersionRequest> captureRequest = ArgumentCaptor.forClass(DeleteVersionRequest.class);
Mockito.verify(mockS3Client, Mockito.times(1)).deleteVersion(captureRequest.capture());
DeleteVersionRequest request = captureRequest.getValue();
assertEquals("test-bucket", request.getBucketName());
assertEquals("test-key", request.getKey());
assertEquals("test-version", request.getVersionId());
Mockito.verify(mockS3Client, Mockito.never()).deleteObject(Mockito.any(DeleteObjectRequest.class));
}
@Test
public void testDeleteVersionFromExpressions() {
runner.setProperty(DeleteS3Object.REGION, "us-west-2");
runner.setProperty(DeleteS3Object.BUCKET, "${s3.bucket}");
runner.setProperty(DeleteS3Object.VERSION_ID, "${s3.version}");
final Map<String, String> attrs = new HashMap<>();
attrs.put("filename", "test-key");
attrs.put("s3.bucket", "test-bucket");
attrs.put("s3.version", "test-version");
runner.enqueue(new byte[0], attrs);
runner.run(1);
runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_SUCCESS, 1);
ArgumentCaptor<DeleteVersionRequest> captureRequest = ArgumentCaptor.forClass(DeleteVersionRequest.class);
Mockito.verify(mockS3Client, Mockito.times(1)).deleteVersion(captureRequest.capture());
DeleteVersionRequest request = captureRequest.getValue();
assertEquals("test-bucket", request.getBucketName());
assertEquals("test-key", request.getKey());
assertEquals("test-version", request.getVersionId());
Mockito.verify(mockS3Client, Mockito.never()).deleteObject(Mockito.any(DeleteObjectRequest.class));
}
/**
* Delete file from Amazon S3 storage.
*
* @param bucket
* - S3 Bucket name.
* @param key
* - S3 storage path. Example:
* DEMO/TestSuiteName/TestMethodName/file.txt
*/
public void delete(String bucket, String key) {
if (key == null) {
throw new RuntimeException("Key is null!");
}
if (key.isEmpty()) {
throw new RuntimeException("Key is empty!");
}
try {
s3client.deleteObject(new DeleteObjectRequest(bucket, key));
} catch (AmazonServiceException ase) {
LOGGER.error("Caught an AmazonServiceException, which "
+ "means your request made it "
+ "to Amazon S3, but was rejected with an error response for some reason.\n"
+ "Error Message: " + ase.getMessage() + "\n"
+ "HTTP Status Code: " + ase.getStatusCode() + "\n"
+ "AWS Error Code: " + ase.getErrorCode() + "\n"
+ "Error Type: " + ase.getErrorType() + "\n"
+ "Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
LOGGER.error("Caught an AmazonClientException.\n"
+ "Error Message: " + ace.getMessage());
}
}
@Override
public Parameters handleRequest(Parameters parameters, Context context) {
context.getLogger().log("Input Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]");
// The archive location of the snapshot will be decided by the alert
// flag
String newFilename;
if (parameters.getSendAlert()) {
newFilename = parameters.getS3Key().replace("upload/", "archive/alerts/");
} else {
newFilename = parameters.getS3Key().replace("upload/", "archive/falsepositives/");
}
// Ensure that the first two hyphens are used to create sub-directories
// in the file path
newFilename = newFilename.replaceFirst("-", "/");
newFilename = newFilename.replaceFirst("-", "/");
// Using the S3 client, first copy the file to the archive, and then
// delete the original
AmazonS3 client = AmazonS3ClientBuilder.defaultClient();
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(parameters.getS3Bucket(), parameters.getS3Key(), parameters.getS3Bucket(), newFilename);
client.copyObject(copyObjectRequest);
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(parameters.getS3Bucket(), parameters.getS3Key());
client.deleteObject(deleteObjectRequest);
// Place the new location in the parameters
parameters.setS3ArchivedKey(newFilename);
context.getLogger().log("Output Function [" + context.getFunctionName() + "], Parameters [" + parameters + "]");
return parameters;
}
@Override
public void delete(String path) throws BusinessException {
if (!Optional.ofNullable(path).isPresent()) {
throw new BusinessException(Validations.INVALID_PATH.getCode());
}
client.getClient(credentials.getCredentials());
try {
s3Client.deleteObject(
new DeleteObjectRequest(cdnConfig.getName(), path)
);
} catch (AmazonServiceException e) {
throw new BusinessException(e.getMessage());
}
}
@Override
public void deleteBySha1(final String tenant, final String sha1Hash) {
final String key = objectKey(tenant, sha1Hash);
LOG.info("Deleting S3 object from bucket {} and key {}", s3Properties.getBucketName(), key);
amazonS3.deleteObject(new DeleteObjectRequest(s3Properties.getBucketName(), key));
}
static void copy(
AmazonS3 s3Client,
String srcBucket,
String sourceKey,
String destBucket,
String destKey,
boolean isMove
) {
CopyObjectRequest cp = new CopyObjectRequest(srcBucket, sourceKey, destBucket, destKey);
s3Client.copyObject(cp);
if (isMove) {
s3Client.deleteObject(new DeleteObjectRequest(srcBucket, sourceKey));
}
}
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{
AmazonKey amazonKey = getAmazonKey(_session, argStruct);
AmazonS3 s3Client = getAmazonS3(amazonKey);
String bucket = getNamedStringParam(argStruct, "bucket", null );
String srckey = getNamedStringParam(argStruct, "srckey", null );
String deskey = getNamedStringParam(argStruct, "destkey", null );
String aes256key = getNamedStringParam(argStruct, "aes256key", null );
if ( srckey != null && srckey.charAt( 0 ) == '/' )
srckey = srckey.substring(1);
if ( deskey != null && deskey.charAt( 0 ) == '/' )
deskey = deskey.substring(1);
CopyObjectRequest cor = new CopyObjectRequest(bucket, srckey, bucket, deskey);
if ( aes256key != null && !aes256key.isEmpty() ){
cor.setSourceSSECustomerKey( new SSECustomerKey(aes256key) );
cor.setDestinationSSECustomerKey( new SSECustomerKey(aes256key) );
}
try {
s3Client.copyObject(cor);
s3Client.deleteObject(new DeleteObjectRequest(bucket, srckey));
return cfBooleanData.TRUE;
} catch (Exception e) {
throwException(_session, "AmazonS3: " + e.getMessage() );
return cfBooleanData.FALSE;
}
}
@Test
public void testDeleteObjectS3Exception() {
runner.setProperty(DeleteS3Object.REGION, "us-west-2");
runner.setProperty(DeleteS3Object.BUCKET, "test-bucket");
final Map<String, String> attrs = new HashMap<>();
attrs.put("filename", "delete-key");
runner.enqueue(new byte[0], attrs);
Mockito.doThrow(new AmazonS3Exception("NoSuchBucket")).when(mockS3Client).deleteObject(Mockito.any());
runner.run(1);
runner.assertAllFlowFilesTransferred(DeleteS3Object.REL_FAILURE, 1);
ArgumentCaptor<DeleteObjectRequest> captureRequest = ArgumentCaptor.forClass(DeleteObjectRequest.class);
Mockito.verify(mockS3Client, Mockito.never()).deleteVersion(Mockito.any(DeleteVersionRequest.class));
}
@Override
public void deleteProduct(Product product) throws DataTransferException, IOException {
for (Reference ref : product.getProductReferences()) {
DeleteObjectRequest request = new DeleteObjectRequest(bucketName, stripProtocol(
ref.getDataStoreReference(), true));
try {
s3Client.deleteObject(request);
} catch (AmazonClientException e) {
throw new DataTransferException(String.format(
"Failed to delete product reference %s from S3", ref.getDataStoreReference()), e);
}
}
}
@Test
public void testDeleteProduct() throws DataTransferException, IOException {
dataTransferer.deleteProduct(product);
ArgumentCaptor<DeleteObjectRequest> argument = ArgumentCaptor
.forClass(DeleteObjectRequest.class);
verify(s3Client).deleteObject(argument.capture());
DeleteObjectRequest request = argument.getValue();
assertThat(request.getBucketName(), is(S3_BUCKET_NAME));
assertThat(request.getKey(), is(EXPECTED_DATA_STORE_REF));
}
@Override
public void deleteFile(S3Object f) throws FileSystemException {
try {
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, f.getKey());
s3Client.deleteObject(deleteObjectRequest);
} catch (AmazonServiceException e) {
throw new FileSystemException(e);
}
}
public void deleteContent(String p_bucket_name, String p_s3_key) throws AmazonClientException {
if (s3Client.doesBucketExist(p_bucket_name)) {
DeleteObjectRequest delete_req = new DeleteObjectRequest(p_bucket_name, p_s3_key);
s3Client.deleteObject(delete_req);
}
}
public DeleteObjectRequest newDeleteRequest() {
return new DeleteObjectRequest(bucket, key);
}
@Test
public void testJobCommitFailure() throws Exception {
Path jobAttemptPath = jobCommitter.getJobAttemptPath(job);
FileSystem fs = jobAttemptPath.getFileSystem(conf);
Set<String> uploads = runTasks(job, 4, 3);
Assert.assertTrue(fs.exists(jobAttemptPath));
jobCommitter.errors.failOnCommit(5);
TestUtil.assertThrows("Should propagate the commit failure",
AmazonClientException.class, "Fail on commit 5", new Callable<Void>() {
@Override
public Void call() throws IOException {
jobCommitter.commitJob(job);
return null;
}
});
Assert.assertEquals("Should have succeeded to commit some uploads",
5, jobCommitter.results.getCommits().size());
Assert.assertEquals("Should have deleted the files that succeeded",
5, jobCommitter.results.getDeletes().size());
Set<String> commits = Sets.newHashSet();
for (CompleteMultipartUploadRequest commit : jobCommitter.results.getCommits()) {
commits.add(commit.getBucketName() + commit.getKey());
}
Set<String> deletes = Sets.newHashSet();
for (DeleteObjectRequest delete : jobCommitter.results.getDeletes()) {
deletes.add(delete.getBucketName() + delete.getKey());
}
Assert.assertEquals("Committed and deleted objects should match",
commits, deletes);
Assert.assertEquals("Should have aborted the remaining uploads",
7, jobCommitter.results.getAborts().size());
Set<String> uploadIds = getCommittedIds(jobCommitter.results.getCommits());
uploadIds.addAll(getAbortedIds(jobCommitter.results.getAborts()));
Assert.assertEquals("Should have committed/deleted or aborted all uploads",
uploads, uploadIds);
Assert.assertFalse(fs.exists(jobAttemptPath));
}
public List<DeleteObjectRequest> getDeletes() {
return deletes;
}
@Override
public void delete() {
if (delegate.exists()) {
client.deleteObject(new DeleteObjectRequest(bucket, delegate.getFilename()));
}
}
/** Unsupported Operation. */
@Override public void deleteObject(DeleteObjectRequest delObjReq) throws SdkClientException {
throw new UnsupportedOperationException("Operation not supported");
}
public void deleteObjectsAfterVerificationInTarget(String objectKey) throws Exception {
s3client.deleteObject(new DeleteObjectRequest(TARGET_BUCKET_NAME, objectKey));
}
@Override
public String handleRequest(S3Event s3Event, Context context) {
byte[] buffer = new byte[1024];
try {
for (S3EventNotificationRecord record: s3Event.getRecords()) {
String srcBucket = record.getS3().getBucket().getName();
// Object key may have spaces or unicode non-ASCII characters.
String srcKey = record.getS3().getObject().getKey()
.replace('+', ' ');
srcKey = URLDecoder.decode(srcKey, "UTF-8");
// Detect file type
Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
if (!matcher.matches()) {
System.out.println("Unable to detect file type for key " + srcKey);
return "";
}
String extension = matcher.group(1).toLowerCase();
if (!"zip".equals(extension)) {
System.out.println("Skipping non-zip file " + srcKey + " with extension " + extension);
return "";
}
System.out.println("Extracting zip file " + srcBucket + "/" + srcKey);
// Download the zip from S3 into a stream
AmazonS3 s3Client = new AmazonS3Client();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent());
ZipEntry entry = zis.getNextEntry();
while(entry != null) {
String fileName = entry.getName();
String mimeType = FileMimeType.fromExtension(FilenameUtils.getExtension(fileName)).mimeType();
System.out.println("Extracting " + fileName + ", compressed: " + entry.getCompressedSize() + " bytes, extracted: " + entry.getSize() + " bytes, mimetype: " + mimeType);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = zis.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(outputStream.size());
meta.setContentType(mimeType);
s3Client.putObject(srcBucket, FilenameUtils.getFullPath(srcKey) + fileName, is, meta);
is.close();
outputStream.close();
entry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
//delete zip file when done
System.out.println("Deleting zip file " + srcBucket + "/" + srcKey + "...");
s3Client.deleteObject(new DeleteObjectRequest(srcBucket, srcKey));
System.out.println("Done deleting");
}
return "Ok";
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void run() {
AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
AmazonS3 s3Client = new AmazonS3Client(credentials,
new ClientConfiguration());
if (endpoint.contains("amazonaws.com")) {
String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
if (aws_endpoint.contains("US")) {
s3Client.setEndpoint("https://s3.amazonaws.com");
} else if (aws_endpoint.contains("us-west")) {
s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
} else if (aws_endpoint.contains("eu-west")) {
s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
} else if (aws_endpoint.contains("ap-")) {
s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
} else if (aws_endpoint.contains("sa-east-1")) {
s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
} else {
s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
}
} else {
s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
s3Client.setEndpoint(endpoint);
}
try {
if (version != null) {
s3Client.deleteVersion(new DeleteVersionRequest(bucket, what, version));
} else {
s3Client.deleteObject(new DeleteObjectRequest(bucket, what));
}
if (!debug) {
NewJFrame.jTextArea1.append("\nDeleted file: " + what);
}
calibrate();
} catch (AmazonServiceException ase) {
if (NewJFrame.gui) {
mainFrame.jTextArea1.append("\n\nError Message: " + ase.getMessage());
mainFrame.jTextArea1.append("\nHTTP Status Code: " + ase.getStatusCode());
mainFrame.jTextArea1.append("\nAWS Error Code: " + ase.getErrorCode());
mainFrame.jTextArea1.append("\nError Type: " + ase.getErrorType());
mainFrame.jTextArea1.append("\nRequest ID: " + ase.getRequestId());
calibrate();
} else {
System.out.print("\n\nError Message: " + ase.getMessage());
System.out.print("\nHTTP Status Code: " + ase.getStatusCode());
System.out.print("\nAWS Error Code: " + ase.getErrorCode());
System.out.print("\nError Type: " + ase.getErrorType());
System.out.print("\nRequest ID: " + ase.getRequestId());
}
} catch (Exception delete) {
}
}
@Override
public void delete( UUID appId, Entity entity ) throws Exception {
getS3Client().deleteObject(new DeleteObjectRequest(bucketName, AssetUtils.buildAssetKey( appId, entity )));
}
@Override
public void deleteObject(DeleteObjectRequest deleteObjectRequest) throws AmazonClientException,
AmazonServiceException {
// TODO Auto-generated method stub
}
@Override
public void deleteObject(final DeleteObjectRequest request) throws AmazonClientException {
assertThat(request.getBucketName(), equalTo(bucket));
blobs.remove(request.getKey());
}
/**
* Deletes the specified object in the specified bucket
*
* @param bucketName bucket name
* @param key object key
* @throws AmazonClientException If any errors are encountered in the client while making the
* request or handling the response.
* @throws AmazonServiceException If any errors occurred in Amazon S3 while processing the
* request.
*/
protected void startDelete(String bucketName, String key) throws AmazonClientException, AmazonServiceException {
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, key);
amazonS3.deleteObject(deleteObjectRequest);
}