@ejekanshjain/cloud-storage
Version:
All in one package to handle files accross cloud storage services
50 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GCPStorageClient = void 0;
const storage_1 = require("@google-cloud/storage");
const zod_1 = require("zod");
const GCPStorageClientOptionsZ = zod_1.z.object({
projectId: zod_1.z.string().min(1),
privateKey: zod_1.z.string().min(1),
clientEmail: zod_1.z.string().min(1),
bucket: zod_1.z.string().min(1),
defaultMediaPublic: zod_1.z.boolean()
});
const GCPStorageClient = (options) => {
const { projectId, privateKey, clientEmail, bucket, defaultMediaPublic } = GCPStorageClientOptionsZ.parse(options);
const storage = new storage_1.Storage({
projectId,
credentials: {
private_key: privateKey,
client_email: clientEmail
}
});
const getBucket = () => storage.bucket(bucket);
const addFile = async (options) => {
const bucket = getBucket();
const fileRef = bucket.file(options.filename);
await fileRef.save(options.data, {
public: defaultMediaPublic
});
const url = fileRef.publicUrl();
return url;
};
const deleteFile = async (filename) => {
const bucket = getBucket();
const fileRef = bucket.file(filename);
await fileRef.delete();
};
const getFile = async (filename) => {
const bucket = getBucket();
const fileRef = bucket.file(filename);
const downloaded = await fileRef.download();
return downloaded[0];
};
return {
addFile,
deleteFile,
getFile
};
};
exports.GCPStorageClient = GCPStorageClient;
//# sourceMappingURL=gcp.js.map