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

时间:2021-10-05 10:41:52

I am working on a project, now i need to rename a key using its prefix in s3 php sdk api. i couldn't find it, if any can help. Thanks

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

}

}

2 个解决方案

#1


0  

You can rename s3 files using below code :

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

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

both names need to contain the full s3 path e.g:

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

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

Basically registerStreamWrapper() enables PHP filesystem commands for s3 files.

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

#2


1  

I did this, you guys answered late. i did it myself but LuFFy answer is also correct.

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

}

}

#1


0  

You can rename s3 files using below code :

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

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

both names need to contain the full s3 path e.g:

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

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

Basically registerStreamWrapper() enables PHP filesystem commands for s3 files.

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

#2


1  

I did this, you guys answered late. i did it myself but LuFFy answer is also correct.

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

}

}