Reputation: 85
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
Reputation: 11
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
Reputation: 313
Deleting One Object (Non-Versioned Bucket)
$s3 = S3Client::factory();
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
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
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
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
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