How to rename files and folders in S3 bucket
You can use aws s3 mv command to rename or move files (objects) or folders (keys) in an S3 bucket. Let me demonstrate with some examples.
Rename a file (object) in S3 bucket using AWS CLI
1 2 |
## Rename a file in S3 bucket aws s3 mv s3:// |
Rename a folder (key) in S3 bucket using AWS CLI
1 2 |
## Rename a folder in S3 bucket aws s3 mv s3:// |
Move a file (object) from one location another in an S3 bucket:
1 2 |
## Move a file in S3 bucket aws s3 mv s3:// |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
## Create an S3 bucket aws s3api create-bucket \ --bucket cloudaffaire \ --region ap-south-1 \ --create-bucket-configuration LocationConstraint=ap-south-1 ## Create a file and folder in S3 bucket mkdir -p mydir/myfolder && echo "hello world" > mydir/myfolder/myfile.txt && aws s3 cp mydir s3://cloudaffaire/ \ --recursive ## List all your files and folders aws s3 ls s3://cloudaffaire/ \ --recursive ## Rename a file in S3 bucket aws s3 mv s3://cloudaffaire/myfolder/myfile.txt s3://cloudaffaire/myfolder/mynewfile.txt ## List all your files and folders aws s3 ls s3://cloudaffaire/ \ --recursive ## Rename a folder in S3 bucket aws s3 mv s3://cloudaffaire/myfolder/mynewfile.txt s3://cloudaffaire/mynewfolder/mynewfile.txt ## List all your files and folders aws s3 ls s3://cloudaffaire/ \ --recursive ## Move files and folders from one location to another in S3 bucket aws s3 mv s3://cloudaffaire/mynewfolder/mynewfile.txt s3://cloudaffaire/newlocation/mynewfolder/mynewfile.txt ## List all your files and folders aws s3 ls s3://cloudaffaire/ \ --recursive ## Delete the S3 bucket along with content aws s3 rb s3://cloudaffaire --force |
You can use below links to install and configure AWS CLI
https://cloudaffaire.com/how-to-install-aws-cli/
https://cloudaffaire.com/how-to-configure-aws-cli/
Hope this solves your issue, keep learning and keep sharing.