protected function S3fsFileService::copyObject in S3 File System 8.3
Same name and namespace in other branches
- 4.0.x src/S3fsFileService.php \Drupal\s3fs\S3fsFileService::copyObject()
Copy a file that is already in the the bucket.
Parameters
string $source: Source file to be copied.
string $destination: Destination path in bucket.
Return value
bool True if successful, else FALSE.
2 calls to S3fsFileService::copyObject()
- 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.
File
- src/
S3fsFileService.php, line 540
Class
- S3fsFileService
- Provides helpers to operate on files and stream wrappers.
Namespace
Drupal\s3fsCode
protected function copyObject($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);
$src_key_path = $this->streamWrapperManager
->getTarget($source);
if ($scheme === 'public') {
$target_folder = !empty($config['public_folder']) ? $config['public_folder'] . '/' : 's3fs-public/';
$key_path = $target_folder . $key_path;
$src_key_path = $target_folder . $src_key_path;
}
elseif ($scheme === 'private') {
$target_folder = !empty($config['private_folder']) ? $config['private_folder'] . '/' : 's3fs-private/';
$key_path = $target_folder . $key_path;
$src_key_path = $target_folder . $src_key_path;
}
if (!empty($config['root_folder'])) {
$key_path = $config['root_folder'] . '/' . $key_path;
$src_key_path = $config['root_folder'] . '/' . $src_key_path;
}
if (method_exists($this->mimeGuesser, 'guessMimeType')) {
$contentType = $this->mimeGuesser
->guessMimeType($key_path);
}
else {
$contentType = $this->mimeGuesser
->guess($key_path);
}
$copyParams = [
'Bucket' => $config['bucket'],
'Key' => $key_path,
'CopySource' => $config['bucket'] . '/' . $src_key_path,
'ContentType' => $contentType,
'MetadataDirective' => 'REPLACE',
];
if (!empty($config['encryption'])) {
$copyParams['ServerSideEncryption'] = $config['encryption'];
}
// Set the Cache-Control header, if the user specified one.
if (!empty($config['cache_control_header'])) {
$copyParams['CacheControl'] = $config['cache_control_header'];
}
$uploadAsPrivate = Settings::get('s3fs.upload_as_private');
if ($scheme !== 'private' && !$uploadAsPrivate) {
$copyParams['ACL'] = 'public-read';
}
$this->moduleHandler
->alter('s3fs_copy_params_alter', $copyParams);
$s3 = $this->s3fs
->getAmazonS3Client($config);
try {
$s3
->copyObject($copyParams);
} catch (\Exception $e) {
return FALSE;
}
$wrapper
->writeUriToCache($destination);
return TRUE;
}