| Did this page help you? Yes No Tell us about it... |
The following tasks guide you through using the PHP classes to delete multiple objects. For more information about versioning, see Using Versioning.
Deleting Multiple Objects (Non-Versioned Bucket)
|
1 |
Create an instance of the |
|
2 |
Execute the |
The following PHP code sample demonstrates the preceding steps.
// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->delete_objects ($bucket, array(
'objects' => array(
array('key' => $keyname1),
array('key' => $keyname2)
)
));Amazon S3 returns a response that shows the objects that were deleted and objects it could not delete because of errors (for example, permission errors). The following PHP code sample prints the object keys for objects that were deleted. It also prints the object keys that were not deleted and related error messages.
header('Content-Type: text/plain; charset=utf-8');
// Process response.
echo 'Printing object keys that were deleted successfully:' . PHP_EOL;
foreach ($response->body->Deleted as $item ) {
echo('Key = ' . (string) $item->Key . ', ' .
'VersionId = ' . (string)$item->VersionId .
PHP_EOL);
}
echo PHP_EOL;
echo 'The following objects could not be deleted:' . PHP_EOL;
foreach ($response->body->Error as $item) {
echo 'Key= ' . (string)$item->Key . ', ' .
'VersionId = ' . (string)$item->VersionId .
PHP_EOL;
echo 'Code = ' . (string)$item->Code . ', ' .
'Message = ' . (string)$item->Message .
PHP_EOL;
}The following tasks guide you through deleting objects from a version-enabled bucket.
Deleting Multiple Objects (Version-Enabled Bucket)
|
1 |
Create an instance of the |
|
2 |
Execute the If you specify version ID of the object that you want to delete, Amazon S3 deletes the specific object version. If you don't specify the version ID of the object that you want to delete, Amazon S3 adds a delete marker. For more information, see Deleting One Object Per Request. |
The following PHP code sample demonstrates the preceding steps.
// Instantiate the class.
$s3 = new AmazonS3();
$response = $s3->delete_objects
($bucket,
array('objects' => array(
array('key' => $keyname1, 'version_id' => $versionId1),
array('key' => $keyname2, 'version_id' => $versionId2)
)
));Example 1: Multi-Object Delete (Non-Versioned Bucket)
The following PHP code example uses the Multi-Object API to delete objects
from a bucket that is not version-enabled. The example first uploads the sample
objects to the bucket and then uses the delete_objects method to delete
the objects in a single request. In the delete_objects method, the
example specifies only the object key names because the version IDs are null.
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***;
// Instantiate the class.
$s3 = new AmazonS3();
// Upload the sample objects.
$keys = create_objects(3, $bucket, $s3);
// Delete specific object versions.
$response = $s3->delete_objects($bucket, array(
'objects' => $keys,
'quiet' => 'false'
));
// Success?
var_dump($response->isOK());
// Create the sample objects.
function create_objects($number, $bucket, $s3)
{
$keys = array();
$content = "This is the content body!";
for ($i=1; $i < $number + 1; $i++){
$keyName = "ObjectToDelete-" . mt_rand();
$response = $s3->create_object($bucket, $keyName, array(
'body' => $content,
'acl' => AmazonS3::ACL_AUTH_READ,
'contentType' => 'text/plain',
));
$keys[$i] = array('key' => $keyName);
}
return $keys;
}
?> Example 2: Multi-Object Delete (Version-Enabled Bucket)
The following PHP code example uses the Multi-Object API to delete objects
from a version-enabled bucket. In addition to showing the
delete_objects Multi-Object Delete API usage, it also
illustrates how versioning works in a version-enabled bucket.
Before you can test the sample, you must create a sample bucket and provide the bucket name in the example. You can use the AWS Management Console to create a bucket.
The example performs the following actions:
Enable versioning on the bucket.
Perform a versioned-delete.
The example first uploads the sample objects. In response, Amazon S3 returns the version IDs for each sample object that you uploaded. The example then deletes these objects using the Multi-Object Delete API. In the request, it specifies both the object keys and the version IDs (that is, versioned delete).
Perform a non-versioned delete.
The example uploads the new sample objects. Then, it deletes the objects using the Multi-Object API. However, in the request, it specifies only the object keys. In this case, Amazon S3 adds the delete markers and the objects disappear from your bucket.
Delete the delete markers.
To illustrate how the delete markers work, the sample deletes the delete markers. In the Multi-Object Delete request, it specifies the object keys and the version IDs of the delete markers it received in the response in the preceding step. This action makes the objects reappear in your bucket.
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***;
// Instantiate the class.
$s3 = new AmazonS3();
// 1. Enable versioning on the bucket.
$response = $s3->enable_versioning($bucket);
// 2a. Upload the sample objects.
$keys = create_objects(3, $bucket, $s3);
// 2b. Delete specific object versions.
$response = $s3->delete_objects($bucket, array(
'objects' => $keys,
'quiet' => 'false'
));
// 3a. Upload the sample objects.
$keys = create_objects(3, $bucket, $s3);
// 3b. Delete the objects using only keys. Amazon S3 creates a delete marker and
// returns its version Id in the response.
$justKeys = array();
for ($i=1; $i < count($keys)+1; $i++) {
$justKeys[$i] = array('key' => $keys[$i]['key']);
}
$response = $s3->delete_objects($bucket, array(
'objects' => $justKeys,
'quiet' => 'false'
));
// Success?
var_dump($response->isOK());
// 3c. Additional exercise - using a multi-object versioned delete, remove the
// delete markers received in the preceding response. This results in your objects
// reappearing in your bucket.
$keysAndVersions = array();
$i = 1;
foreach ($response->body->Deleted as $item ) {
$keysAndVersions[$i] = array(
'key' => $item->Key,
'version_id' => $item->VersionId);
}
// Create a request to delete the delete markers.
// Deleting the delete markers brings the objects back to the bucket.
$response = $s3->delete_objects ($bucket, array(
'objects' => $keysAndVersions,
'quiet' => 'false'
));
// Create the sample objects.
function create_objects($number, $bucket, $s3)
{
$keys = array();
$content = "This is the content body!";
for ($i=1; $i < $number + 1; $i++){
$keyName = "ObjectToDelete-" . mt_rand();
$response = $s3->create_object($bucket, $keyName, array(
'body' => $content,
'acl' => AmazonS3::ACL_AUTH_READ,
'contentType' => 'text/plain',
));
$keys[] = array('key' => $keyName);
}
return $keys;
}
?>