データベースを使用するとsnapshot.exists()
、特定のデータが存在するかどうかを確認できます。ドキュメントによると、ストレージを使用した同様の方法はありません。
https://firebase.google.com/docs/reference/js/firebase.storage.Reference
特定のファイルが Firebase Storage に存在するかどうかを確認する適切な方法は何ですか?
データベースを使用するとsnapshot.exists()
、特定のデータが存在するかどうかを確認できます。ドキュメントによると、ストレージを使用した同様の方法はありません。
https://firebase.google.com/docs/reference/js/firebase.storage.Reference
特定のファイルが Firebase Storage に存在するかどうかを確認する適切な方法は何ですか?
Promiseを返すgetDownloadURLを使用できます。これを使用して、「見つかりません」エラーをキャッチしたり、ファイルが存在する場合はファイルを処理したりできます。例えば:
storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);
function onResolve(foundURL) {
//stuff
}
function onReject(error) {
console.log(error.code);
}
FB ストレージ API は、ユーザーが存在するファイルのみを要求するように設定されていると思います。
したがって、存在しないファイルはエラーとして処理する必要があります: https://firebase.google.com/docs/storage/web/handle-errors
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)執筆時点