简体   繁体   中英

Delete folder in Amazon S3 using PHP

I just started trying out Amazon S3 for hosting my website's images. I'm using the official Amazon AWS PHP SDK library.

Problem: How can I delete all files located in a S3 'folder'?
For example if I have a file named images/2012/photo.jpg , I want to delete all files whose filenames start with images/2012/ .

The best way to delete a folder from S3 with all its files is using the API deleteMatchingObjects()

$s3 = S3Client::factory(...);
$s3->deleteMatchingObjects('YOUR_BUCKET_NAME', '/some/dir');

S3 does not have "folders" as you would traditionally think of them on a file system (some S3 clients just do a nice job making S3 appear to have folders). Those / are actually part of the file name.

As such, there is no "delete folder" option in the API. You would just need to delete each individual file that has the images/2012/... prefix.

Update:

This can be accomplished via the delete_all_objects method in the Amazon S3 PHP Client. Simply specify "/^images\\/2012\\//" as the regex prefix in the second argument (the first argument being your bucket name).

I've tested this and it works 2019-05-28

function Amazon_s3_delete_dir($delPath, $s3, $bucket) {
//the $dir is the path to the directory including the directory
// the directories need to have a / at the end.  
// Clear it just in case it may or may not be there and then add it back in.
$dir = rtrim($dir, "/");
$dir = ltrim($dir, "/");
$dir = $dir . "/";

$response = $s3->getIterator(
        'ListObjects',
        [
            'Bucket' => $bucket,
            'Prefix' => $delPath
        ]
);
//delete each 
foreach ($response as $object) {
    $fileName = $object['Key'];
    $s3->deleteObject([
        'Bucket' => $bucket,
        'Key' => $fileName
    ]);
}//foreach

    return true;
 }//function

Usage:

$delPath = $myDir . $theFolderName . "/";        
Amazon_s3_delete_dir($delPath, $s3, $bucket);
$s3 = new Aws\S3\Client([ 'region' => 'us-west-2', 'version' => 'latest' ]); 
$listObjectsParams = ['Bucket' => 'foo', 'Prefix' => 'starts/with/']; 

// Asynchronously delete 
$delete = Aws\S3\BatchDelete::fromListObjects($s3, $listObjectsParams); 

// Force synchronous completion $delete->delete();
$promise = $delete->promise(); 

Here is a function that will do what you are looking to do.

/**
*   This function will delete a directory.  It first needs to look up all objects with the specified directory
*   and then delete the objects.
*/
function Amazon_s3_delete_dir($dir){
    $s3 = new AmazonS3();

    //the $dir is the path to the directory including the directory

    // the directories need to have a / at the end.  
    // Clear it just in case it may or may not be there and then add it back in.
            $dir = rtrim($dir, "/");
            $dir = ltrim($dir, "/");
            $dir = $dir . "/";

    //get list of directories
        $response = $s3->get_object_list(YOUR_A3_BUCKET, array(
           'prefix' => $dir
        ));


    //delete each 
        foreach ($response as $v) {
            $s3->delete_object(YOUR_A3_BUCKET, $v);
        }//foreach

    return true;

}//function

Use: if I want to delete the directory foo

Amazon_s3_delete_dir("path/to/directory/foo/");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM