Simple Digital Ocean Spaces Client
Usage
Credentials setup
You can set the environment variables AWS_ACCESS_KEY_ID
and AWS_SECRET_KEY
as default credentials for the S3 SDK which is also used by Digital Ocean Spaces API.
Create client for Digital Ocean Spaces API
; const client = 'sfo2.digitaloceanspaces.com' // Required, Digital Ocean Spaces Endpoint 'bucketName' // Required. Bucket name 'accessKeyId' // Optional. Access key, can be provided via env var AWS_ACCESS_KEY_ID 'secretAccessKey' // Optional. Access key secret, can be env var AWS_SECRET_ACCESS_KEY 'digitalOceanAPIToken' // Optional. Digital Ocean API Token, used to purge the cdn cache 'cdnEndpointId' // Optional. CDN's Endpoint Id, used to purge the cdn cache, // if not provided will be auto-extracted from bucketName.;
All methods returns a Promise which resolves to the values described below.
Upload file
client;
- uploadFilePath : File path in file system
- destinationPath : Path to save in DO Spaces
- permission : Permission of the file, default is
private
, can also bepublic-read
. - options : Options object to be sent to S3 SDK
- exponentialBackoff : Use exponential backoff if the upload failed
- purgeCache : Purge the cache from CDN related to this new file
Return file URL in DO Spaces CDN
List files
client;
- path : Path in DO Spaces to list
- options : Options object
- sortByDate : Sort files by LastModified date, can be 'ASC' (default) or 'DESC'
- pathOnly : If true it will not return the complete URL just the path in DO Spaces
Return array of objects with the 'url' and 'lastModified' properties
url: 'file url' lastModified: Date ...
Delete one file
client;
- path : Relative path to one file in DO spaces to delete it
Delete multiple files
client;
- paths : Array of relative paths in DO spaces to delete
Copy file within DO Spaces
client;
Download file
client;
- filePathToRead : Path in DO Spaces
- filePathToSave : Path to save in file system
- createDirIfNotExists : Boolean to create the folder if the it doesn't exists. Default is
true
Get Pre-signed URL for private files
client;
- path : Path for the file to get the pre-signed URL
- expires : Time to expire the pre-signed URL (in seconds) (defaults to 900 seconds / 15 minutes);
License
MIT © Bruno Orlandi