mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-20 06:35:54 +02:00
feat: enable custom file storage for document siging
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { S3Client, PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
||||
import * as crypto from 'node:crypto';
|
||||
async function uploadFileToS3(buffer, fileName, mimeType, adapter) {
|
||||
const bucketName = adapter?.bucketName;
|
||||
let client;
|
||||
if (adapter?.fileAdapter === 'digitalocean') {
|
||||
client = new S3Client({
|
||||
endpoint: adapter?.endpoint,
|
||||
region: adapter?.region,
|
||||
credentials: { accessKeyId: adapter?.accessKeyId, secretAccessKey: adapter?.secretAccessKey },
|
||||
});
|
||||
} else {
|
||||
client = new S3Client({
|
||||
region: adapter?.region,
|
||||
credentials: { accessKeyId: adapter?.accessKeyId, secretAccessKey: adapter?.secretAccessKey },
|
||||
});
|
||||
}
|
||||
const prefixId = crypto.randomBytes(16).toString('hex');
|
||||
const fileKey = `${prefixId}_${fileName}`;
|
||||
const uploadParams = { Bucket: bucketName, Key: fileKey, Body: buffer, ContentType: mimeType };
|
||||
|
||||
try {
|
||||
// Upload the buffer to the Space
|
||||
const command = new PutObjectCommand(uploadParams);
|
||||
await client.send(command);
|
||||
const getCommand = new GetObjectCommand({ Bucket: bucketName, Key: fileKey });
|
||||
|
||||
// Generate a presigned URL for the uploaded file
|
||||
const presignedUrl = await getSignedUrl(client, getCommand, { expiresIn: 900 }); // URL expiration time in seconds (e.g., 15 min)
|
||||
return presignedUrl;
|
||||
} catch (error) {
|
||||
console.error('Error uploading file to aws:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export default uploadFileToS3;
|
||||
Reference in New Issue
Block a user