-
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.
feat(s3): migrate files to s3, upload file on bdd and s3
- Loading branch information
Showing
9 changed files
with
166 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/sh | ||
|
||
# Configure the alias for MinIO | ||
mc alias set minio http://minio:9000 ${S3_BUCKET_ACCESS_KEY} ${S3_BUCKET_SECRET_KEY} | ||
|
||
# Check if the bucket exists | ||
if mc ls minio/${S3_BUCKET_NAME}; then | ||
echo "Bucket already exists" | ||
else | ||
# Create the bucket if it doesn't exist | ||
mc mb minio/${S3_BUCKET_NAME} | ||
echo "Bucket created successfully" | ||
fi |
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
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
51 changes: 51 additions & 0 deletions
51
packages/migrations/src/migrations/20240918085236_migrate_files_to_minio.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3"); | ||
|
||
const S3_BUCKET_NAME = process.env.S3_BUCKET_NAME; | ||
|
||
const s3Client = new S3Client({ | ||
credentials: { | ||
accessKeyId: process.env.S3_BUCKET_ACCESS_KEY, | ||
secretAccessKey: process.env.S3_BUCKET_SECRET_KEY, | ||
}, | ||
endpoint: process.env.S3_BUCKET_ENDPOINT, | ||
forcePathStyle: true, | ||
region: process.env.S3_BUCKET_REGION, | ||
}); | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.up = (knex) => { | ||
return knex | ||
.withSchema("doc") | ||
.select() | ||
.from("documents") | ||
.then(async (rows) => { | ||
for (const row of rows) { | ||
try { | ||
await s3Client.send( | ||
new PutObjectCommand({ | ||
Body: row.file, | ||
Bucket: S3_BUCKET_NAME, | ||
Key: `${row.uuid}.pdf`, | ||
Metadata: { | ||
category: String(row.category), | ||
created_at: String(row.created_at), | ||
mimetype: String(row.mime_type), | ||
originalname: String(row.filename), | ||
}, | ||
}), | ||
); | ||
} catch (err) { | ||
console.error(`Failed to upload ${row.uuid}:`, err); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* @param { import("knex").Knex } knex | ||
* @returns { Promise<void> } | ||
*/ | ||
exports.down = () => {}; |