From 3eeef95247cc285545cbd9e888faa0d29cd1ced3 Mon Sep 17 00:00:00 2001 From: Nihey Takizawa Date: Tue, 25 Aug 2020 15:38:32 -0300 Subject: [PATCH] Add getPresignedURL method on client --- README.md | 9 +++++++++ src/index.js | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/README.md b/README.md index cda24f7..22d3123 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,15 @@ client.downloadFile(filePathToRead, filePathToSave, createDirIfNotExists); - **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 + +```js +client.getPresignedURL(path, expires); +``` + +- **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 diff --git a/src/index.js b/src/index.js index 4b7b7d8..76c233c 100644 --- a/src/index.js +++ b/src/index.js @@ -58,6 +58,14 @@ class SpacesClient { return url.replace(/digitaloceanspaces/, 'cdn.digitaloceanspaces'); } + async getPresignedURL(path, expires = 900) { + return this.s3client.getSignedUrlPromise('getObject', { + Bucket: this.bucket, + Key: path, + Expires: expires, + }); + } + async isFilePublic(filePath) { const acl = await this.s3client.getObjectAcl({ Bucket: this.bucket,