{"id":3795,"date":"2023-06-26T11:40:25","date_gmt":"2023-06-26T11:40:25","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3795"},"modified":"2023-06-26T11:40:25","modified_gmt":"2023-06-26T11:40:25","slug":"uploading-files-to-aws-s3-using-cli","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3795","title":{"rendered":"Uploading Files to AWS S3 Using CLI"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I&#8217;ll assume you have your AWS CLI configured so you are able to login to an AWS account. If you need any help on this see: <a rel=\"noreferrer noopener\" href=\"https:\/\/geekmungus.co.uk\/?p=1167\" target=\"_blank\">https:\/\/geekmungus.co.uk\/?p=1167<\/a>. Here&#8217;s a bit of a cheat sheet on how to use AWS S3 via the CLI.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Listing Buckets<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 ls<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Upload One File<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Upload the file called &#8220;myfile.txt&#8221; into the S3 bucket called &#8220;mybucket1&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 cp c:\\localcopy\\myfile.txt s3:\/\/mybucket1\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">List Contents of a Bucket<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 ls s3:\/\/mybucket1\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Upload Multiple Files<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Upload the contents of the &#8220;c:\\localcopy&#8221; directory into the S3 bucket called &#8220;mybucket1&#8221;, the &#8220;recursive&#8221; argument is added which ensures that all the files and sub-directories (and the files within) are also uploaded.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 cp c:\\localcopy s3:\/\/mybucket1\/ --recursive<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Upload Multiple Files (Include and Exclude)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, uploading all files to a bucket that have the &#8220;.txt&#8221; file extension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 cp c:\\localcopy s3:\/\/mybucket1\/ --recursive --exclude * --include *.txt<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Download Files (and Multiple Files)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Downloading is just a reverse of the upload essentially, you can specify the &#8220;&#8211;recursive&#8221; option if you want to download all files (and sub-directories including their contents).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 cp s3:\/\/mybucket1\/ c:\\localcopy\naws s3 cp s3:\/\/mybucket1\/ c:\\localcopy --recursive<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Copying Files (Objects) Between S3 Locations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To copy a file (object) between S3 locations, you can perform this as follows, so in this example copying a file called &#8220;myfile.txt&#8221; from S3 &#8220;mybucket1&#8221; to S3 &#8220;mybucket2&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 cp s3:\/\/mybucket1\/myfile.txt s3:\/\/mybucket2\/<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sync Files (Objects)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say you want to sync changed files, you can use the &#8220;sync&#8221; command, this has a number of ways to use it but a simple upload sync would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 sync c:\\localcopy s3:\/\/mybucket1\/ --recursive<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">While a sync but which also deletes files that have been deleted from your source local directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws s3 sync c:\\localcopy s3:\/\/mybucket1\/ --recursive --delete<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A bit of a whistle stop tour, but this gives you an overview of some simple commands you can use, which can be helpful for scripting, or just managing files in your S3 buckets.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll assume you have your AWS CLI configured so you are able to login to an AWS account. If you need any help on this see: https:\/\/geekmungus.co.uk\/?p=1167. Here&#8217;s a bit of a cheat sheet on how to use AWS S3 via the CLI. Listing Buckets Upload One File Upload the file called &#8220;myfile.txt&#8221; into the &#8230; <a title=\"Uploading Files to AWS S3 Using CLI\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3795\" aria-label=\"Read more about Uploading Files to AWS S3 Using CLI\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-3795","post","type-post","status-publish","format-standard","hentry","category-aws"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3795","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3795"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3795\/revisions"}],"predecessor-version":[{"id":3797,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3795\/revisions\/3797"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}