File size: 1,189 Bytes
16855a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { PutObjectCommand, GetObjectCommand } from '@aws-sdk/client-s3';
//import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; // Optional if we want signed URLs
import r2Client from '../config/storage.js';

const BUCKET_NAME = process.env.R2_BUCKET_NAME;

// Upload File to R2
export const uploadToR2 = async (fileBuffer, fileName, mimeType) => {
    const key = `tenders/${Date.now()}_${fileName.replace(/\s+/g, '_')}`;
    
    const command = new PutObjectCommand({
        Bucket: BUCKET_NAME,
        Key: key,
        Body: fileBuffer,
        ContentType: mimeType
    });

    await r2Client.send(command);
    return key;
};

// Get File Stream from R2
export const getFileStream = async (key) => {
    const command = new GetObjectCommand({
        Bucket: BUCKET_NAME,
        Key: key
    });

    const response = await r2Client.send(command);
    return response.Body;
};

// Get Full Object from R2 (Modified to return full response)
export const getFileObject = async (key) => {
    const command = new GetObjectCommand({
        Bucket: BUCKET_NAME,
        Key: key
    });

    return await r2Client.send(command);
};