[英]Python Firebase How to check whether a file exists on firebase storage
[英]How to check if a file exists in Firebase storage from your android application?
我正在開發 android 應用程序,用戶點擊圖像,它被存儲在 firebase 中,雲函數處理這個圖像並以文本文件的形式將輸出存儲回 firebase。 為了在 android 中顯示輸出,應用程序會不斷檢查輸出文件是否存在。 如果是,則它會在應用程序中顯示輸出。 如果沒有,我必須一直等待文件可用。
我找不到任何文檔來檢查 Firebase 中是否存在任何文件。 任何幫助或指示都會有所幫助。
謝謝。
您可以使用返回 Promise 的 getDownloadURL,后者又可用於捕獲“未找到”錯誤,或處理文件(如果存在)。 例如:
storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);
function onResolve(foundURL) {
//stuff
}
function onReject(error){
//fill not found
console.log(error.code);
}
更新
這是另一個更簡單、更清潔的解決方案。
storageRef.child("users/me/file.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// File not found
}
});
如果文件不存在,那么它將引發StorageException
; 然而StorageException
可以由不同的原因引發,每個原因都有一個唯一的錯誤代碼,定義為StorageException
類的常量。
如果該文件不存在,那么您將獲得StorageException.ERROR_OBJECT_NOT_FOUND
的錯誤代碼
如果您有文件的完整 URL 引用,則可以通過以下方式檢查它是否存在:
String url = "https://firebasestorage.googleapis.com/v0/b/******************"
StorageReference ref = FirebaseStorage.getInstance().getReferenceFromUrl(url);
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Log.d(LOG_TAG, "File exists");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
if (exception instanceof StorageException &&
((StorageException) exception).getErrorCode() == StorageException.ERROR_OBJECT_NOT_FOUND) {
Log.d(LOG_TAG, "File not exist");
}
}
});
其余的錯誤代碼可以在這里檢查
我的代碼
void getReferenceAndLoadNewBackground(String photoName) {
final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Photos").child(photoName + ".png");
storageReference.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
loadBackground(storageReference);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
int errorCode = ((StorageException) exception).getErrorCode();
if (errorCode == StorageException.ERROR_OBJECT_NOT_FOUND) {
StorageReference storageReference2 = FirebaseStorage.getInstance().getReference().child("Photos").child("photo_1.png");
loadBackground(storageReference2);
}
}
});
}
這就是我目前檢查文件是否存在的方式。
this.auth.user$ 從 FireStore 數據庫中拉取一個顯示當前用戶數據的可觀察對象。 我將 FileStorage 配置文件圖像引用存儲在 FireStore 數據庫中。
然后,我使用用戶數據中的文件路徑並將其用於 FileStorage 引用。
現在使用 observable 並檢查 downloadURL 長度是否小於或等於 0。
如果它確實大於零,則該文件存在; 然后去做點什么。 否則做點別的。
ngOnInit() {
this.userSubscription = this.auth.user$.subscribe((x) => {
console.log(x);
this.userUID = x.userId;
this.userPhotoRef = x.appPhotoRef;
this.userDownloadURL = x.appPhotoURL;
});
const storageRef = this.storage.ref(this.userPhotoRef);
console.log(storageRef);
if (storageRef.getDownloadURL.length <= 0) {
console.log('File Does not Exist');
} else {
console.log('File Exists');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.