How to Upload Local Files to AWS S3 and DynamoDB
How Do You Upload Files to AWS?
I've taken many online Amazon Web Services (AWS) certification courses over the last few years with instructors who invariably use either Linux or Mac devices for hands-on labs. That can present some problems for Windows users because accessing and using the AWS Command Line Interface (CLI) varies by Operating System (OS). One of the things many instructors don't explain is how to upload local files to AWS services like S3 and DynamoDB from a Windows computer, even though this is a required step in some hands-on labs.
Online courses covering AWS Associate certifications typically teach how to access the AWS CLI using Puttygen and Putty, the Connect option in EC2, or CloudShell. However, they usually don't explain how to upload local files to AWS services like S3 and DynamoDB. In this guide, I'll explain a few different ways to do this. They will require the installation of the AWS CLI.
How to Access the AWS CLI
Windows
Installing the AWS CLI is taught in beginner courses, so I won't explain that here. Once you have installed it, you can access the CLI directly from your Windows machine in a few different ways. Go to the Search box in the bottom left-hand corner of Windows labeled Type here to search, and type:
- cmd to open the Command Prompt, or,
- powershell to open Windows Powershell
If you choose PowerShell, you can use many Linux commands. If you choose the Command Prompt, you may find it helpful to learn Windows equivalents of common Linux commands.
Windows Subsystem for Linux (WSL):
Another option is to use the Windows Subsystem for Linux (WSL) and choose a distribution. I use the Ubuntu distribution. Go to the Microsoft Store, and search for Windows Subsystem for Linux to see the available options. When it's installed, go to the search box in the bottom left-hand corner of Windows and type the required command to run it. For example, if you install Ubuntu, type ubuntu into the search.
You must install the AWS CLI on this Linux distribution to access AWS.
Your C: drive should be mounted at /mnt/c, so your Windows user account files should be located at /mnt/c/Users/<windows-user-name>.
Windows and Linux Equivalent Commands
If you want to use the Command Prompt, you will have to use Linux commands for AWS commands but Windows equivalents for commands on your local system. For example, if you want a list of objects in an S3 bucket called mybucket, you will use the Linux ls command:
aws s3 ls s3://mybucket
If you want to list all the items in your local Windows Documents folder, you have to use the Windows command dir:
dir Documents
Of course, you could just go to the Documents folder to see all the objects it contains. But that's more time-consuming than accessing that information within the command line itself. Both Windows and Linux commands can be used with Windows PowerShell.
Windows Equivalents for Common Linux Commands
Linux | Windows | Action |
---|---|---|
ls | dir | list items in a directory |
cd | cd | change directory |
mkdir | mkdir | create a directory |
mv | move | move a file |
rm | del | delete a file |
cp | cp | copy a file |
mv | rename or ren | rename a file |
clear | cls | clear the screen |
cat | type | display the contents of a file |
cd ~ | cd \ | go to the root/home directory |
To create files in Linux, use the touch or echo commands.
touch myfile or
echo "This is some text" > file1.txt
To create files in Windows, use the echo command.
echo > myfile.txt
Text can be entered into a file during file creation by placing it before the > symbol.
echo This is some text > file1.txt
Create a Dedicated Folder for Lab Uploads
The easiest way to upload lab files is to have a dedicated folder on your hard drive for that purpose. In my Documents folder, I have a subfolder called Learn. I download the lab files I need to upload to AWS into this Learn folder.
Before uploading files, you need to navigate to that folder in whatever command-line tool you are using. Type the following commands:
Command Prompt and PowerShell
cd \
This will take you to the c:> prompt. This is the command I type to navigate to the Learn folder.
cd Users\<windows-user-name>\Documents\Learn
Figure out the path to whatever folder you are using, then use the cd (change directory) command to navigate to it.
Windows Subsystem for Linux (WSL)
When using the Windows Subsystem for Linux, this is my path to the Learn folder.
cd /mnt/c/Users/<windows-user-name>/Documents/Learn
Once you have navigated to the folder that stores your files, you can use the copy command (cp) to upload files from that folder to AWS S3.
Upload Files to AWS S3 and DynamoDB
Once you have installed the AWS CLI, you can access AWS using your Access Key ID and Secret Access Key. To log in, go to either the Command Prompt, Windows PowerShell, or the Windows Subsystem for Linux, and type this command:
aws configure
When prompted, enter your Access Key ID and Secret Access Key. If you are working within a particular region, you will be prompted to enter it. For example, enter us-east-1 if you will be working in the Northern Virginia region, ap-southeast-1 for the Singapore region, eu-west-1 for the Ireland region, and so on.
These are some useful commands for working with S3 buckets and uploading objects.
S3 Commands
To see a list of S3 buckets in your account:
aws s3 ls
To list all the objects in a particular bucket:
aws s3 ls s3://<my-bucket-name>
To upload an object called mypic.png to a bucket:
aws s3 cp mypic.png s3://<my-bucket-name>
To upload a folder called mysynctest, use this command:
aws s3 mysynctest s3://<my-bucket-name>/mysynctest --recursive
(--recursive copies the directory and everything in it)
To see the files in the uploaded folder:
aws ls s3://<my-bucket-name>/mysynctest
To sync a folder called mysynctest on your hard drive to a subdirectory in s3 called mysynctest, use this command:
aws s3 sync mysynctest s3://<my-bucket-name>/mysynctest/
DynamoDB Upload Command
This command can be used to upload a JSON file called myfile.json to DynamoDB:
aws dynamodb batch-write-item --request-items file://myfile.json
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2021 LT Wright