FileUploadS3
Module is configuration only and simplifies uploads to AWS S3 cloud storage. It also provides methods for creating signed, expiring urls.
Creating FileUpload Object
To create the module (instantiate class) and prepare it for use, just provide configuration object as following:
All FileUploadS3
configuration properties are mandatory unless stated differently.
key
type
description
s3
object
Configuration obtained from AWS S3, keys are accessKeyId
, secretAccessKey
, region
bucket
string
S3 bucket name
urlExpiration (optional)
integer
, defaults to 120
Expiration time in seconds if you are using signed links
Caution: do not store s3 keys in the repository, use dotenv or environment variables.
Uploading files
To upload a file, you use instantiated FileUploaderS3
, provide a <Buffer>
, path (key) and optional callbacks for upload success and upload error.
Uploader.upload
configuration object:
key
type
description
file
Buffer
Configuration obtained from AWS S3, keys are accessKeyId
, 'secretAccessKey', region
key
string
path (key), and filename with extension
Uploading files to different buckets
To upload files to different buckets, instantiate two objects with separate configuration and use those accordingly:
Creating signed links
AWS S3 can provide signed links, that have expiry date. If you prefer signed links, you should set your bucket to private
. Rest is easy as:
Please note that function is synchronous and requires one argument (key
). If you want to set up custom expiry, you can set urlExpiration
in FileUploadS3 configuration object. Default urlExpiration is set to 120 seconds.
Last updated