Error while uploading image to storage bucket
I have a storage bucket, where I could upload and read images. With the help of supabase AI, created an edge function to generate a thumbnail image whenever an image is uploaded. And a webook (I think in the database functions) to trigger the edge function. After that I couldn't upload files and getting this error.
Error uploading image: {"code": "DatabaseError", "error": "DatabaseError", "message": "insert into "objects" ("bucket_id", "metadata", "name", "owner", "owner_id", "user_metadata", "version") values ($1, DEFAULT, $2, $3, $4, DEFAULT, $5) - record "new" has no field "mime_type"", "statusCode": "500"}
Any help is appreciated.
3 Replies
Are you trying to trigger the edge function from a trigger function on storage.objects? Your error sort of implies you have new.mime_type in your code which does not exist.
Show your trigger function if so.
Show your trigger function if so.
Yes, from my app, when I upload an image to supabase storage, the expectation is the edge function to trigger. The code I have in my app is
const onSelectImage = async () => {
try {
const options: ImagePicker.ImagePickerOptions = {
mediaTypes: ['images', 'videos'],
allowsEditing: false,
exif: true,
};
const result = await ImagePicker.launchImageLibraryAsync(options);
if (!result.canceled) {
setLoading(true);
const img = result.assets[0];
const base64 = await FileSystem.readAsStringAsync(img.uri, { encoding: 'base64' });
const filePath =
${user?.id}/${id}/${new Date().getTime()}.${img.type === 'image' ? 'png' : 'mp4'};
const contentType = img.type === 'image' ? 'image/png' : 'video/mp4';
const { error } = await supabase.storage.from(supabaseBucketName).upload(filePath, decode(base64), { contentType });
setLoading(false);
if (error) {
throw error;
}
Alert.alert("Success", "Image uploaded successfully");
setLoading(false);
await loadImages();
}
} catch (error) {
The webhook function is...
BEGIN
-- Only trigger for image files in the fast-user-files bucket
IF (NEW.bucket_id = 'fast-user-files' AND
NEW.name NOT LIKE 'small/%' AND
NEW.mime_type LIKE 'image/%') THEN
-- Use the Supabase Function URL
PERFORM net.http_post(
url:='https://YOUR_PROJECT.functions.supabase.co/image-thumbnail-generator-debug',
body:=json_build_object(
'type', TG_OP,
'table', TG_TABLE_NAME,
'record', row_to_json(NEW)
)::text,
headers:=ARRAY[
'Content-Type: application/json'
]
);
END IF;
RETURN NEW;
END;
mime_type is not a column. NEW gives you columns. You probably want to use the JSON in metadata column. There is a mimetype key in that.
Also if this is in insert trigger there are two inserts for each upload. The first checks the RLS and is not complete data. It is backed out after it is run. Then there is an insert with the file data.
You can use
raise log 'NEW = %',NEW;
after BEGIN to log to the Postgres log what is going on.