S3-Uploads
S3-Uploads copied to clipboard
Bypass certain paths when uploading?
Hi folks,
Problem
I realized that the stream wrapper is registered for all uploaded files to uploads_dir. Do you have any quick solution for the limiting S3 uploads only to attachments?
Example:
I don't want files generated by plugins such as cache files that are copied to "wp-content/uploads/
Non-working solution (but the idea might be helpful)
I tried to register the "S3 stream wrapper" before and after attachment is handled by registering the following hooks in "Plugin.php"
add_filter('wp_handle_sideload_prefilter', [ $this, 'register_s3_stream_wrapper_on_upload' ]); add_filter('wp_handle_upload_prefilter', [ $this, 'register_s3_stream_wrapper_on_upload' ]); add_filter('wp_generate_attachment_metadata', [ $this, 'unregister_s3_stream_wrapper_on_upload' ], 99, 2);
that invoke the following methods
public function register_s3_stream_wrapper_on_upload($file) { if (!$this->is_wrapper_registered) { $this->register_stream_wrapper(); $this->is_wrapper_registered = true; } return $file; }
public function unregister_s3_stream_wrapper_on_upload($metadata, $attachment_id) { if ($this->is_wrapper_registered) { stream_wrapper_unregister( 's3' ); $this->is_wrapper_registered = false; } return $metadata; }
Any ideas?
Have you found a good workaround for this? I need to bypass certain paths too and was hoping there was a good way.
I too would find this useful.
In my use-case, I need to prevent a certain file being uploaded at all. I don't know how to do that yet, however I at least managed to find a filter which lets me set it to private
, as this file contains API keys etc.
/**
* Change file(s) to private via S3Uploads (Digital Ocean) when the Key (filename) matches a certain string.
*/
add_filter( 's3_uploads_putObject_params', function( $params ) {
if ( strpos( $params['Key'], 'examplefile.log' ) !== false ) {
$params['ACL'] = 'private';
}
return $params;
} );