Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a file from s3 bucket

I've created an upload and download service in php-symfony2. This is working fine. Now I want to delete the uploaded file. Any Example?

Note: No data storing to database tables.

like image 557
Casisac Avatar asked Oct 15 '25 04:10

Casisac


2 Answers

Deleting One Object (Non-Versioned Bucket)

  1. Create instance of S3 client using Aws\S3\S3Client class factory().

$s3 = S3Client::factory();

  1. Execute the Aws\S3\S3Client::deleteObject() method with bucket name and a key name.

    $result = $s3->deleteObject(array( 'Bucket' => $bucket, 'Key' => $keyname ));

If versioning is enabled DELETE MARKER Will be added. (References)

EXAMPLE

<?php

    require 'vendor/autoload.php';
    use Aws\S3\S3Client;
    $s3 = S3Client::factory();
    
    $bucket = '*** Your Bucket Name ***';
    $keyname = '*** Your Object Key ***';
    
    $result = $s3->deleteObject(array(
        'Bucket' => $bucket,
        'Key'    => $keyname
    ));

More References can be found here.

like image 171
Joseph Biju Cadavil Avatar answered Oct 17 '25 17:10

Joseph Biju Cadavil


You can use deleteObject() method, refer the doc.

use Aws\S3\S3Client;

$s3 = S3Client::factory();

$bucket = '*** Your Bucket Name ***';
$keyname = '*** Your Object Key ***';

$result = $s3->deleteObject(array(
    'Bucket' => $bucket,
    'Key'    => $keyname
));       
like image 24
Upul Doluweera Avatar answered Oct 17 '25 19:10

Upul Doluweera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!