How to add an image to storage and get publicUrl by using the "seed.sql" file? #7430
-
Could someone explain how I can add an image file to a storage bucket and reference the publicUrl in a database table by using the "seed.sql" file during the starting process?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
the public URL is a deterministic URL composed as follows:
I hope it helps |
Beta Was this translation helpful? Give feedback.
-
I don't think this answers the question about how we can seed the actual images in buckets in local dev through To answer the question:
123 could be shortcut'd by doing the work locally in Supabase Studio and doing a |
Beta Was this translation helpful? Give feedback.
-
Having out of the box support for that using seed.sql file would be superb, but since it's not yet supported, here is my solution that works quite well for me so far:
import { createClient } from "@supabase/supabase-js";
default async function getImageRefFromImage(
supabaseClient: any,
inputImage: string | File | Blob,
imagePrefix: string,
debounceMs: number = 0,
collectionName: string = '<db_bucket_name>',
upsert: boolean = false, // override if file exists
) {
// inputImage can be either a string (url) or a File
let image = inputImage;
if (typeof inputImage === "string" && inputImage.startsWith("https://")) {
image = await fetch(inputImage).then((r) => r.blob());
await new Promise((resolve) => setTimeout(resolve, debounceMs));
}
const { data: imageData, error } = await supabaseClient.storage
.from(collectionName)
.upload(`${imagePrefix}_image` + ".jpg", image, {
upsert: upsert,
});
if (error) logErrorMessage(error);
return imageData.path;
}
async function placeImagesToBucket() {
const client = createClient(
"http://localhost:54321",
"<anon-key>"
{ auth: { persistSession: false } },
);
let { data } = await client.from("<db-table-with-column-image").select();
if (data) {
for (const item of data) {
if (item.image?.startsWith("https")) {
const imagePath = await getImageRefFromImage(
client,
item.image,
item.id,
100,
);
console.log(`For item(id=${item.id}), imagePath is ${imagePath}`);
await client
.from("meals")
.update({
image: imagePath,
})
.eq("id", item.id);
}
}
}
}
placeImagesToBucket();
|
Beta Was this translation helpful? Give feedback.
the public URL is a deterministic URL composed as follows:
I hope it helps