Error playing video file loaded on S3 bucket in a JW-Player playlist
-
Hi there,
this email follows my thread with the jwplayer support team about an error I’ve found using the jwplayer plugin when Amazon AWS+S3 plugin is in use on WordPress. I’m using WP ver 4.3.
Your last plugin update (WP Offload S3 ver 0.9.2) is still affected too, so I’ve decided to send you my patch that fix the problem (at least in my case but I think is general), hoping you may consider to include it in the next plugin updates. In fact, now we are forced to rewrite the patch by hand every time your plugin is updated.The problem:
Our WP use the AWS+S3 plugins (namely Amazon Web Services ver 0.3.1 + WP Offload S3 ver 0.9.2) so that it uploads media elements (es. videos) on the WP hosting server, then automatically loads them on S3 bucket and removes them from the WP hosting server. Now, in this condition (video on S3 and removed from the local host) If I try to add the file <video_file_name> to a JW-Player playlist picking it from the WP Media Library, it is added to the jwplayer playlist but it doesn’t play reporting “Error loading media: file not found”.
This because it tries to stream the video from the local WP hosting server, i.e. from “https://<my_local_hosting_server>/wp-content/uploads/<video_file_name>” , rather than from Amazon CDN i.e. “https://myCDName.cloudfront.net/wp-content/uploads/<video_file_name>” as it rather should be.The solution (that works fine):
I’ve modified the function upload_attachment_to_s3 located in the file amazon-s3-and-cloudfront.php. In the following the modified function with the changes starting from the line “//hack- Giuseppe-Maio” up to the function end:/** * Upload attachment to S3 * * @param int $post_id * @param array|null $data * @param string|null $file_path * @param bool $force_new_s3_client if we are uploading in bulk, force new S3 client * to cope with possible different regions * @param bool $remove_local_files * * @return array|WP_Error $s3object */ function upload_attachment_to_s3( $post_id, $data = null, $file_path = null, $force_new_s3_client = false, $remove_local_files = true ) { if ( is_null( $data ) ) { $data = wp_get_attachment_metadata( $post_id, true ); } if ( is_null( $file_path ) ) { $file_path = get_attached_file( $post_id, true ); } // Check file exists locally before attempting upload if ( ! file_exists( $file_path ) ) { return new WP_Error( 'exception', sprintf( __( 'File %s does not exist', 'as3cf' ), $file_path ) ); } $file_name = basename( $file_path ); $type = get_post_mime_type( $post_id ); $allowed_types = $this->get_allowed_mime_types(); // check mime type of file is in allowed S3 mime types if ( ! in_array( $type, $allowed_types ) ) { return new WP_Error( 'exception', sprintf( __( 'Mime type %s is not allowed', 'as3cf' ), $type ) ); } $acl = self::DEFAULT_ACL; // check the attachment already exists in S3, eg. edit or restore image if ( ( $old_s3object = $this->get_attachment_s3_info( $post_id ) ) ) { // use existing non default ACL if attachment already exists if ( isset( $old_s3object['acl'] ) ) { $acl = $old_s3object['acl']; } // use existing prefix $prefix = trailingslashit( dirname( $old_s3object['key'] ) ); // use existing bucket $bucket = $old_s3object['bucket']; // get existing region if ( isset( $old_s3object['region'] ) ) { $region = $old_s3object['region']; }; } else { // derive prefix from various settings if ( isset( $data['file'] ) ) { $time = $this->get_folder_time_from_url( $data['file'] ); } else { $time = $this->get_attachment_folder_time( $post_id ); $time = date( 'Y/m', $time ); } $prefix = $this->get_file_prefix( $time, $post_id ); // use bucket from settings $bucket = $this->get_setting( 'bucket' ); $region = $this->get_setting( 'region' ); if ( is_wp_error( $region ) ) { $region = ''; } } $acl = apply_filters( 'wps3_upload_acl', $acl, $type, $data, $post_id, $this ); // Old naming convention, will be deprecated soon $acl = apply_filters( 'as3cf_upload_acl', $acl, $data, $post_id ); $s3object = array( 'bucket' => $bucket, 'key' => $prefix . $file_name, 'region' => $region, ); // store acl if not default if ( $acl != self::DEFAULT_ACL ) { $s3object['acl'] = $acl; } $s3client = $this->get_s3client( $region, $force_new_s3_client ); $args = array( 'Bucket' => $bucket, 'Key' => $prefix . $file_name, 'SourceFile' => $file_path, 'ACL' => $acl, ); // If far future expiration checked (10 years) if ( $this->get_setting( 'expires' ) ) { $args['Expires'] = date( 'D, d M Y H:i:s O', time() + 315360000 ); } $args = apply_filters( 'as3cf_object_meta', $args, $post_id ); do_action( 'as3cf_upload_attachment_pre_remove', $post_id, $s3object, $prefix, $args ); $files_to_remove = array(); if ( file_exists( $file_path ) ) { $files_to_remove[] = $file_path; try { $s3client->putObject( $args ); } catch ( Exception $e ) { $error_msg = sprintf( __( 'Error uploading %s to S3: %s', 'as3cf' ), $file_path, $e->getMessage() ); error_log( $error_msg ); return new WP_Error( 'exception', $error_msg ); } } delete_post_meta( $post_id, 'amazonS3_info' ); add_post_meta( $post_id, 'amazonS3_info', $s3object ); $file_paths = $this->get_attachment_file_paths( $post_id, true, $data ); $additional_images = array(); foreach ( $file_paths as $file_path ) { if ( ! in_array( $file_path, $files_to_remove ) ) { $additional_images[] = array( 'Key' => $prefix . basename( $file_path ), 'SourceFile' => $file_path, ); $files_to_remove[] = $file_path; } } foreach ( $additional_images as $image ) { try { $args = array_merge( $args, $image ); $args['ACL'] = self::DEFAULT_ACL; $s3client->putObject( $args ); } catch ( Exception $e ) { error_log( 'Error uploading ' . $args['SourceFile'] . ' to S3: ' . $e->getMessage() ); } } if ( $remove_local_files ) { if ( $this->get_setting( 'remove-local-file' ) ) { if ( isset( $_POST['action'] ) && 'image-editor' == sanitize_key( $_POST['action'] ) && defined( 'DOING_AJAX' ) && DOING_AJAX ) { // remove original main image after edit $meta = get_post_meta( $post_id, '_wp_attachment_metadata', true ); $original_file = trailingslashit( dirname( $file_path ) ) . basename( $meta['file'] ); if ( file_exists( $original_file ) && ! in_array( $original_file, $files_to_remove ) ) { $files_to_remove[] = $original_file; } } $this->remove_local_files( $files_to_remove ); //hack- Giuseppe-Maio //Erase the row in post meta related to the media containing the local link delete_post_meta( $post_id, '_wp_attached_file' ); //Extract the bucket domain with the path specified in the settings $domain_bucket = $this->get_s3_url_domain( $s3object['bucket'], $region, $args['Expires'] ); //Add row in post meta related to the media with the new link add_post_meta( $post_id, '_wp_attached_file', $domain_bucket . $prefix . $file_name ); global $wpdb; $wpdb->update( $wpdb->prefix . 'posts', array( 'guid' => 'https://'.$domain_bucket .'/'. $prefix . $file_name, // string ), array( 'ID' => $post_id ), array( '%s' ), array( '%d' ) ); } } return $s3object; }
Hoping this helps and looking forward to hear from you on the matter.
Best regards,Giuseppe
https://www.ads-software.com/plugins/amazon-s3-and-cloudfront/
- The topic ‘Error playing video file loaded on S3 bucket in a JW-Player playlist’ is closed to new replies.