I want to access my S3 bucket, and when I remove a file, use the delete_attachment
hook to call to Amazon and delete those files ( we have a service that offloads files to S3 but doesn't delete them.
I've installed the Amazon Web Services plugin for WordPress but I'm not sure where to go from here.
Given a filename, I can remove it from the command line:
aws s3 rm s3://BUCKET/DIRECTORY/wp-content/uploads/2018/FILENAME.JPG
But I need to do this from PHP.
Say the file being deleted is:
https://example.com/wp-content/uploads/2018/04/file1234.png
Then I have the following code that runs on delete:
// define the delete_attachment callback
function action_delete_from_amazon( $id ) {
$location = get_the_guid( $id ); // gets that url above
$s3_filename = $bucket_name . strstr('/wp-content', $location )
//
// Call the Amazon S3 service here and
// delete all variations of the file...
//
};
// add the action
add_action( 'delete_attachment', 'action_delete_from_amazon', 10, 1 );