-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8c7e7c
commit d51ec84
Showing
5 changed files
with
115 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"ignore": ["**/*.test.js", "**/*.spec.js", "node_modules"], | ||
"watch": ["."], | ||
"exec": "npm start", | ||
"exec": "npm run dev", | ||
"ext": "js" | ||
} | ||
} |
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,65 @@ | ||
import fs from "fs"; | ||
import { lookup } from "mime-types"; | ||
import { | ||
S3Client, | ||
PutObjectCommand, | ||
ListObjectsV2Command, | ||
} from "@aws-sdk/client-s3"; | ||
|
||
export class AWS_S3Client { | ||
constructor() { | ||
this._client = new S3Client(); | ||
} | ||
|
||
async upload(bucketName, name, objectPath) { | ||
if (!bucketName) { | ||
console.error("No bucket name provided in environment variables."); | ||
return null; | ||
} | ||
|
||
const params = { | ||
Bucket: bucketName, | ||
Body: fs.createReadStream(objectPath), | ||
Key: name, | ||
ContentType: lookup(objectPath) || "text/plain", | ||
}; | ||
|
||
try { | ||
// Send the PutObjectCommand to S3 | ||
const data = await this._client.send(new PutObjectCommand(params)); | ||
|
||
return { result: data }; | ||
} catch (err) { | ||
// Log the detailed error for troubleshooting | ||
console.error("Error put objects:", JSON.stringify(err, null, 2)); | ||
return Promise.reject(null); | ||
} | ||
} | ||
|
||
async list(bucketName) { | ||
if (!bucketName) { | ||
console.error("No bucket name provided in environment variables."); | ||
return null; | ||
} | ||
|
||
const params = { | ||
Bucket: bucketName, | ||
}; | ||
|
||
try { | ||
// Send the ListObjectsV2Command to S3 | ||
const data = await this._client.send(new ListObjectsV2Command(params)); | ||
|
||
// Extract object keys if Contents exists | ||
const objectKeys = data.Contents | ||
? data.Contents.map((obj) => obj.Key) | ||
: []; | ||
|
||
return { objectKeys: objectKeys }; | ||
} catch (err) { | ||
// Log the detailed error for troubleshooting | ||
console.error("Error listing objects:", JSON.stringify(err, null, 2)); | ||
return Promise.reject(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