Created
March 3, 2020 01:30
-
-
Save f1lander/287cf20fb8dbfcce24a5c3e867346788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AWS from 'aws-sdk'; | |
// set your process env | |
const { | |
S3_BUCKET, | |
STAGE } = process.env; | |
const endpoint = 'http://localhost:4572'; | |
// set your config look | |
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-configuring-buckets.html | |
const s3Config = { endpoint }; | |
export default class S3Util { | |
/** | |
* | |
* @param {Bucket} Bucket defines the bucket for every call for s3 | |
*/ | |
constructor(Bucket = S3_BUCKET) { | |
this.params = { | |
Bucket | |
}; | |
this.s3 = new AWS.S3(s3Config); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
*/ | |
headObject = (params) => { | |
return this.s3.headObject({ ...this.params, ...params }).promise(); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
*/ | |
copyObject = (params) => { | |
return this.s3.copyObject({ ...this.params, ...params }).promise(); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
* @param {operation} got as default getObject, you can set the operation you need | |
*/ | |
getSignedUrl = (params, operation = 'getObject') => { | |
return this.s3.getSignedUrl(operation, { ...this.params, ...params }); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
*/ | |
createPresignedPost = (params) => { | |
return this.s3.createPresignedPost({ ...this.params, ...params }); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
*/ | |
getTaggingObjects = async (params) => { | |
return this.s3.getObjectTagging({ ...this.params, ...params }).promise(); | |
} | |
/** | |
* | |
* @param {params} defines the params for s3 call like BUcket, Key, etc. | |
*/ | |
deleteObject = async (params) => { | |
return this.s3.deleteObject({ ...this.params, ...params }).promise(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment