Casisac
Casisac

Reputation: 85

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.

Upvotes: 3

Views: 18304

Answers (6)

This Code Worked For me.

    $s3Client = new S3Client([
    'version'     => 'latest',
    'region'      => 'your region',
    'credentials' => [
        'key'    => '**AWS ACCESS KEY**',
        'secret' => '**AWS SECRET ACCESS KEY**',
    ],
    ]);

    try {
    $result = $s3Client->deleteObject(array(
        'Bucket' => '**YOUR BUCKET**',
        'Key'    => "videos/file.mp4"
        )); 
        return 1;
    } catch (S3Exception $e) {
        return $e->getMessage() . PHP_EOL;
    }

Upvotes: 1

Joseph Biju Cadavil
Joseph Biju Cadavil

Reputation: 313

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.

Upvotes: 7

Fed C
Fed C

Reputation: 303

Unfortunately, Arsalan's answer doesn't appear to work anymore. What worked for me, using access and secret keys was:

    $this->s3 = new S3Client([
      'driver' => 's3',
      'version' => 'latest',
      'region'  => env('AWS_DEFAULT_REGION'),
      'credentials' => [
        'key' => env('AWS_ACCESS_KEY_ID'), 
        'secret' => env('AWS_SECRET_ACCESS_KEY')
      ]
    ]);

    $this->s3->putObject([
      'Bucket' => env('AWS_BUCKET'),  
      'Key'    => env('AWS_SECRET_ACCESS_KEY'),
      'Body'   => $body,
      'Key'    => $key
    ]);

Upvotes: 0

Pawan Verma
Pawan Verma

Reputation: 1269

After lot of solution i tried i found the solution that works for me perfectly.

$s3 = Storage::disk('s3');
$s3->delete('filename');

Upvotes: 2

Arsalan Ahmed Khan
Arsalan Ahmed Khan

Reputation: 436

You can use the aws s3 delete API method which delete the uploaded file. you can achieve it like below.

use Aws\S3\S3Client;

$s3 = new S3(awsAccessKey, awsSecretKey);

$s3->deleteObject("bucketname", filename);

Upvotes: 1

Upul Doluweera
Upul Doluweera

Reputation: 2326

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
));       

Upvotes: 3

Related Questions