Redirect local files to S3

Created on 19 April 2023, about 2 years ago

Hello,
I just moved a large website to s3.
I copied all the files manully 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);
        }
      }
    
  • Status changed to Closed: outdated 3 months ago
  • 🇺🇸United States cmlara

    Drupal 7 end-of-life triage:
    Drupal 7 reached end of life on January 5th.

    The 7.x branches of S3FS do not have any additional planned releases.

    This issue is not specifically S3FS related and no such feature is planned in newer version of the module.

    The methods for this would be applicable to any sort of deployment of Drupal where files have moved. I would encourage site owners to consider the possibility of redirecting at the server config level to avoid a Drupal bootstrap.

Production build 0.71.5 2024