Redirect old local files to S3

Created on 19 April 2023, over 1 year ago
Updated 27 April 2023, over 1 year ago

Hello,
I just moved a large website to s3.
I copied all the files manually as they were around 2.000.000 to my bucket and then I set up everything and made the s3fs cache.

Everything works smoothly, but I have a problem with the old files as they were indexed so I keep getting on my server 404s.
Is there a way to 301 the old file to the s3 new url like this:

/sites/default/files/filename.jpg ---> https://my.cdn.it/s3fs-public/filename.jpg

Thank you.

💬 Support request
Status

Active

Component

Miscellaneous

Created by

🇮🇹Italy andrea.cavattoni

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @andrea.cavattoni
  • 🇮🇹Italy andrea.cavattoni

    I was planning to make a script and create redirects with https://www.drupal.org/project/redirect to all files in the db in the public folder as I was lookin in the module code and I dont see any redirect functionality.
    Please let me know

  • 🇮🇹Italy andrea.cavattoni

    Just made the script, sounds good or should we take a different approach:

    //LOAD S3 FILES AND NOT FOLDERS
      $query = db_select('s3fs_file','sf');
      $query->fields('sf');
      $query->condition('sf.dir',0);
      $files = $query->execute()->fetchAll();
      
      foreach($files as $f){
        //LOAD LANGUAGE
        //TODO CHECK DIFFERENT FILE LANGUAGES
        global $language;
        //BUILD THE PATH FOR REDIRECT
        $path = array(
          'source' => str_replace('public:/', 'sites/default/files', $f->uri),
          'destination' => file_create_url($f->uri),
          'language' => $language->language
        );
        //CREATE THE REDIRECT
        $redirect = new stdClass();
        redirect_object_prepare($redirect);
        $redirect->source = $path['source'];
        $redirect->redirect = $path['destination'];
        $redirect->language = $path['language'];
        //CHECK IF REDIRECT EXISTS
        $hash = redirect_hash($redirect);
        $existing = redirect_load_by_hash($hash);
        if (!$existing) {
          redirect_save($redirect);
        }
      }
    
Production build 0.71.5 2024