Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHPSDK在s3中通过前缀重命名文件夹_Php_Amazon Web Services_Amazon S3 - Fatal编程技术网

如何使用PHPSDK在s3中通过前缀重命名文件夹

如何使用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

我正在从事一个项目,现在我需要使用S3PHPSDKAPI中的前缀重命名一个键。如果有人能帮忙的话,我找不到。谢谢

    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; }

}

我已通过以下步骤成功重命名批处理中的现有文件:

  • 假设您的config/filesystems.php如下所示:
  • “磁盘”=>[
    “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”,
    ],
    ];
    
  • 比如说,你的AWS S3上有我的测试桶
  • 假设在my test bucket/test目录中有以下文件。 i、 e

    • test-files-1.csv
    • test-files-2.csv
    • test-files-3.csv
  • 调用下面的函数重命名S3 Bucket上选定目录中的现有文件

  • $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);
    }
    }
    }
    
    该代码是我根据前缀移动文件夹/密钥的功能。但我想知道是否有任何功能来重命名文件夹?像这样吗?可能重复的谢谢你的回答也是正确的,但我用上面的代码回答了。