Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 기초
- iP
- css
- leetcode189
- 커스텀알락
- 구조분해할당
- nvm
- youtube iframe
- yarn-berry
- mac
- 파이썬
- nvmrc
- react-native
- Rest
- 리액트
- Next.js
- firebaseui
- 프로토타입
- 타입스크립트
- JS
- 커스텀알림
- Python
- Spread
- React
- leetcode977
- 다리놓기
- react-firebaseui
- 백준
- 자바스크립트
- 파이어베이스로그인
Archives
- Today
- Total
JadeCode
[혼프] 혼자하는 프로젝트 3. firebase storage 본문
Firebase Storage 사진저장 방법
사진을 저장하기 위해 또 clientApp.ts에서 새롭게 import할 라이브러리가 있다.
// firebase/clientApp.ts
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
import "firebase/compat/auth";
import firebaseConfig from "firebaseConfig.json";
const app = firebase.initializeApp(firebaseConfig);
const firestore = getFirestore(app); //DB
const firestorage = getStorage(app); //사진저장
const fireAuth = firebase.auth(); //인증
export { firestore, fireAuth, firestorage };
getStorage를 불러온 후 export 해 준다.
//imageUpload.ts
//이미지 업로드 함수
//return 값 Promise<string>
import { firestorage } from "firebase/clientApp";
import { ref, uploadBytesResumable, getDownloadURL } from "firebase/storage";
import { v4 as uuidv4 } from "uuid"; //firebase image upload용 id
const firebaseImageUpload = (files: FileList): Promise<string> => {
return new Promise(async (resolve, reject) => {
try {
const storageRef = ref(firestorage, uuidv4());
const uploadTask = uploadBytesResumable(storageRef, files[0]);
uploadTask.on(
"state_changed",
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
progress === 100 && alert("Upload is " + progress + "% done");
switch (snapshot.state) {
case "paused":
console.log("Upload is paused");
break;
case "running":
console.log("Upload is running");
break;
}
},
(error) => {
// Handle unsuccessful uploads
reject(error);
},
async () => {
// Handle successful completion of the upload task here
const downloadUrl = await getDownloadURL(uploadTask.snapshot.ref);
console.log(
`File uploaded successfully, download URL: ${downloadUrl}`
);
resolve(downloadUrl);
}
);
} catch (error) {
reject(error);
}
});
};
export default firebaseImageUpload;
사용법
위에서 정리한 imageUpload import하기
....
const handleFileUpload = async (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
await imageUpload(e.target.files).then((res) => {
setForm([...res]);
}
});
}
};
...
return (
...
<input
id="imageInput"
type="file"
accept="image/*"
onChange={handleFileUpload}
multiple
/>
);
참고
https://firebase.google.com/docs/storage/web/upload-files?hl=ko
웹에서 Cloud Storage로 파일 업로드 | Firebase용 Cloud Storage
5월 10일, Google I/O에서 Firebase가 돌아옵니다. 지금 등록하기 의견 보내기 웹에서 Cloud Storage로 파일 업로드 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Fireb
firebase.google.com
'개발' 카테고리의 다른 글
React-Query 도입기 (0) | 2023.05.02 |
---|---|
모노레포(MonoRepo) (0) | 2023.04.27 |
[혼프] 혼자하는 프로젝트 2. firebase store (0) | 2023.03.30 |
[혼프] 혼자하는 프로젝트 1. firebase-auth 적용기 (0) | 2023.03.29 |
React-Query 도입에 대한 고민 (0) | 2023.03.16 |
Comments