13

I created a new amazon bucket called "photos". The bucket url is something like:

www.amazons3.salcaiser.com/photos

Now I upload subfolders containing files, into that bucket for example

www.amazons3.salcaiser.com/photos/thumbs/file.jpg

My questions are, does thumbs/ is assumed a new bucket or is it an object?

Then if I want to delete the entire thumbs/ directory need I first to delete all files inside that or can I delete all in one time?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
itsme
  • 48,972
  • 96
  • 224
  • 345
  • 2
    there are no folders in s3 you would delete all objects with the same prefix –  Aug 20 '13 at 23:29

4 Answers4

21

In the case you are describing, "photos" is the bucket. S3 does not have sub-buckets or directories. Directories are simulated by using slashes in the object key. "thumbs/file.jpg" is an object key and "thumbs/" would be considered a key prefix.

Dagon's examples are good and use the older version 1.x of the AWS SDK for PHP. However, you can do this more easily with the newest 2.4.x version AWS SDK for PHP which includes a helper method for deleting multiple objects.

<?php

// Include the SDK. This line depends on your installation method.
require 'aws.phar';

use Aws\S3\S3Client;

$s3 = S3Client::factory(array(
    'key'    => 'your-aws-access-key',
    'secret' => 'your-aws-secret-key',
));

// Delete the objects in the "photos" bucket with the a prefix of "thumbs/"
$s3->deleteMatchingObjects('photos', 'thumbs/');
Jeremy Lindblom
  • 6,437
  • 27
  • 30
  • this is quite awesome, do you know if deleteMatchingObjects() launchs multiple or single request to s3? – itsme Aug 21 '13 at 19:41
  • 1
    There is no possible way to do this without executing multiple requests. However, it does try to do as few as possible by using S3's [multi-object delete API](http://docs.aws.amazon.com/AmazonS3/latest/API/multiobjectdeleteapi.html) under the hood. – Jeremy Lindblom Aug 22 '13 at 17:47
  • @JeremyLindblom Is it possible to use this method and the key to delete all occurrences ? Ie. different prefixes but same filename. small/a.png , thumb/a.png , large/a.png ? – Gayan Hewa Aug 12 '14 at 03:28
  • I think the regex param from the method signature will work. public function deleteMatchingObjects($bucket, $prefix = '', $regex = '', array $options = array()) , awesome. – Gayan Hewa Aug 12 '14 at 03:41
  • dont know but i am using phpsdk-v3 and its not working anymore – NullPoiиteя Oct 27 '15 at 06:16
  • $s3->deleteMatchingObjects('photos', 'thumbs/'); this function return any thing to identify deleted successfully execute? – Kailas Jan 04 '19 at 07:12
2
//Include s3.php file first in code

if (!class_exists('S3'))
            require_once('S3.php');
        //AWS access info
        if (!defined('awsAccessKey'))
            define('awsAccessKey', 'awsAccessKey');
        if (!defined('awsSecretKey'))
            define('awsSecretKey', 'awsSecretKey');
        //instantiate the class
        $s3 = new S3(awsAccessKey, awsSecretKey);

  if ($s3->deleteObject("bucketname", `filename`)) {
        echo 'deleted';
}
else
{
echo 'no file found';
}
Ankit Aranya
  • 930
  • 1
  • 10
  • 17
1

found some code snippets for 'directory' deletion - i did not write them:

PHP 5.3+:

$s3 = new AmazonS3();

$bucket = 'your-bucket';
$folder = 'folder/sub-folder/';

$s3->get_object_list($bucket, array(
    'prefix' => $folder
))->each(function($node, $i, $s3) {
    $s3->batch()->delete_object($bucket, $node);
}, array($s3));
$responses = $s3->batch()->send();

var_dump($responses->areOK());

Older PHP 5.2.x:

$s3 = new AmazonS3();


$bucket = 'your-bucket';
$folder = 'folder/sub-folder/';

$s3->get_object_list($bucket, array(
    'prefix' => $folder
))->each('construct_batch_delete', array($s3));

function construct_batch_delete($node, $i, &$s3)
{
    $s3->batch()->delete_object($bucket, $node);
}

$responses = $s3->batch()->send();

var_dump($responses->areOK());
  • thank you for the lovely piece of code! from the code above i can see i need to list and delete all by one the objects which are not subfolders ;) – itsme Aug 21 '13 at 00:17
0

I have implemented this in Yii as,

$aws = Yii::$app->awssdk->getAwsSdk();
$s3 = $aws->createS3();
$s3->deleteMatchingObjects('Bucket Name','object key');
AmarjaPatil4
  • 1,640
  • 3
  • 28
  • 41