Price to Upload File to S3 Using Cli
How to Upload to Amazon S3 via AWS CLI and NPM scripts
How to Upload to Amazon S3 via AWS CLI and NPM scripts: How to upload your site to S3 by running one simple control in your project terminal. This guide assumes that you accept some familiarity navigating the AWS Console and each Amazon Web Service. We will be using Amazon IAM (Identity and Access Direction) besides. This method will work for any NPM project that uses a package.json file to control project dependencies and. NPM. scripts. This script is very uncomplicated and will salvage yous a lot of time and salve you time.
@ ianholden
Ian HoldenI am a software engineer who enjoys learning and writing about frontend & backend evolution.
Static websites are a vivid way to create performant sites. My website is built using Gatsby and hosted on Amazon S3. I have created a simple script to help yous quickly upload your site to S3 by running ane simple control in your project terminal.
Prerequisites
Before we start, you must ensure that y'all have completed the post-obit:
- Install AWS CLI (Amazon Web Services Command Line Interface) on your machine.
- Create a bucket in Amazon S3 (this is where we volition exist uploading our projection files to).
This guide assumes that yous have some familiarity navigating the AWS Console and each Amazon Web Service.
Configuring our Amazon services
Amazon S3 is the chief service that nosotros will use for this tutorial. I like to create new users when I work with dissimilar projects using Amazon's CLI so nosotros will be using Amazon IAM (Identity and Access Direction) too.
Create a user for our project
Sign in to the AWS Console and find the IAM service under the services dropdown.
In the IAM service, create a new user, give them a name and grant them Programmatic access.
When setting permissions for this user, we want to 'Attach existing policies straight' and Create policy.
Using the JSON editor, we can paste the following policy. Y'all must replace 'ianholden.co.uk' with the proper noun of the S3 saucepan that you lot desire to copy your project files to:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Issue": "Let", "Activeness": [ "s3:PutObject", "s3:ListBucket", "s3:DeleteObject" ], "Resources": [ "arn:aws:s3:::ianholden.co.great britain/", "arn:aws:s3:::ianholden.co.u.k." ] } ] }
Salvage this policy confronting your new user and you should be presented with an Admission Cardinal ID and a Surreptitious Admission Primal. Yous must keep these credentials rubber equally they will be needed later.
Add together our new user every bit a named contour
With the user that we have only created, we need to add this user to our system'south AWS CLI configuration. Follow these instructions for creating named profiles. In your local AWS configuration file, add a new named profile called 'testUser'.
Doing this will require u.s. to specify the user that we want to use when nosotros apply the AWS CLI. As this volition exist scripted subsequently, we tin include this in the script that we write.
Create a script
This part of the guide will occur in our project's directory. This method will piece of work for whatsoever NPM project that uses a package.json file to control project dependencies and NPM scripts. Examples of these are React, Athwart and Vue projects, to name a few.
Create script file
Open up your web project in a code editor and create a file at the root of this project called upload-to-s3.sh. Enter the following code, replacing 'ianholden.co.britain' with the name of your S3 bucket. Replace 'project-files' with the relative directory that you lot desire to upload to your S3 bucket:
#!/bin/fustigate # Empty the S3 bucket aws s3 rm s3://ianholden.co.uk --recursive --contour testUser # Upload our project files to the S3 bucket aws s3 cp project-files/ s3://ianholden.co.britain --recursive --profile testUser
Update package.json file
In your project'southward package.json file, add together the following aspect to your 'scripts' object:
scripts: { ..., "upload-to-s3": "./upload-to-s3.sh" }
This improver now allows us to run the post-obit command in our terminal which will execute the new script that we have merely created:
That'south information technology!
If everything has worked, yous should at present be able to log into your AWS Panel again, view your S3 saucepan and find that your project files accept been uploaded. This helpful script is very elementary and it will save you lot a lot of fourth dimension doing these tasks manually.
If you lot have whatsoever issues, it could be that you lot take not setup your AWS user correctly. Your concluding will commonly requite y'all an mistake message if something hasn't worked correctly.
In many cases, you might but use 1 user for your AWS business relationship. If that is the case, you lot do not need to create a named profile, you can merely fix upwards the AWS CLI using default credentials. I would recommend using multiple users across each project because information technology gives you the power to finely melody that user's admission to your services which makes it more than secure.
I promise you lot accept found this helpful.
Previously published at https://ianholden.co.uk/web log/upload-to-s3-using-aws-cli-and-npm-scripts/
Tags
Related Stories
tudawaliwidee1967.blogspot.com
Source: https://hackernoon.com/how-to-upload-to-amazon-s3-via-aws-cli-and-npm-scripts-ct143zxb