如何使用PHP SDK在s3中通过前缀重命名文件夹
如何使用PHP SDK在s3中通过前缀重命名文件夹
我正在开发一个项目,现在我需要使用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; }
}
admin 更改状态以发布 2023年5月23日
我做了这个,你们回答晚了。我自己也做了,但LuFFy的答案也是正确的。
function renameFolder($oldPath,$newPath){ $oKey = $this->getKey($oldPath); if(strpos($oKey,'/')==false){$oKey.='/';} //echo 'oKey: '.$oKey.''; 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 'objectKey: '.$file['Key'].''; $nKey = str_replace($this->getLastKey($oldPath),$this->getLastKey($newPath),$file['Key']); //echo 'nKey: '.$nKey.''; $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; }
}