humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.93k stars 389 forks source link

fopen() Mode not supported: a+ #550

Open Mastergalen opened 3 years ago

Mastergalen commented 3 years ago

I'm trying to generate a new report in WooCommerce Admin, which is trying to write the .csv report to wp-content/uploads/woocommerce_uploads/reports/wc-orders-report-export-16327718422508.csv

ErrorException: User Warning: Mode not supported: a+. Use one 'r', 'w', 'a', or 'x'.
  File "/wp-content/plugins/s3-uploads/inc/class-stream-wrapper.php", line 980, in S3_Uploads\Stream_Wrapper::triggerError
    trigger_error( implode( "\n", (array) $errors ), E_USER_WARNING ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  File "/wp-content/plugins/s3-uploads/inc/class-stream-wrapper.php", line 165, in S3_Uploads\Stream_Wrapper::stream_open
    return $this->triggerError( $errors );
  File "/wp-content/plugins/woocommerce/includes/export/abstract-wc-csv-batch-exporter.php", line 133, in WC_CSV_Batch_Exporter::write_csv_data
    $fp = fopen( $this->get_file_path(), 'a+' );
  File "/wp-content/plugins/woocommerce/includes/export/abstract-wc-csv-batch-exporter.php", line 118, in WC_CSV_Batch_Exporter::generate_file
    $this->write_csv_data( $this->get_csv_data() );
  File "/wp-content/plugins/woocommerce/packages/woocommerce-admin/src/ReportExporter.php", line 123, in Automattic\WooCommerce\Admin\ReportExporter::export_report
    $exporter->generate_file();
...
(19 additional frame(s) were not displayed)

I suppose there are no plans to support a+ mode, so is there perhaps a way to have an exclude list of paths which the S3-uploads plugin ignore, and allows plugins to write to the local file system instead?

I'm currently working around this by disabling the S3 plugin temporarily when generating the report. Obviously, that's not great in a production environment as it breaks all the images while disabled 😅.

brandwaffle commented 3 years ago

Got the same error and the same deactivation workaround worked for me.

rkajbaf commented 2 years ago

I have the same issue when trying to import/export. Export can write to S3 but can't read from it. Import can't move the local uploaded file over to S3 to attempt the import. Disabling this plugin gave an error about not being able to create the media folder (probably because its ephemeral/scratch data storage maybe? Either way this should be working, and the fact that we can write to s3 from export but not move to s3 from import upload is confusing.

mhodge commented 2 years ago

According to the AWS docs the modes supported include (r, w, a, x). As you say it would error if you using a+ as the library s3 uploads uses to handle the fopen will not support it.

https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-stream-wrapper.html

What you could do is copy the [plugins/woocommerce/includes/export/abstract-wc-csv-batch-exporter.php] file into your theme, change the a+ to be a and include it in your functions. I have tested it and it works perfectly.

I have also created a PR request as per https://github.com/woocommerce/woocommerce/pull/33652/files to hopefully see if WooCommerce would accept my filter proposition to allow an fopen orride more "easily"

hagedigital commented 7 months ago

Found a workaround that works well

add_filter( 'woocommerce_csv_exporter_fopen_mode', 'custom_woocommerce_csv_exporter_fopen_mode' ); function custom_woocommerce_csv_exporter_fopen_mode( $mode ) { return 'a'; }