42

データベースを使用するとsnapshot.exists()、特定のデータが存在するかどうかを確認できます。ドキュメントによると、ストレージを使用した同様の方法はありません。

https://firebase.google.com/docs/reference/js/firebase.storage.Reference

特定のファイルが Firebase Storage に存在するかどうかを確認する適切な方法は何ですか?

4

5 に答える 5

28

Promiseを返すgetDownloadURLを使用できます。これを使用して、「見つかりません」エラーをキャッチしたり、ファイルが存在する場合はファイルを処理したりできます。例えば:

storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);

function onResolve(foundURL) {
    //stuff
}

function onReject(error) {
    console.log(error.code);
}
于 2016-06-10T16:21:11.520 に答える
9

FB ストレージ API は、ユーザーが存在するファイルのみを要求するように設定されていると思います。

したがって、存在しないファイルはエラーとして処理する必要があります: https://firebase.google.com/docs/storage/web/handle-errors

于 2016-06-10T16:17:56.990 に答える
5

File.existsを使用して Node.js Firebase Gogole Cloud Storage SDK 内に保持している間に、私は素晴らしい解決策を見つけました。

const admin = require("firebase-admin");
const bucket = admin.storage().bucket('my-bucket');

const storageFile = bucket.file('path/to/file.txt');
storageFile
  .exists()
  .then(() => {
    console.log("File exists");
  })
  .catch(() => {
    console.log("File doesn't exist");
  });

Google Cloud Storage: Node.js SDK バージョン 5.1.1 (2020-06-19)執筆時点

于 2020-07-21T09:30:28.600 に答える