我正在处理一个项目,现在我需要在 s3 php sdk API 中使用其前缀重命名密钥。 我找不到它,如果有的话。谢谢
function moveFile($oldPath,$newPath){
$oKey = $this->getKey($oldPath);
$nKey = $this->getKey($newPath);
try{
// Copy an object.
$this->o->copyObject(array(
'Bucket' => $this->bucket,
'ACL' => 'public-read',
'Key' => $nKey,
'CopySource' => "{$this->bucket}/{$oKey}"
));
$this->deleteFile($oldPath);
} catch (S3Exception $e) {
echo $e->getMessage() . "n";
return false;
}
}
您可以使用以下代码重命名s3
文件:
$s3sdk = new Sdk($awsConfig);
$s3 = $s3sdk->createS3();
$s3->registerStreamWrapper();
rename($oldName, $newName);
两个名称都需要包含完整的 S3 路径,例如:
"s3://yourBucketName/path/to/file"
基本上registerStreamWrapper()
为 s3 文件启用 PHP 文件系统命令。
我这样做了,你们回答晚了。 我自己做的,但LuFFy的答案也是正确的。
function renameFolder($oldPath,$newPath){
$oKey = $this->getKey($oldPath);
if(strpos($oKey,'/')==false){$oKey.='/';}
//echo '<br>oKey: '.$oKey.'<br>';
try{
// Copy an object.
/*$this->o->copyObject(array(
'Bucket' => $this->bucket,
'ACL' => 'public-read',
'Key' => $nKey,
'CopySource' => "{$this->bucket}/{$oKey}"
));*/
$result = $this->o->listObjects([
'Bucket' => $this->bucket, // REQUIRED
'Prefix' => $oKey,
]);
foreach($result['Contents'] as $file){
//echo '<br>objectKey: '.$file['Key'].'<br>';
$nKey = str_replace($this->getLastKey($oldPath),$this->getLastKey($newPath),$file['Key']);
//echo '<br>nKey: '.$nKey.'<br>';
$this->o->copyObject(array(
'Bucket' => $this->bucket,
'ACL' => 'public-read',
'Key' => $nKey,
'CopySource' => "{$this->bucket}/".$file['Key'].""
));
}
$this->deleteDir($oldPath);
}catch(S3Exception $e) {
echo $e->getMessage() . "n";
return false;
}
}
我已经设法使用以下步骤重命名了批处理中的现有文件:
- 假设您的配置文件/文件系统.php如下所示:
'disks' => [
's3_test_bucket' => [
'driver' => 's3',
'key' => env('AWS_KEY', 'your_aws_key_here'),
'secret' => env('AWS_SECRET','your_aws_secret_here'),
'region' => env('AWS_REGION', 'your_aws_region_here'),
'version' => 'latest',
'bucket' => 'my-test-bucket',
],
];
- 假设您的 AWS S3 上有 my-test-bucket。
假设您在 my-test-bucket/test-directory 目录中有以下文件。 即
- 测试文件-1.csv
- 测试文件-2.csv
- 测试文件-3.csv
调用以下函数以重命名 S3 存储桶上选定目录上的现有文件。
$directoryPath = 'test-directory';
$storage = new MyStorageRepository();
$storage->renameAnyExistingFilesOnImportDirectory('my-test-bucket', 'test-directory');
输出:文件应该在 my-test-bucket/test-directory 目录上
重命名如下:- 测试文件-1--1548870936.csv
- 测试文件-2--1548870936.csv
- 测试文件-3--1548870936.csv
在你的类中包括下面的库类或方法,你应该很好。
use IlluminateSupportFacadesApp;
use IlluminateSupportFacadesStorage;
class MyStorageRepository
{
public function renameAnyExistingFilesOnImportDirectory($bucket, $directoryPath)
{
$directoryPath = App::environment() . '/' . $directoryPath;
$storage = Storage::disk('s3_test_bucket');
$suffix = '--' . time(); // File suffix to rename.
if ($storage->exists($directoryPath)) {
$this->renameStorageDirectoryFiles($directoryPath, $storage, $suffix);
}
}
private function getNewFilename($filename, $suffix = null)
{
$file = (object) pathinfo($filename);
if (!$suffix) {
$suffix = '--' . time();
}
return $file->dirname . '/' . $file->filename . $suffix . '.' . $file->extension;
}
private function renameStorageDirectoryFiles($directoryPath, $storage = null, $suffix = null, $filesystemDriver = null)
{
if (!$storage) {
$storage = Storage::disk($filesystemDriver);
}
// List all the existing files from the directory
$files = $storage->files($directoryPath);
if (count($files) < 1 ) return false;
foreach($files as $file) {
// Get new filename
$newFilename = Helpers::getNewFilename($file, $suffix);
// Renamed the files
$storage->move($file, $newFilename);
}
}
}