To upload a folder to Amazon S3 using s3cmd, you can use the put command with the --recursive option. This command will upload all files within the specified directory to the specified bucket. Here are the steps:
-
Install
s3cmd: If you haven't installeds3cmdyet, you can do so by running the following command on your Linux system:sudo apt-get install s3cmd -
Configure
s3cmd: You need to configures3cmdwith your AWS access key and secret key. You can do this by running:s3cmd --configure -
Upload the folder: Use the
putcommand with the--recursiveoption to upload the folder and all its contents to the specified bucket:s3cmd put --recursive /path/to/local/folder s3://bucket-name/path/to/folder//path/to/local/folderis the directory you want to upload.s3://bucket-name/path/to/folder/is the path in the bucket where you want to upload the folder.
-
Optional flags: You can add additional flags to customize the upload process. For example:
--acl-publicsets the permissions to public.--add-header=Cache-Control:max-age=86400sets the cache control header to 1 day.
s3cmd put --recursive --acl-public --add-header=Cache-Control:max-age=86400 /path/to/local/folder s3://bucket-name/path/to/folder/
Example Usage
Here is an example of uploading a folder named dir1 from the local directory /home/user to the bucket my.bucket in the path /assets/dir1/:
s3cmd put --recursive --acl-public --add-header=Cache-Control:max-age=86400 /home/user/dir1 s3://my.bucket/assets/dir1/
Additional Tips
- Trailing Slash: Ensure that the bucket path includes a trailing slash (
/) to maintain the directory structure. - Recursive Upload: The
--recursiveoption ensures that all files within the specified directory are uploaded. - File Naming: You can rename files during upload by specifying the new name at the end of the path.
Troubleshooting
- Existing Directory Structure: If the directory structure already exists in the bucket,
s3cmdwill not overwrite the existing files. You can use thesynccommand to update the files if needed. - File Size and MD5 Check:
s3cmdperforms a size and MD5 checksum comparison to ensure that the files are identical on both the local and remote sides.