【问题标题】:How to rename a folder by prefix in s3 using PHP sdk如何使用 PHP sdk 在 s3 中通过前缀重命名文件夹
【发布时间】:2017-12-02 14:45:26
【问题描述】:

我正在做一个项目,现在我需要在 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;
}

}

【问题讨论】:

标签: php amazon-web-services amazon-s3


【解决方案1】:

我这样做了,你们回答晚了。我自己做的,但 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;
}

}

【讨论】:

    【解决方案2】:

    您可以使用以下代码重命名s3 文件:

    $s3sdk = new Sdk($awsConfig);
    $s3 = $s3sdk->createS3();
    $s3->registerStreamWrapper();
    rename($oldName, $newName);
    

    两个名称都需要包含完整的 s3 路径 例如:

    "s3://yourBucketName/path/to/file"
    

    基本上registerStreamWrapper() 为 s3 文件启用 PHP 文件系统命令。

    【讨论】:

    • 谢谢你的回答也是正确的,但我回答了上面的代码。
    【解决方案3】:

    我已设法使用以下步骤重命名批处理中的现有文件:

    1. 假设您的 config/filesystems.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',
          ],
        ];
    
    1. 假设您的 AWS S3 上有 my-test-bucket
    2. 假设您在 my-test-bucket/test-directory 目录中有以下文件。 即

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

        $directoryPath = 'test-directory';
        $storage = new MyStorageRepository();
        $storage->renameAnyExistingFilesOnImportDirectory('my-test-bucket', 'test-directory');
    
    1. 输出:my-test-bucket/test-directory 目录中的文件应重命名如下:

      • test-files-1--1548870936.csv
      • test-files-2--1548870936.csv
      • test-files-3--1548870936.csv
    2. 在您的类中包含以下库类或方法,您应该会很好。

    use Illuminate\Support\Facades\App;
    use Illuminate\Support\Facades\Storage;
    
    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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 2016-01-17
      • 1970-01-01
      • 2015-03-16
      • 2021-04-02
      • 2011-09-13
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      相关资源
      最近更新 更多