-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from zazuko/s3
S3 support
- Loading branch information
Showing
6 changed files
with
1,808 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// @ts-check | ||
|
||
import { GetObjectCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3"; | ||
|
||
/** | ||
* The S3 bucket to use. | ||
* @type {string} | ||
* @default "default" | ||
*/ | ||
export const s3Bucket = `${process.env.S3_BUCKET || "default"}`; | ||
|
||
/** | ||
* The S3 client. | ||
* @type {S3Client} | ||
*/ | ||
export const s3Client = new S3Client({ | ||
credentials: { | ||
accessKeyId: `${process.env.S3_ACCESS_KEY_ID}` || "", | ||
secretAccessKey: `${process.env.S3_SECRET_ACCESS_KEY}` || "", | ||
}, | ||
region: `${process.env.S3_REGION}` || "default", | ||
endpoint: `${process.env.S3_ENDPOINT}`, | ||
tls: process.env.S3_SSL_ENABLED === "true", | ||
forcePathStyle: process.env.S3_FORCE_PATH_STYLE === "true", | ||
}); | ||
|
||
/** | ||
* Get an object from S3. | ||
* | ||
* @param {string} key The key of the object to get. | ||
* @returns {Promise<import('@aws-sdk/client-s3').GetObjectCommandOutput>} | ||
*/ | ||
export const getObject = async (key) => { | ||
const command = new GetObjectCommand({ | ||
Bucket: s3Bucket, | ||
Key: key, | ||
}); | ||
return s3Client.send(command); | ||
}; | ||
|
||
/** | ||
* Save an object to S3. | ||
* | ||
* @param {string} key The key of the object to save. | ||
* @param {string} body The body of the object to save. | ||
* @param {string} [contentType="text/plain"] The content type of the object to save. | ||
* @returns {Promise<import('@aws-sdk/client-s3').PutObjectCommandOutput>} | ||
*/ | ||
export const saveObject = async (key, body, contentType = "text/plain") => { | ||
const command = new PutObjectCommand({ | ||
Bucket: s3Bucket, | ||
Key: key, | ||
Body: body, | ||
ContentType: contentType, | ||
}); | ||
return s3Client.send(command); | ||
}; |
Oops, something went wrong.