You are here

protected function S3fsFileService::putObject in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/S3fsFileService.php \Drupal\s3fs\S3fsFileService::putObject()

Upload a file that is not in the bucket.

Parameters

string $source: Source file to be copied.

string $destination: Destination path in bucket.

Return value

bool True if successful, else FALSE.

3 calls to S3fsFileService::putObject()
S3fsFileService::copy in src/S3fsFileService.php
Copies a file to a new location without invoking the file API.
S3fsFileService::move in src/S3fsFileService.php
Moves a file to a new location without database changes or hook invocation.
S3fsFileService::moveUploadedFile in src/S3fsFileService.php
Moves an uploaded file to a new location.

File

src/S3fsFileService.php, line 457

Class

S3fsFileService
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\s3fs

Code

protected function putObject($source, $destination) {
  if (mb_strlen($destination) > S3fsServiceInterface::MAX_URI_LENGTH) {
    $this->logger
      ->error("The specified file '%destination' exceeds max URI length limit.", [
      '%destination' => $destination,
    ]);
    return FALSE;
  }
  $config = $this->configFactory
    ->get('s3fs.settings')
    ->get();
  $wrapper = $this->streamWrapperManager
    ->getViaUri($destination);
  $scheme = $this->streamWrapperManager
    ->getScheme($destination);
  $key_path = $this->streamWrapperManager
    ->getTarget($destination);
  if ($scheme === 'public') {
    $target_folder = !empty($config['public_folder']) ? $config['public_folder'] . '/' : 's3fs-public/';
    $key_path = $target_folder . $key_path;
  }
  elseif ($scheme === 'private') {
    $target_folder = !empty($config['private_folder']) ? $config['private_folder'] . '/' : 's3fs-private/';
    $key_path = $target_folder . $key_path;
  }
  if (!empty($config['root_folder'])) {
    $key_path = $config['root_folder'] . '/' . $key_path;
  }
  if (method_exists($this->mimeGuesser, 'guessMimeType')) {
    $contentType = $this->mimeGuesser
      ->guessMimeType($key_path);
  }
  else {
    $contentType = $this->mimeGuesser
      ->guess($key_path);
  }
  $uploadParams = [
    'Bucket' => $config['bucket'],
    'Key' => $key_path,
    'SourceFile' => $source,
    'ContentType' => $contentType,
  ];
  if (!empty($config['encryption'])) {
    $uploadParams['ServerSideEncryption'] = $config['encryption'];
  }

  // Set the Cache-Control header, if the user specified one.
  if (!empty($config['cache_control_header'])) {
    $uploadParams['CacheControl'] = $config['cache_control_header'];
  }
  $uploadAsPrivate = Settings::get('s3fs.upload_as_private');
  if ($scheme !== 'private' && !$uploadAsPrivate) {
    $uploadParams['ACL'] = 'public-read';
  }
  $this->moduleHandler
    ->alter('s3fs_upload_params', $uploadParams);
  $s3 = $this->s3fs
    ->getAmazonS3Client($config);
  try {
    $s3
      ->putObject($uploadParams);
  } catch (\Exception $e) {
    return FALSE;
  }
  $wrapper
    ->writeUriToCache($destination);
  return TRUE;
}