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 |
Tags
- UI
- 자바스크립트
- https://lo-victoria.com/introduction-to-redux-toolkit-for-beginners
- slice/splice/split
- Beesbeesbees
- dom
- redux상태유지
- 자바스크립트#조건문#문자열
- 노드교과서
- https://developer-talk.tistory.com/299
- ㄷㅌ
- UX
- for~in/for~of
- removeCookie
- js
- toString#String
- 자바스크립트#JS#var#let#const#undefined#null
- variable#function
- https://dasima.xyz/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%A0%9C%EA%B3%B1-math-pow-%EA%B3%84%EC%82%B0/
- User Flow
- https://www.daleseo.com/js-array-slice-splice/
- JS#3일차달리자#초반인데#시간금방~
- react
- 헷갈린다~
- 내장고차함수
- @redux-toolkit
- cmarket
- CSS
- children vs childrenNodes
- 자바스크립트#JS#slice#splice
Archives
- Today
- Total
Daily Front_Minhhk
[firebase] storage 설정 (+ 파일 업로드, 다운로드) 본문
빌드에서 Storage 를 눌러 생성!
스토리지 위치는 당연 서울!
공식문서를 살펴보자 (Storage)
firebase.js 파일에서
const storage = getStorage(app)
import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
storageBucket: ''
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
// Initialize Cloud Storage and get a reference to the service
const storage = getStorage(app);
Blob 또는 File에서 업로드
적절한 참조를 만들었으면 uploadBytes() 메서드를 호출합니다. uploadBytes()는 자바스크립트 File 및 Blob API를 통해 파일을 가져와서 Cloud Storage에 업로드합니다.
import { getStorage, ref, uploadBytes } from "firebase/storage";
const storage = getStorage();
const storageRef = ref(storage, 'some-child');
// 'file' comes from the Blob or File API
uploadBytes(storageRef, file).then((snapshot) => {
console.log('Uploaded a blob or file!');
});
공식문서를 참고하여
작성 ->>
1. uploadBytes(storageRef, file) 에서
storageRef 는
ref를 import 하여 인자로 storage 와 로그인한 유저의 개인 uid를 넣어 구별해준다.
2. 다음 file 부분은 <input type="file">로 등록한 이미지 파일을 가져오자
const file = e.target.files[0]
3. 파일 업로드 코드이다. snapshot 을 콘솔로 찍으면
//! 프로필사진 storage 업로드
const uploadImg = async (e) => {
// 업로드한 이미지 파일
const file = e.target.files[0];
console.log(file);
const storageRef = ref(storage, userUid);
try {
//! 스토리지 파일 저장
await uploadBytes(storageRef, file).then(
(snapshot) => {
console.log("Uploaded a blob or file!");
console.log(snapshot)
}
);
} catch (err) {
console.log(err);
}
};
콘솔에서 찍은 결과를 볼 수가 있다.
파이어베이스 storage 에서도 확인
by
URL을 통해 데이터 다운로드
공식문서의 코드에서 요점은 아래와 같다.
getDownloadURL(ref(storage, 파일))
import { getStorage, ref, getDownloadURL } from "firebase/storage";
const storage = getStorage();
getDownloadURL(ref(storage, 'images/stars.jpg'))
.then((url) => {
// `url` is the download URL for 'images/stars.jpg'
// This can be downloaded directly:
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = (event) => {
const blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
// Or inserted into an <img> element
const img = document.getElementById('myimg');
img.setAttribute('src', url);
})
.catch((error) => {
// Handle any errors
});
>>
현재 프로젝트에서는 업로드한 사진을 가져올 것이기 때문에 변수를 생성하여 담아 주었다.
let downStoragePhotoUrl = await getDownloadURL(ref(storage, userUid));
console.log(downStoragePhotoUrl);
콘솔에 찍어보면 링크로 나온다!!
이것을 갖고 유저정보 업데이트를 해준다. {photoURL : downStoragePhotoUrl }
// 파이어베이스 유저정보 업데이트
await updateProfile(appAuth.currentUser, {
photoURL: downStoragePhotoUrl,
});
결과 화면 >>
by
'Project > ChatProject' 카테고리의 다른 글
[firebase] 이메일,비밀번호 로그인 & 구글로그인 & 로그아웃 (0) | 2023.05.08 |
---|---|
[firebase] 실시간 데이터베이스 쓰기 (0) | 2023.05.08 |
[firebase] 회원가입 (createUserWithEmailAndPassword, updateProfile ) (0) | 2023.05.07 |
react-hook-form / 가입폼, 유효성 검사 (라이브러리) (0) | 2023.05.07 |