How to Download a File from S3 in AWS: Simple Steps
To download a file from AWS S3, use the
aws s3 cp command in AWS CLI or the getObject method in AWS SDK. Specify the S3 bucket and file key to retrieve the file to your local system.Syntax
Here is the basic syntax to download a file from S3 using AWS CLI and AWS SDK for JavaScript.
- AWS CLI: Use
aws s3 cp s3://bucket-name/file-key local-file-pathto copy the file from S3 to your local machine. - AWS SDK (JavaScript): Use
s3.getObject(params).promise()whereparamsincludes the bucket name and key of the file.
bash and javascript
aws s3 cp s3://my-bucket/my-file.txt ./my-file.txt // JavaScript AWS SDK v3 example import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; const client = new S3Client({ region: "us-east-1" }); const params = { Bucket: "my-bucket", Key: "my-file.txt" }; async function downloadFile() { const data = await client.send(new GetObjectCommand(params)); // data.Body is a stream of the file content }
Example
This example shows how to download a file named example.txt from an S3 bucket called my-sample-bucket to your local folder using AWS CLI and Node.js AWS SDK v3.
bash and javascript
aws s3 cp s3://my-sample-bucket/example.txt ./example.txt // Node.js AWS SDK v3 example import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3"; import { createWriteStream } from "fs"; const client = new S3Client({ region: "us-east-1" }); const params = { Bucket: "my-sample-bucket", Key: "example.txt" }; async function downloadFile() { const data = await client.send(new GetObjectCommand(params)); const stream = data.Body; const fileStream = createWriteStream("./example.txt"); stream.pipe(fileStream); fileStream.on("close", () => console.log("Download complete.")); } downloadFile();
Output
download: s3://my-sample-bucket/example.txt to ./example.txt
Download complete.
Common Pitfalls
Common mistakes when downloading files from S3 include:
- Using incorrect bucket name or file key, causing "NoSuchKey" errors.
- Not having proper AWS permissions to access the bucket or object.
- For SDK usage, not handling the stream correctly, which can cause incomplete downloads.
- For CLI, forgetting to configure AWS credentials or region.
bash
// Wrong: Missing bucket or wrong key aws s3 cp s3://wrong-bucket/wrong-file.txt ./file.txt // Right: Correct bucket and key aws s3 cp s3://my-bucket/my-file.txt ./file.txt
Quick Reference
Remember these tips when downloading from S3:
- Always specify the full S3 URI:
s3://bucket-name/file-key. - Ensure AWS credentials and region are configured.
- Use streams properly in SDK to save files.
- Check permissions if access is denied.
Key Takeaways
Use
aws s3 cp command or AWS SDK's getObject to download files from S3.Always specify the correct bucket name and file key to avoid errors.
Ensure your AWS credentials and permissions allow access to the S3 object.
Handle file streams properly when using SDKs to save files locally.
Check AWS region configuration to match your S3 bucket location.