How to upload files to Amazon S3 using an access key id and secret access key

Question:

I have to upload some static HTML and CSS files to Amazon S3, and have been given an Access Key ID as well as a Secret Access Key.

I’ve signed up for AWS, how to I upload stuff?

Answer:

If you are comfortable using the command line, the most versatile (and enabling) approach for interacting with (almost) all things AWS is to use the excellent AWS Command Line Interface (AWS CLI) – it meanwhile covers most services’ APIs, and it also features higher level S3 commands that ease dealing with your use case considerably, see the AWS CLI reference for S3 (the lower level commands are in s3api) – specifically you are likely interested in:

  • cpCopies a local file or S3 object to another location locally or in S3
  • syncSyncs directories and S3 prefixes.

I use the latter to deploy static websites hosted on S3 by simply syncing what’s changed, convenient and fast. Your use case is covered by the first of several Examples (more fine grained usage with --exclude, --include and prefix handling etc. is available):

The following sync command syncs objects under a specified prefix and
bucket to files in a local directory by uploading the local files to
s3. […]

While the AWS CLI supports the regular AWS Credentials handling via environment variables, you can also configure Multiple Configuration Profiles for yourself and other AWS accounts and switch as needed:

The AWS CLI supports switching between multiple profiles stored within the configuration file. […] Each profile uses different credentials—perhaps from two different IAM users—and also specifies a different region. The first profile, default, specifies the region us-east-1. The second profile, test-user, specifies us-west-2. Note that, for profiles other than default, you must prefix the profile name with the string, profile.

Leave a Reply