You are here

function ValidateService::copyFileSystemToS3 in S3 File System 8.2

Copies all the local files from the specified file system into S3.

Parameters

array $config: An s3fs configuration array.

$scheme: A variable defining which scheme (Public or Private) to copy.

File

src/ValidateService.php, line 177

Class

ValidateService
Defines a ValidateService service.

Namespace

Drupal\s3fs

Code

function copyFileSystemToS3($config, $scheme) {
  if ($scheme == 'public') {
    $source_folder = realpath(PublicStream::basePath());
    $target_folder = !empty($config['public_folder']) ? $config['public_folder'] . '/' : 's3fs-public/';
  }
  else {
    if ($scheme == 'private') {
      $source_folder = PrivateStream::basePath() ? PrivateStream::basePath() : '';
      $source_folder_real = realpath($source_folder);
      if (empty($source_folder) || empty($source_folder_real)) {
        drupal_set_message('Private file system base path is unknown. Unable to perform S3 copy.', 'error');
        return;
      }
      $target_folder = !empty($config['private_folder']) ? $config['private_folder'] . '/' : 's3fs-private/';
    }
  }
  if (!empty($config['root_folder'])) {
    $target_folder = "{$config['root_folder']}/{$target_folder}";
  }

  // Create S3 object to move files.
  $s3 = $this
    ->getAmazonS3Client($config);
  $file_paths = $this
    ->dirScan($source_folder);
  foreach ($file_paths as $path) {
    $relative_path = str_replace($source_folder . '/', '', $path);
    print "Copying {$scheme}://{$relative_path} into S3...\n";

    // Finally get to make use of S3fsStreamWrapper's "S3 is actually a local
    // file system. No really!" functionality.
    copy($path, "s3fs://{$relative_path}");
  }
  drupal_set_message($this
    ->t('Copied all local %scheme files to S3.', [
    '%scheme' => $scheme,
  ]), 'status');
}