Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bossino's avatar

Renaming a directory in S3

Hello. Is there a way that I can rename a s3 folder through code in Laravel. There are also a subfolder and files in that folder so renaming it would be easier than creating a new one and transfer other files. I tried this code below but it didn't work.

Storage::disk('s3')->rename('oldFol', 'newFol');
0 likes
2 replies
martinbean's avatar

@bossino Not really, as S3 isn’t a “real” file system, so moving an object from /old-folder/foo.txt to /new-folder/foo.txt requires a CopyObject operation. So it’s important to pick a directory structure up front and then stick to it.

PeteBatin's avatar

In S3 you have buckets and objects. Directories in S3 don't exist. What you think are directories are object "prefixes" and are part of your S3 object's name. S3 uses a flat structure.

Prefixes can be presented visually as directories by some S3 clients.

directoryA/fileA.txt has an object prefix of "directoryA/" and it's full object name is "directoryA/fileA.txt"

The following 2 files in the same bucket are at the same level/location:

"file1.txt"
"dir1/file2.txt"

You cannot rename via PHP, you can only copy to a different object name and then delete the original. For multiple files that use prefixes you'd have to loop through them, copy, new name, and delete original.

If you have access to the AWS CLI then you can do

aws s3 mv --recursive s3://bucket/prefix1/ s3://bucket/prefix2/

Please or to participate in this conversation.