with gzip for r2

This commit is contained in:
adueck 2023-12-18 17:32:27 +04:00
parent 72691403cd
commit 8cb1d8f043
1 changed files with 24 additions and 8 deletions

View File

@ -15,6 +15,7 @@ import { getWordList } from "./word-list-maker";
import { PublishDictionaryResponse } from "../../website/src/types/functions-types";
import { Storage } from "@google-cloud/storage";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import zlib from "zlib";
const s3Client = new S3Client({
region: "auto",
endpoint: functions.config().r2.endpoint,
@ -258,14 +259,29 @@ async function upload(content: Buffer | string, filename: string) {
: "text/plain; charset=UTF-8",
"Cache-Control": "no-cache",
};
// TODO: GZIP FILES
const putObjectCommand = new PutObjectCommand({
Bucket: functions.config().r2.bucket_name,
Key: `dictionary/${filename}`,
Body: content,
Metadata,
});
await s3Client.send(putObjectCommand);
if (isBuffer) {
const putObjectCommand = new PutObjectCommand({
Bucket: functions.config().r2.bucket_name,
Key: `dictionary/${filename}`,
Body: content,
Metadata,
});
await s3Client.send(putObjectCommand);
} else {
zlib.gzip(content, (err, buffer) => {
if (err) {
console.error(err);
}
const putObjectCommand = new PutObjectCommand({
Bucket: functions.config().r2.bucket_name,
Key: `dictionary/${filename}`,
Metadata,
Body: buffer,
ContentEncoding: "gzip",
});
s3Client.send(putObjectCommand);
});
}
}
// async function uploadHunspellToStorage(wordlist: {