问题
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.
回答1:
Deleting One Object (Non-Versioned Bucket)
Create instance of S3 client using Aws\S3\S3Client class factory().
$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. (Reference)
EXAMPLE
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 Reference can be found here.
回答2:
After lot of solution i tried i found the solution that works for me perfectly.
$s3 = Storage::disk('s3');
$s3->delete('filename');
回答3:
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
));
回答4:
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
);
来源:https://stackoverflow.com/questions/47605878/delete-a-file-from-s3-bucket