-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadSample.ts
More file actions
70 lines (56 loc) · 1.89 KB
/
uploadSample.ts
File metadata and controls
70 lines (56 loc) · 1.89 KB
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
import { createClient } from "@supabase/supabase-js";
import fs from "fs";
import path from "path";
// Manually parse .env.local
const envPath = path.join(process.cwd(), ".env.local");
const envContent = fs.readFileSync(envPath, "utf-8");
const envVars: Record<string, string> = {};
envContent.split("\n").forEach((line) => {
const [key, ...valueParts] = line.split("=");
if (key && valueParts.length > 0) {
let value = valueParts.join("=");
if (value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1, -1);
}
envVars[key.trim()] = value.trim();
}
});
const supabaseUrl = envVars["NEXT_PUBLIC_SUPABASE_URL"];
const supabaseKey =
envVars["SUPABASE_SERVICE_ROLE_KEY"] ||
envVars["NEXT_PUBLIC_SUPABASE_ANON_KEY"];
if (!supabaseUrl || !supabaseKey) {
console.error("Missing Supabase credentials in .env.local");
process.exit(1);
}
const supabase = createClient(supabaseUrl, supabaseKey);
async function uploadFile() {
const filePath = path.join(
process.cwd(),
"public/pdf/mileage-log-2024-01-01-2024-12-31.pdf"
);
if (!fs.existsSync(filePath)) {
console.error(`File not found at: ${filePath}`);
process.exit(1);
}
const fileBuffer = fs.readFileSync(filePath);
const fileName = "sample-mileage-log.pdf";
const bucketName = "public"; // Assumes 'public' bucket exists, change if needed (e.g., 'documents')
console.log(`Uploading ${fileName} to bucket '${bucketName}'...`);
const { data, error } = await supabase.storage
.from(bucketName)
.upload(fileName, fileBuffer, {
contentType: "application/pdf",
upsert: true,
});
if (error) {
console.error("Upload failed:", error);
process.exit(1);
}
const { data: publicUrlData } = supabase.storage
.from(bucketName)
.getPublicUrl(fileName);
console.log("Upload successful!");
console.log("Public URL:", publicUrlData.publicUrl);
}
uploadFile();