A Node.js CLI tool for managing AWS S3 buckets using the AWS SDK and Commander.js.
-
Clone the repository:
git clone <your-repo-url> cd <your-repo-directory>
-
Install the dependencies:
npm install
-
Create a
.envfile in the root of the project directory with the following content:AWS_REGION=your-region S3_BUCKET=your-s3-bucket S3_PREFIX=your-s3-prefix
-
Link the package globally:
npm link
-
Use the
s3-clicommand to run the CLI tool (e.g.,s3-cli list-files). When you are done, you can unlink the package withnpm unlink.
- Use the
./cli.jscommand to run the CLI tool without global installation (e.g.,./cli.js list-files).
The CLI tool provides several commands to interact with your S3 bucket. Each command accepts an optional --aws-profile parameter to specify which AWS profile to use from your credentials file. If not provided, the default profile will be used.
List all files in the S3 bucket:
```bash
s3-cli list-files --bucket <bucket> --prefix <prefix> --filter <filter>
```
--bucket <bucket>: (Optional) The S3 bucket name. Defaults to the value in the.envfile.--prefix <prefix>: (Optional) The S3 prefix. Defaults to the value in the.envfile.--filter <filter>: (Optional) The filter to apply to the file list. This is a regular expression. Defaults to.*.
Example:
```bash
s3-cli list-files --bucket my-bucket --prefix my-prefix --filter '.*\.txt$'
```
Upload a local file to a defined location in the S3 bucket:
```bash
s3-cli upload-file --bucket <bucket> --key <key> --file <file>
```
--bucket <bucket>: (Optional) The S3 bucket name. Defaults to the value in the.envfile.--key <key>: The destination key in the S3 bucket.--file <file>: The local file path.
Example:
```bash
s3-cli upload-file --bucket my-bucket --key my-prefix/my-file.txt --file path/to/local/file.txt
```
Delete all files in the S3 bucket that match the filter:
```bash
s3-cli delete-files --bucket <bucket> --prefix <prefix> --filter <filter>
```
--bucket <bucket>: (Optional) The S3 bucket name. Defaults to the value in the.envfile.--prefix <prefix>: (Optional) The S3 prefix. Defaults to the value in the.envfile.--filter <filter>: The filter to apply to the file list. This is a regular expression.
Example:
```bash
s3-cli delete-files --bucket my-bucket --prefix my-prefix --filter '.*\.txt$'
```
Display help information for the CLI tool:
```bash
s3-cli help
```
Create a .env file in the root of the project directory with the following content:
```plaintext
AWS_REGION=my-region
S3_BUCKET=my-bucket
S3_PREFIX=my-prefix
```
Ensure the following environment variables are set in your environment or use aws profiles:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_REGION
Optionally, if you use session tokens:
AWS_SESSION_TOKEN