如何使用PHPSDK在s3中通过前缀重命名文件夹
我正在从事一个项目,现在我需要使用S3PHPSDKAPI中的前缀重命名一个键。如果有人能帮忙的话,我找不到。谢谢如何使用PHPSDK在s3中通过前缀重命名文件夹,php,amazon-web-services,amazon-s3,Php,Amazon Web Services,Amazon S3,我正在从事一个项目,现在我需要使用S3PHPSDKAPI中的前缀重命名一个键。如果有人能帮忙的话,我找不到。谢谢 function moveFile($oldPath,$newPath){ $oKey = $this->getKey($oldPath); $nKey = $this->getKey($newPath); try{ // Copy an object. $this->o->copyObject(array( 'Buc
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"
基本上,
RegisterStreamRapper()
为s3文件启用PHP文件系统命令。我这么做了,你们回答晚了。我自己做的,但鲁菲的回答也是正确的
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;
}
函数重命名文件夹($oldPath,$newPath){
$oKey=$this->getKey($oldPath);
if(strpos($oKey,“/”)==false){$oKey.='/';}
//回音“
好的:”.$oKey.
”;
试一试{
//复制一个对象。
/*$this->o->copyObject(数组)(
“Bucket”=>this->Bucket,
“ACL”=>“公共读取”,
“密钥”=>nKey美元,
'CopySource'=>“{$this->bucket}/{$oKey}”
));*/
$result=$this->o->listObjects([
“Bucket”=>$this->Bucket,//必需
“前缀”=>$OK,
]);
foreach($result['Contents']作为$file){
//回显'
对象键:'.$file['Key'].
';
$nKey=str_replace($this->getLastKey($oldPath),$this->getLastKey($newPath),$file['Key']);
//回音“
nKey:”.$nKey.
”;
$this->o->copyObject(数组)(
“Bucket”=>this->Bucket,
“ACL”=>“公共读取”,
“密钥”=>nKey美元,
“CopySource'=>”{$this->bucket}/“$file['Key']”
));
}
$this->deleteDir($oldPath);
}捕获(S3$e){
echo$e->getMessage()。“\n”;
返回false;
}
}我已通过以下步骤成功重命名批处理中的现有文件:
“磁盘”=>[
“s3_测试_桶”=>[
“驱动程序”=>“s3”,
'key'=>env('AWS_key','your_AWS_key_here'),
'secret'=>env('AWS_secret','your_AWS_secret_here'),
'region'=>env('AWS\u region','your\u AWS\u region\u here'),
'版本'=>'最新',
“bucket”=>“我的测试bucket”,
],
];
- test-files-1.csv
- test-files-2.csv
- test-files-3.csv
$directoryPath='testdirectory';
$storage=new MyStorageRepository();
$storage->rename现有文件导入目录('my-test-bucket','test directory');
- 测试文件-1--1548870936.csv
- 测试文件-2--1548870936.csv
- 测试文件-3--1548870936.csv
使用light\Support\Facades\App;
使用照明\支持\立面\存储;
类mystoragepository
{
公共函数重命名现有文件导入目录($bucket,$directoryPath)
{
$directoryPath=App::environment().“/”。$directoryPath;
$storage=storage::disk('s3_test_bucket');
$suffix='--'.time();//要重命名的文件后缀。
如果($storage->exists($directoryPath)){
$this->renameStorageDirectoryFiles($directoryPath,$storage,$suffix);
}
}
私有函数getNewFilename($filename,$suffix=null)
{
$file=(对象)路径信息($filename);
如果(!$后缀){
$suffix='--'.time();
}
返回$file->dirname.'/'.$file->filename.$suffix.'..$file->扩展名;
}
私有函数renameStorageDirectoryFiles($directoryPath,$storage=null,$suffix=null,$filesystemDriver=null)
{
如果(!$storage){
$storage=storage::disk($filesystemDriver);
}
//列出目录中的所有现有文件
$files=$storage->files($directoryPath);
如果(计数($files)<1)返回false;
foreach($files作为$file){
//获取新文件名
$newFilename=Helpers::getNewFilename($file,$suffix);
//重命名文件
$storage->move($file,$newFilename);
}
}
}
该代码是我根据前缀移动文件夹/密钥的功能。但我想知道是否有任何功能来重命名文件夹?像这样吗?可能重复的谢谢你的回答也是正确的,但我用上面的代码回答了。