react-s3-typescript
v4.0.6
Published
A npm package to upload your files into AWS S3 Bucket directly using react
Downloads
242
Maintainers
Readme
react-s3-typescript
A npm package to upload your files into AWS S3 Bucket directly using ReactJS or NodeJS with aws sdk version 3. It also support upload with progress functionality.
What new in version 4.0.0
I add a new function for upload large or big file with progress bar status. No other changes.
It supports upload process with progress status(Upload Large File).
Aws SDK Version 3
Introduction
A npm package to upload your media and other types of files directly into AWS S3 Bucket using ReactJs or NodeJS with aws sdk version 3. You can upload and and delete the file if needed! It also support upload with progress bar system.
You need to have an AWS account to use AWS S3 Bucket. If you already do not have an account, create an account here.
Read more about AWS S3 bucket: https://docs.aws.amazon.com/s3/index.html
Features
- Upload files to S3 bucket and retrieve the public url
- Delete files from S3 bucket
- Upload large or big file with progress
- Aws Javascript SDK Version 3 Integrated!
- No unnecessary Dependencies
- Small in Size
Installing
Using npm
$ npm install react-s3-typescript
Example
Upload a file to AWS S3 Bucket
You can define a default directory for uploads using the s3Config object
/* s3Config.ts */
/* Configure Aws S3 */
export const s3Config = {
bucketName: 'bucket-name',
dirName: 'directory-name', /* Optional */
region: 'ap-south-1',
accessKeyId:'ABCD12EFGH3IJ4KLMNO8',
secretAccessKey: 'a12bCde3f4+5GhIjKLm6nOpqr7stuVwxy8ZA9bC9',
s3Url: 'https:/your-aws-s3-bucket-url/' /* Optional */
}
/* End of s3Config.ts */
/* AWS S3 Client */
/* uploadFile.ts */
import { ReactS3Client } from 'react-s3-typescript';
import { s3Config } from './s3Config.ts';
const uploadFile = async () => {
const s3 = new ReactS3Client(s3Config);
/* You can use the default directory defined in s3Config object
* Or you can a define custom directory to upload when calling the
* constructor using js/ts object destructuring.
*
* const s3 = new ReactS3Client({
* ...s3Config,
* dirName: 'custom-directory'
* });
*
*/
const filename = 'filename-to-be-uploaded'; /* Optional */
/* If you do not specify a file name, file will be uploaded using uuid generated
* by short-UUID (https://www.npmjs.com/package/short-uuid)
*/
const res = await s3.uploadFile(file, filename);
console.log(res);
/* End of uploadFile.ts */
Upload big or large file with progress status
/* AWS S3 Client */
/* uploadFile.ts */
import { ReactS3Client } from 'react-s3-typescript';
import { s3Config } from './s3Config.ts';
const uploadFile = async () => {
const s3 = new ReactS3Client(s3Config);
/* You can use the default directory defined in s3Config object
* Or you can a define custom directory to upload when calling the
* constructor using js/ts object destructuring.
*
* const s3 = new ReactS3Client({
* ...s3Config,
* dirName: 'custom-directory'
* });
*
*/
const filename = 'filename-to-be-uploaded'; /* Optional */
/* If you do not specify a file name, file will be uploaded using uuid generated
* by short-UUID (https://www.npmjs.com/package/short-uuid)
*/
const res = await s3.uploadWithProgress(
file,
//Progress Callback
(progress, details) => {
//Progress in number (0.00 to 1.00)
console.log("Progress in number", progress);
console.log("Progress Details", details);
},
filename
);
//Final Result
console.log(res);
/* End of uploadFile.ts */
Delete a file from AWS S3 Bucket
/* AWS S3 Client */
/* deleteFile.ts */
import { ReactS3Client } from 'react-aws-s3-typescript';
import { s3Config } from './s3Config.ts';
const deleteFile = async () => {
const s3 = new ReactS3Client(s3Config);
const filepath = 'directory-name/filename-to-be-deleted';
const data = await s3.deleteFile(filepath);
console.log(data);
}
/* End of deleteFile.ts */
Important : Add public-read
Canned ACL to the bucket to grant the public read access for files.
Read more: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
S3 Config
These are the available config options for calling the S3 constructor bucketName
, region
, accessKeyId
and secretAccessKey
are required. Upload will default to root path, if dirName
is not specified.
Stay in touch
- Author - Siam Ahnaf
- Website - https://www.siamahnaf.com/
- Github - https://github.com/siamahnaf
License
Released under MIT license.