LinuxCommandLibrary

aws-s3-cp

Copy files to/from Amazon S3

TLDR

Copy a file from local to a specific bucket

$ aws s3 cp [path/to/file] s3://[bucket_name]/[path/to/remote_file]
copy

Copy a specific S3 object into another bucket
$ aws s3 cp s3://[bucket_name1]/[path/to/file] s3://[bucket_name2]/[path/to/target]
copy

Copy a specific S3 object into another bucket keeping the original name
$ aws s3 cp s3://[bucket_name1]/[path/to/file] s3://[bucket_name2]
copy

Copy S3 objects to a local directory recursively
$ aws s3 cp s3://[bucket_name] . --recursive
copy

Display help
$ aws s3 cp help
copy

SYNOPSIS

aws s3 cp <SOURCE> <TARGET> [options]

Examples:
aws s3 cp my-local-file.txt s3://my-bucket/
aws s3 cp s3://my-bucket/my-remote-file.txt .
aws s3 cp s3://source-bucket/data/ s3://target-bucket/backup/ --recursive

PARAMETERS

--recursive
    Recursively copy contents of directories. Required when copying a directory to or from S3.

--exclude "pattern"
    Exclude files or objects that match the specified pattern from the copy operation.

--include "pattern"
    Don't exclude files or objects that match the specified pattern. Used in conjunction with --exclude to refine filtering.

--sse
    Specifies that the object should be encrypted at rest in S3 using Amazon S3-managed encryption keys (SSE-S3).

--sse-kms-key-id
    Specifies the KMS key ID to use for server-side encryption with AWS KMS (SSE-KMS).

--storage-class
    Specifies the storage class for the uploaded objects (e.g., STANDARD, STANDARD_IA, ONEZONE_IA, GLACIER, DEEP_ARCHIVE).

--acl
    Sets the access control list (ACL) for the copied objects (e.g., private, public-read, bucket-owner-full-control).

--metadata
    Sets user-defined metadata for the object being copied.

--dryrun
    Displays the operations that would be performed without actually executing them.

--quiet
    Suppresses all output except for errors, making the command less verbose.

DESCRIPTION

The aws s3 cp command is a high-level command within the AWS Command Line Interface (AWS CLI) designed for copying objects. It facilitates the transfer of local files or directories to Amazon S3 buckets, S3 objects to local files or directories, and S3 objects between different S3 buckets. This command is highly versatile, supporting recursive copying for entire directories, filtering files based on include/exclude patterns, and various S3-specific features such as server-side encryption, setting storage classes, and managing access control lists (ACLs). For large files, it automatically handles multipart uploads, ensuring efficient and robust data transfer operations, along with automatic retries for failed transfers.

CAVEATS

  • Eventual Consistency: Amazon S3 is an eventually consistent service for certain operations (e.g., PUTS of new objects in some regions, or eventual consistency for overwrite PUTS and DELETES). A read immediately after a write might not reflect the latest changes.
  • Large File Transfers: While aws s3 cp automatically handles multipart uploads for large files, network disruptions during transfers can lead to retries, potentially extending the total transfer time.
  • Pricing Implications: Copying data into or out of S3, especially across different AWS regions or using different storage classes, incurs data transfer and storage costs. Users should be aware of S3 pricing.
  • Permissions: Proper AWS Identity and Access Management (IAM) permissions are critical. The AWS CLI user or role must have sufficient permissions (s3:GetObject and s3:PutObject, plus s3:ListBucket for recursive operations) on both source and target paths.

COMMON USE CASES

  • Backup and Archiving: Copying local data to S3 for durable, secure, and cost-effective backups and long-term archiving.
  • Data Migration: Transferring large datasets between on-premises storage systems and S3, or moving data between different S3 buckets or AWS regions.
  • Static Website Hosting: Uploading static website content (HTML, CSS, JavaScript, images) to an S3 bucket configured for static website hosting.
  • Data Ingestion: Copying application logs, sensor data, or other generated data into S3 for subsequent processing, analysis, or machine learning workflows.

AUTHENTICATION AND CONFIGURATION

aws s3 cp operates using the credentials configured for the AWS CLI. These credentials can be provided through environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), a shared credentials file (~/.aws/credentials), or automatically assumed IAM roles when running on an EC2 instance or within other AWS services. The associated principal (user or role) must possess the necessary IAM permissions to read from the source and write to the destination S3 locations.

HISTORY

The aws s3 cp command is a core component of the AWS Command Line Interface (AWS CLI), which was first released by Amazon Web Services in 2013. The AWS CLI was developed to provide a unified, programmatic interface for interacting with AWS services from the command line. The high-level s3 commands, including cp, were specifically designed to offer a user-friendly and familiar syntax for common Amazon S3 operations, abstracting away the complexities of the underlying S3 API. Since its initial release, the AWS CLI has undergone continuous development, with regular updates introducing new features, performance enhancements, and stability improvements, reflecting the continuous evolution of the S3 service itself.

SEE ALSO

aws s3 sync, aws s3 mv, aws s3 ls, cp(1)

Copied to clipboard