S3-Uploads icon indicating copy to clipboard operation
S3-Uploads copied to clipboard

Bypass certain paths when uploading?

Open NizarBlond opened this issue 3 years ago • 3 comments

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//cache" to be uploaded to S3, but only if it was uploaded via media library.

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?

NizarBlond avatar May 13 '21 14:05 NizarBlond

Have you found a good workaround for this? I need to bypass certain paths too and was hoping there was a good way.

cwebz avatar Jun 10 '21 20:06 cwebz

I too would find this useful.

jordan26 avatar Aug 17 '23 08:08 jordan26

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

jordan26 avatar Aug 17 '23 10:08 jordan26