[flutter] 플러터를 사용하여 Firebase Storage의 파일 이름 변경하는 방법

Firebase는 Firebase Storage를 통해 파일을 업로드하고 저장할 수 있는 기능을 제공합니다. 때로는 이미 업로드한 파일의 이름을 변경해야하는 경우가 있는데, 이때 Flutter 앱에서 Firebase Storage의 파일 이름을 변경하는 방법에 대해 알아보겠습니다.

1. Firebase Storage 파일 이름 변경을 위한 단계

1.1. Firebase Storage 라이브러리 추가

먼저 pubspec.yaml 파일에 Firebase Storage 라이브러리를 추가합니다.

dependencies:
  firebase_storage: ^10.0.3

변경을 적용하기 위해 터미널에서 다음 명령을 실행합니다.

flutter pub get

1.2. 파일 이름 변경 코드 구현

다음으로, 파일 이름을 변경할 코드를 구현합니다.

import 'package:firebase_storage/firebase_storage.dart';

void renameFileInStorage(String filePath, String newFileName) async {
  Reference ref = FirebaseStorage.instance.ref(filePath);

  try {
    await ref.updateMetadata(contentType: null, 
        customMetadata: {'fileName': newFileName});
    print('File name updated successfully');
  } catch (e) {
    print('Error occurred: $e');
  }
}

위 코드에서 renameFileInStorage 함수는 filePath에 있는 파일의 이름을 newFileName으로 변경합니다.

1.3. 파일 이름 변경 기능 사용

이제 해당 함수를 사용하여 파일 이름을 변경할 수 있습니다.

renameFileInStorage('images/profile.jpg', 'new_profile.jpg');

위 코드는 images 폴더 안에 있는 profile.jpg 파일 이름을 new_profile.jpg로 변경합니다.

2. 요약

이렇게 하면 Flutter 앱에서 Firebase Storage의 파일 이름을 변경할 수 있습니다. 이를 이용하여 사용자 경험을 향상시키고 데이터를 보다 효율적으로 관리할 수 있습니다.

참고문헌:

부가적 참고: