How to Uploade Files to Aws S3

Introduction

Amazon Spider web Service, aka AWS, is a leading cloud infrastructure provider for storing your servers, applications, databases, networking, domain controllers, and active directories in a widespread cloud architecture. AWS provides a Simple Southwardtorage Service (S3) for storing your objects or data with (119's) of data durability. AWS S3 is compliant with PCI-DSS, HIPAA/HITECH, FedRAMP, EU Data Protection Directive, and FISMA that helps satisfy regulatory requirements.

When you log in to the AWS portal, navigate to the S3 bucket, cull your required bucket, and download or upload the files. Doing it manually on the portal is quite a fourth dimension-consuming task. Instead, you lot tin can use the AWS Command Line Interface (CLI) that works best for bulk file operations with easy-to-utilise scripts. Y'all can schedule the execution of these scripts for an unattended object download/upload.

Configure AWS CLI

Download the AWS CLI and install AWS Command Line Interface V2 on Windows, macOS, or Linux operating systems.

AWS CLI Configuration

Y'all can follow the installation magician for a quick setup.

Create an IAM user

To access the AWS S3 bucket using the command line interface, we need to set upward an IAM user. In the AWS portal, navigate to Identity and Access Management (IAM) and click Add User .

setting up an IAM user

In the Add User page, enter the username and access type as Programmatic access.

Programmatic access

Side by side, we provide permissions to the IAM user using existing policies. For this article, we have chosen [AmazonS3FullAccess] from the AWS managed policies.

provide permissions to the IAM user using existing policies

Review your IAM user configuration and click Create user .

Review IAM user configuration and click Create user

In one case the AWS IAM user is created, information technology gives the Access Key ID and Secret access key to connect using the AWS CLI.

Note : You should re-create and save these credentials. AWS does not let y'all to remember them at a later stage.

IAM User created successfully.

Configure AWS Profile On Your Computer

To work with AWS CLI on Amazon web service resources, launch the PowerShell and run the post-obit command.

          >aws configure        

Information technology requires the following user inputs:

  • IAM user Access Cardinal ID
  • AWS Surreptitious Access key
  • Default AWS region-proper noun
  • Default output format
AWS Profile Configuration

Create S3 Bucket Using AWS CLI

To store the files or objects, we need an S3 bucket.  We tin create it using both the AWS portal and AWS CLI.

The following CLI command creates a saucepan named [mys3bucket-testupload1] in the the states-east-1 region. The query returns the bucket name in the output, equally shown below.

          >aws s3api create-bucket --bucket mys3bucket-testupload1 --region u.s.a.-east-1        
Create S3 Bucket Using AWS CLI

Yous can verify the newly-created s3 bucket using the AWS console. As shown below, the [mys3bucket-testupload1] is uploaded in the US East (North. Virginia).

verify the newly-created s3 bucket using the AWS console

To list the existing S3 bucket using AWS CLI, run the command – aws s3 ls

list the existing S3 bucket using AWS CLI

Uploading Objects in the S3 Saucepan Using AWS CLI

We tin can upload a unmarried file or multiple files together in the AWS S3 saucepan using the AWS CLI command. Suppose we have a unmarried file to upload. The file is stored locally in the C:\S3Files with the name script1.txt.

To upload the single file, use the following CLI script.

          >aws s3 cp C:\S3Files\Script1.txt s3://mys3bucket-testupload1/        

It uploads the file and returns the source-destination file paths in the output:

Uploading Objects in the S3 Bucket Using AWS CLI

Notation: The fourth dimension to upload on the S3 bucket depends on the file size and the network bandwidth. For the demo purpose, I used a small file of a few KBs.

You can refresh the s3 bucket [mys3bucket-testupload1] and view the file stored in it.

refresh the s3 bucket

Similarly, nosotros can use the same CLI script with a slight modification. It uploads all files from the source to the destination S3 saucepan. Here, we utilise the parameter –recursive for uploading multiple files together:

          >aws s3 cp c:\s3files s3://mys3bucket-testupload1/ --recursive        

Every bit shown below, it uploads all files stored inside the local directory c:\S3Files to the S3 bucket. You go the progress of each upload in the console.

 progress of each upload

Nosotros can see all uploaded files using recursive parameters in the S3 bucket in the post-obit figure:

all uploaded files

If you do not want to go to the AWS portal to verify the uploaded list, run the CLI script, return all files, and upload timestamps.

          >aws s3 ls s3://mys3bucket-testupload1        
run the CLI script, return all files, and upload timestamps.

Suppose we want to upload simply files with a specific extension into the separate folder of AWS S3. You lot can exercise the object filtering using the CLI script equally well. For this purpose, the script uses include and exclude keywords.

For example, the query below checks files in the source directory (c:\s3bucket), filters files with .sql extension, and uploads them into SQL/ folder of the S3 bucket. Here, we specified the extension using the include keyword:

          >aws s3 cp C:\S3Files s3://mys3bucket-testupload1/  --recursive   --exclude * --include *.sql        

In the script output, you tin can verify that files with the .sql extensions just were uploaded.

verify that files with the .sql extensions only were uploaded
verify that files with the .sql extensions only were uploaded

Similarly, the below script uploads files with the .csv extension into the S3 bucket.

          >aws s3 cp C:\S3Files s3://mys3bucket-testupload1/  --recursive   --exclude * --include *.csv        
 script uploads files with the .csv extension into the S3 bucket

Upload New or Modified Files from Source Folder to S3 Saucepan

Suppose you use an S3 saucepan to motion your database transaction log backups.

For this purpose, we use the sync keyword. It recursively copies new, modified files from the source directory to the destination s3 bucket.

          >aws s3 sync C:\S3Files s3://mys3bucket-testupload1/  --recursive   --exclude * --include *.sql        

Every bit shown below, it uploaded a file that was absent in the s3 bucket. Similarly, if yous modify whatever existing file in the source folder, the CLI script will choice information technology and upload it to the S3 saucepan.

Upload New or Modified Files from Source Folder to S3 Bucket
Upload New or Modified Files from Source Folder to S3 Bucket

Summary

The AWS CLI script can make your piece of work easier for storing files in the S3 saucepan. Y'all can utilize it to upload or synchronize files between local folders and the S3 bucket. It is a quick way to deploy and work with objects in the AWS cloud.

Tags: AWS, aws cli, aws s3, cloud platform Final modified: September 16, 2021

kleimanevehist.blogspot.com

Source: https://codingsight.com/upload-files-to-aws-s3-with-the-aws-cli/

0 Response to "How to Uploade Files to Aws S3"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel