-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file-uploader): added base for s3 storage (#214)
* feat(file-uploader): added base for s3 storage * refactor: added todo for eslint rules * fix: try to fix yarn.lock
- Loading branch information
Showing
7 changed files
with
399 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import S3 from 'aws-sdk/clients/s3.js'; | ||
import type { Buffer } from 'buffer'; | ||
|
||
/** | ||
* Class to handle S3 bucket operations | ||
*/ | ||
export class S3Storage { | ||
/** | ||
* S3 instance | ||
*/ | ||
private readonly s3: S3; | ||
|
||
/** | ||
* Constructor for S3Bucket | ||
* | ||
* @param accessKeyId - AWS access key | ||
* @param secretAccessKey - AWS secret access key | ||
* @param region - AWS region | ||
* @param endpoint - AWS endpoint (in case of localstack or other S3 compatible services) | ||
*/ | ||
constructor(accessKeyId: string, secretAccessKey: string, region?: string, endpoint?: string) { | ||
this.s3 = new S3({ | ||
endpoint, | ||
region, | ||
s3ForcePathStyle: true, | ||
credentials: { | ||
accessKeyId, | ||
secretAccessKey, | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Method to upload a file to S3 | ||
* | ||
* @param bucket - S3 bucket name | ||
* @param key - Key to store the file in S3 | ||
* @param file - file data to upload | ||
*/ | ||
public async uploadFile(bucket: string, key: string, file: Buffer): Promise<string | null> { | ||
/** | ||
* Create an upload manager to upload the file to S3 | ||
*/ | ||
const uploadManager = this.s3.upload({ | ||
Bucket: bucket, | ||
Key: key, | ||
Body: file, | ||
}); | ||
|
||
/** | ||
* Wait for the upload to complete and return the URL of the uploaded file | ||
*/ | ||
try { | ||
const response = await uploadManager.promise(); | ||
|
||
return response.Location; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Method to get a file from S3 | ||
* | ||
* @param bucket - S3 bucket name | ||
* @param key - Key of the file in S3 | ||
*/ | ||
public async getFile(bucket: string, key: string): Promise<Buffer | null> { | ||
const getObjectManager = this.s3.getObject({ | ||
Bucket: bucket, | ||
Key: key, | ||
}); | ||
|
||
try { | ||
const response = await getObjectManager.promise(); | ||
|
||
return response.Body as Buffer; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.