| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
You can delete an object from a bucket. If you have versioning enabled on the bucket, you can also delete a specific version of an object.
The following tasks guide you through using the PHP classes to delete an object. For more information about versioning, see Using Versioning.
Deleting One Object (Non-Versioned Bucket)
|
1 |
Create an instance of the |
|
2 |
Execute the 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 PHP code sample demonstrates the preceding steps using the
delete_object() method.
// Instantiate the class. $s3 = new AmazonS3(); $response = $s3->delete_object($bucket, $keyname);
Deleting a Specific Version of an Object (Version-Enabled Bucket)
|
1 |
Create an instance of the |
|
2 |
Execute the The method deletes the specific version of the object. |
The following PHP code sample demonstrates the preceding steps.
// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->delete_object($bucket, $keyname, array (
'versionId' =>$versionId)); Example 1: Deleting an Object (Non-Versioned Bucket)
The following PHP code example deletes an object from a bucket. It does not provide a version Id in the delete request. 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 Using the AWS SDK for PHP.
<?php require_once '../sdk.class.php'; $bucket = '*** Provide a Bucket Name ***'; $keyname = '*** Provide a Key Name ***'; // Instantiate the class. $s3 = new AmazonS3(); // Delete the object. $response = $s3->delete_object($bucket, $keyname); // Success? var_dump($response->isOK()); ?>
Example 2: Deleting an Object (Versioned Bucket)
The following PHP code example deletes an object from a versioned-bucket. The
delete_object() method specifies an object key name and a version
ID and 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 delete_object()
method.
For information about how to create and test a working sample, see Using the AWS SDK for PHP.
<?php
require_once '../sdk.class.php';
$bucket = '***Provide Bucket Name***;
$keyname = '***Provide Object Key Name****';
// Instantiate the class.
$s3 = new AmazonS3();
// Enable versioning on the bucket.
$response = $s3->enable_versioning($bucket);
// Add a sample object.
$versionId = create_object($keyname, $bucket, $s3);
// Delete the object.
$response = $s3->delete_object($bucket,$keyname, array(
'versionId' => $versionId));
// Success?
var_dump($response->isOK());
// Create a sample object.
function create_object($keyname, $bucket, $s3)
{
$keys = array();
$content = "This is the content body!";
$response = $s3->create_object($bucket, $keyname, array(
'body' => $content,
'acl' => AmazonS3::ACL_AUTH_READ,
'contentType' => 'text/plain'
)
);
return $response->header['x-amz-version-id'];
}
?>