| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The following tasks guide you through using the AWS SDK for Java classes to delete an object.
Deleting an Object (Non-Versioned Bucket)
|
1 |
Create an instance of the
|
|
2 |
Execute one of the
You can provide a bucket name and an object name as
parameters or provide the same information in a
If you have not enabled versioning on the bucket, the operation deletes the object. If you have enabled versioning, the operation adds a delete marker. For more information, see Deleting One Object Per Request. |
The following Java sample demonstrates the preceding steps. The sample uses the
DeleteObjectRequest class to provide a bucket name and an
object key.
AWSCredentials myCredentials = new BasicAWSCredentials(
myAccessKeyID, mySecretKey);
AmazonS3 s3client = new AmazonS3Client(myCredentials);
s3client.deleteObject(new DeleteObjectRequest(bucketName, keyName)); Deleting a Specific Version of an Object (Version-Enabled Bucket)
|
1 |
Create an instance of the
|
|
2 |
Execute one of the
You can provide a bucket name and an object key directly as
parameters or use the |
The following Java sample demonstrates the preceding steps. The sample uses the
DeleteVersionRequest class to provide a bucket name, an object
key, and a version Id.
AWSCredentials myCredentials = new BasicAWSCredentials(
myAccessKeyID, mySecretKey);
AmazonS3 s3client = new AmazonS3Client(myCredentials);
s3client.deleteObject(new DeleteVersionRequest(bucketName, keyName, versionId)); Example 1: Deleting an Object (Non-Versioned Bucket)
The following Java example deletes an object from a bucket. If you have not enabled versioning on the bucket, Amazon S3 deletes the object. If you have enabled versioning, Amazon S3 adds a delete marker and the object is not deleted. For information about how to create and test a working sample, see Testing the Java Code Examples.
import java.io.IOException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
public class S3Sample {
private static String bucketName = "*** Provide a Bucket Name ***";
private static String keyName = "*** Provide a Key Name ****";
public static void main(String[] args) throws IOException {
AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
S3Sample.class.getResourceAsStream("AwsCredentials.properties")));
try {
s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
} 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());
}
}
}Example 2: Deleting an Object (Versioned Bucket)
The following Java example deletes a specific version of an object from a
versioned bucket. The deleteObject request removes the specific object
version from the bucket.
To test the sample, you must provide a bucket name. The code sample performs the following tasks:
Enable versioning on the bucket.
Add a sample object to the bucket. In response, Amazon S3 returns the version ID of the newly added object.
Delete the sample object using the deleteVersion
method. The DeleteVersionRequest class specifies both an
object key name and a version ID.
For information about how to create and test a working sample, see Testing the Java Code Examples.
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Random;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.BucketVersioningConfiguration;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteVersionRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest;
public class S3Sample {
static String bucketName = "*** Provide a Bucket Name ***";
static String keyName = "*** Provide a Key Name ****";
static AmazonS3Client s3Client;
public static void main(String[] args) throws IOException {
s3Client = new AmazonS3Client(new PropertiesCredentials(
S3Sample.class.getResourceAsStream("AwsCredentials.properties")));
try {
// Make the bucket version-enabled.
enableVersioningOnBucket(s3Client, bucketName);
// Add a sample object.
String versionId = putAnObject(keyName);
s3Client.deleteVersion(
new DeleteVersionRequest(
bucketName,
keyName,
versionId));
} 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());
}
}
static void enableVersioningOnBucket(AmazonS3Client s3Client,
String bucketName) {
BucketVersioningConfiguration config = new BucketVersioningConfiguration()
.withStatus(BucketVersioningConfiguration.ENABLED);
SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = new SetBucketVersioningConfigurationRequest(
bucketName, config);
s3Client.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);
}
static String putAnObject(String keyName) {
String content = "This is the content body!";
String key = "ObjectToDelete-" + new Random().nextInt();
ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("Subject", "Content-As-Object");
metadata.setHeader("Content-Length", content.length());
PutObjectRequest request = new PutObjectRequest(bucketName, key,
new ByteArrayInputStream(content.getBytes()), metadata)
.withCannedAcl(CannedAccessControlList.AuthenticatedRead);
PutObjectResult response = s3Client.putObject(request);
return response.getVersionId();
}
}