@tsdiapi/s3 v0.1.0
@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
Option | Description | Required |
---|---|---|
publicBucketName | Name of the public S3 bucket | No |
privateBucketName | Name of the private S3 bucket | No |
accessKeyId | AWS access key ID | Yes |
secretAccessKey | AWS secret access key | Yes |
region | AWS region | Yes |
customHost | Custom host or CDN URL | No |
Note: At least one of
publicBucketName
orprivateBucketName
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! š
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
6 months ago