[supabase]그림/영상 업로드

코드왕·2023년 12월 25일
  1. 그림 업로드 시에는 base64 형태로 변환해서 올려야한다.
    useState 형태의 변수를쓰면 틀어짐
import * as ImagePicker from 'expo-image-picker';
import { decode } from 'base64-arraybuffer'
import * as FileSystem from 'expo-file-system';


  const pickImage = async () => {
    // No permissions request is necessary for launching the image library
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.Videos,
      quality: 1,
    });

    console.log(result);

    if (result) {
      const img = result.assets[0];
      const base64 = await FileSystem.readAsStringAsync(img.uri, { encoding: 'base64' });
      setImageBase64(base64)
      uploadImage(base64)
    }
  };

  const uploadImage= async (imageBase64)=>{
    const { data, error } = await supabase
    .storage
    .from('test')
    .upload('abc/test5.mp4', decode(imageBase64), {
      contentType: 'video/mp4'
    })

    if (error) {
      console.log(error);
    } else {
      console.log(data);
    }

  }

위 코드에서 만약 이미지를 업로드하고 싶다면 contentType을 image/jpg로 바꿔주야 함

profile
CODE DIVE!

0개의 댓글