0.1.0 • Published 4 months ago

@tsdiapi/s3 v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@tsdiapi/s3 – AWS S3 Plugin for TSDIAPI-Server

@tsdiapi/s3 is a plugin for the TSDIAPI-Server framework that enables seamless file management with AWS S3. It supports public and private bucket configurations, allowing you to upload, retrieve, delete files, and generate presigned URLs with ease.


šŸš€ Features

āœ… Public and Private Buckets – Manage files separately across public and private S3 buckets.
āœ… Presigned URLs – Generate time-limited URLs for secure access to private files.
āœ… File Uploads – Upload single or multiple files using buffers or file objects.
āœ… File Deletion – Remove files from both public and private buckets.
āœ… Flexible Configuration – Use inline options or environment variables for AWS credentials and bucket details.
āœ… Global Provider Access – Use getS3Provider() to access S3 services from anywhere in your app.


šŸ“¦ Installation

Install via NPM

npm install @tsdiapi/s3

Or Use the CLI

tsdiapi plugins add s3

šŸ”§ Configuration & Usage

Registering the Plugin in TSDIAPI

Add the plugin to your TSDIAPI-Server setup:

import { createApp } from "@tsdiapi/server";
import createPlugin from "@tsdiapi/s3";

createApp({
  plugins: [
    createPlugin({
      publicBucketName: "your-public-bucket",
      privateBucketName: "your-private-bucket",
      accessKeyId: "your-access-key-id",
      secretAccessKey: "your-secret-access-key",
      region: "your-region",
      customHost: "your-custom-host", // Optional (CDN or custom domain)
    }),
  ],
});

Alternatively, Configure via Environment Variables

Instead of hardcoding credentials, use .env variables:

AWS_PUBLIC_BUCKET_NAME=your-public-bucket
AWS_PRIVATE_BUCKET_NAME=your-private-bucket
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_REGION=your-region
AWS_CUSTOM_HOST=your-custom-host

Then initialize without passing options:

import createPlugin from "@tsdiapi/s3";
import { createApp } from "@tsdiapi/server";

createApp({
  plugins: [createPlugin()],
});

āš™ļø Plugin Options

OptionDescriptionRequired
publicBucketNameName of the public S3 bucketNo
privateBucketNameName of the private S3 bucketNo
accessKeyIdAWS access key IDYes
secretAccessKeyAWS secret access keyYes
regionAWS regionYes
customHostCustom host or CDN URLNo

Note: At least one of publicBucketName or privateBucketName must be set.


šŸ›  API Methods

1ļøāƒ£ Upload a File

import { getS3Provider } from "@tsdiapi/s3";

const s3 = getS3Provider();
const response = await s3.uploadFile(
  "your-public-bucket",
  "example.png",
  fileBuffer,
  "image/png"
);

console.log(response.url);

2ļøāƒ£ Upload Multiple Files

const responses = await getS3Provider().uploadFiles(fileArray);

3ļøāƒ£ Delete a File

await getS3Provider().deleteFile("path/to/file.png", false);

4ļøāƒ£ Generate a Presigned URL

const presignedUrl = await getS3Provider().getPresignedUrl("path/to/file.png", true);

5ļøāƒ£ Get Public URL

const publicUrl = getS3Provider().getPublicURL("path/to/file.png");

šŸ”„ Example Workflow

1ļøāƒ£ Upload Files → Upload files to public or private buckets.
2ļøāƒ£ Access Secure Files → Use presigned URLs to grant secure, temporary access to private files.
3ļøāƒ£ Batch Operations → Upload and manage multiple files at once.


āš ļø Error Handling

All methods include built-in error handling and logging:

try {
  const response = await getS3Provider().uploadFile(
    "your-public-bucket",
    "example.pdf",
    fileBuffer,
    "application/pdf"
  );
  console.log("File uploaded successfully:", response);
} catch (error) {
  console.error("File upload failed:", error);
}

🌐 Standalone Usage (Without TSDIAPI)

You can also use @tsdiapi/s3 outside of TSDIAPI-Server:

import { S3Provider } from "@tsdiapi/s3";

const s3 = new S3Provider();
s3.init({
    accessKeyId: "your-access-key",
    secretAccessKey: "your-secret-key",
    region: "your-region",
});

await s3.uploadFile("your-bucket", "example.txt", Buffer.from("Hello, S3!"), "text/plain");

šŸš€ Why Use @tsdiapi/s3?

  • šŸ”„ Easy AWS S3 Integration – No complex setup required.
  • šŸ”„ Works Inside & Outside TSDIAPI – Flexible plugin design.
  • šŸ† Full Feature Set – Upload, delete, presigned URLs, and more.
  • šŸ“Œ Secure & Configurable – Supports .env and custom authentication.

šŸ“¢ Contributing

We welcome contributions! If you have feature requests or bug reports, submit them via
šŸ‘‰ GitHub Issues.


šŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.


šŸ‘Øā€šŸ’» Author

Artyom Gorlovetskiy

šŸ“Œ GitHub: @unbywyd
šŸ“© Email: unbywyd@gmail.com


āœ… Now you're ready to manage files with AWS S3 using @tsdiapi/s3! šŸš€