• Resolved twcfan92

    (@twcfan92)


    My files get uploaded in WordPress to a custom path, based on the post name. So if I’m uploading an image within a post or page called “test,” the file gets uploaded to uploads/test/file.jpg. For some reason, this path doesn’t transfer over to my S3 bucket when this plugin copies the file there. It just uploads everything to the root. Is there a way to fix this? I tried the following code, but it didn’t work:

    add_filter('wp_update_attachment_metadata', 'wp_update_attachment_metadata_2', 90, 2);
    
    function wp_update_attachment_metadata_2($data, $post_id)
    {
        $guid = get_the_guid($post_id);
        $guid = str_replace('http://' .  $_SERVER['HTTP_HOST'] . '/wp/wp-content/uploads/', '', $guid);
        $data['file'] = $guid;
    
        return $data;
    }

    https://ww.wp.xz.cn/plugins/amazon-s3-and-cloudfront/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor A5hleyRich

    (@a5hleyrich)

    Take a look at the as3cf_object_meta filter.

    https://github.com/deliciousbrains/wp-amazon-s3-and-cloudfront-tweaks/blob/master/amazon-s3-and-cloudfront-tweaks.php#L138

    The defaults are as follows:

    $args = array(
    	'Bucket' => $bucket,
    	'Key' => $prefix . $file_name,
    	'SourceFile' => $file_path,
    	'ACL' => $acl,
    	'ContentType' => $type,
    	'CacheControl' => 'max-age=31536000',
    	'Expires' => date( 'D, d M Y H:i:s O', time() + 31536000 ),
    );

    You will need to modify the Key argument for a custom upload location.

    Thread Starter twcfan92

    (@twcfan92)

    Thanks for your reply. I tried the following, but it didn’t work. It still uploads to the root. In fact, it seems to ignore the code altogether. I tried replacing ‘wp/’ with gibberish, but the path didn’t change after I uploaded a file.

    add_filter('as3cf_object_meta', 'as3cf_update_meta', 10, 2);
    
    function as3cf_update_meta($args, $post_id)
    {
        $guid = get_the_guid($post_id);
        $guid = str_replace('http://' .  $_SERVER['HTTP_HOST'] . '/wp/wp-content/uploads/', '', $guid);
    
        $args['Key'] = 'wp/' . $guid;
    
        return $args;
    }

    Plugin Contributor A5hleyRich

    (@a5hleyrich)

    Sorry I only gave you half the steps in the previous response. The filter provided should upload the file to the correct location on S3 (visible by using the AWS console). However, you also need to inform WordPress of the different location. To do this you need to filter the values before they’re added to the postmeta table. Try the sanitize_post_meta_amazonS3_info hook.

    Thread Starter twcfan92

    (@twcfan92)

    Thanks for the reply. I actually figured this out the other day. For the benefit of anyone else trying to do the same thing, here’s the code I used. In my add_attachment hook, I added the following:

    add_action('as3cf_setting_object-prefix', 'setS3Path_' . $fcategory2, 10, 1);

    $fcategory2 is my attachment category, which is what I’m using to determine which folder to put the file in. The function setS3Path_X looks something like this…

    function setS3Path_audio($value)
    {
        return 'wp/audio';
    }
    
    function setS3Path_video($value)
    {
        return 'wp/video';
    }

    …with audio and video being different attachment categories.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Upload using the same file path’ is closed to new replies.