File size: 3,689 Bytes
7a3f393 2b1f3f2 7a3f393 2b1f3f2 7a3f393 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | const {
S3Client,
PutObjectCommand,
DeleteObjectCommand,
ListObjectsV2Command,
GetObjectCommand,
HeadObjectCommand,
} = require('@aws-sdk/client-s3');
const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
const BUCKET = process.env.R2_BUCKET_NAME; // "bet"
const ACCOUNT_ID = process.env.R2_ACCOUNT_ID;
const r2 = new S3Client({
region: 'auto',
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: process.env.R2_ACCESS_KEY_ID,
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY,
},
});
const USER_QUOTA_BYTES = 45 * 1024 * 1024; // 45 MB
/**
* Upload a file buffer to R2.
* @param {string} key - e.g. "cvbora/users/{userId}/uploads/resume.pdf"
* @param {Buffer} buffer
* @param {string} contentType
* @returns {Promise<{key, size}>}
*/
async function uploadFile(key, buffer, contentType) {
const command = new PutObjectCommand({
Bucket: BUCKET,
Key: key,
Body: buffer,
ContentType: contentType,
ContentLength: buffer.length,
});
await r2.send(command);
return { key, size: buffer.length };
}
/**
* Upload a UTF-8 string (e.g. HTML) to R2.
*/
async function uploadText(key, text, contentType = 'text/html; charset=utf-8') {
const buffer = Buffer.from(text, 'utf-8');
return uploadFile(key, buffer, contentType);
}
/**
* Delete an object from R2.
* @param {string} key
*/
async function deleteFile(key) {
const command = new DeleteObjectCommand({ Bucket: BUCKET, Key: key });
await r2.send(command);
}
/**
* List all objects under a prefix.
* @param {string} prefix - e.g. "cvbora/users/{userId}/"
* @returns {Promise<Array<{key, size, lastModified}>>}
*/
async function listFiles(prefix) {
const command = new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: prefix,
});
const response = await r2.send(command);
return (response.Contents || []).map(obj => ({
key: obj.Key,
size: obj.Size,
lastModified: obj.LastModified,
}));
}
/**
* Get the size of a single object.
* @param {string} key
* @returns {Promise<number>} size in bytes
*/
async function getObjectSize(key) {
try {
const command = new HeadObjectCommand({ Bucket: BUCKET, Key: key });
const response = await r2.send(command);
return response.ContentLength || 0;
} catch {
return 0;
}
}
/**
* Generate a presigned GET URL (1 hour expiry).
* @param {string} key
* @returns {Promise<string>}
*/
async function getPresignedUrl(key, expiresInSeconds = 3600) {
const command = new GetObjectCommand({ Bucket: BUCKET, Key: key });
return getSignedUrl(r2, command, { expiresIn: expiresInSeconds });
}
/**
* Build the R2 key prefix for a user's CVBora files.
* @param {string} userId
* @param {'uploads'|'generated'} folder
*/
function userPrefix(userId, folder) {
return `cvbora/users/${userId}/${folder}/`;
}
/**
* Download the full content of an R2 object as a Buffer.
* @param {string} key
* @returns {Promise<Buffer>}
*/
async function getFileContent(key) {
const command = new GetObjectCommand({ Bucket: BUCKET, Key: key });
const response = await r2.send(command);
// response.Body is a ReadableStream (AWS SDK v3)
const chunks = [];
for await (const chunk of response.Body) {
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
}
return Buffer.concat(chunks);
}
module.exports = {
uploadFile,
uploadText,
deleteFile,
listFiles,
getObjectSize,
getFileContent,
getPresignedUrl,
userPrefix,
USER_QUOTA_BYTES,
};
|