firebase - 雲端函數 刪除資料夾:TypeScript
我的儲存桶 budget-32bfc 包含以下資料夾;
--+ Folder1 --+ Folder2 --+ Folder3
我想從雲端函數中刪除 Folder1。
嘗試 1:
gcs.bucket("budget-32bfc").file('Folder1').delete().then(() => {
console.log("Folder Deleted");
}).catch(function (error) {
console.log("Error listing users:", error);
});嘗試 2:
admin.bucket("budget-32bfc").file('Folder1').delete().then(() => {
console.log("Folder Deleted");
}).catch(function (error) {
console.log("Error listing users:", error);
});兩次嘗試都給了我一個 404 錯誤:'沒有這個物件:budget-32bfc/Folder1'。
我如何刪除一個資料夾?
答案#1
版本 1.7.x 有以下方法會返回一個 Promise:
deleteFiles(查詢, 回調)
它將嘗試刪除指定資料夾內的每個檔案(資料夾也包括在內)。如果刪除某個檔案失敗,過程將停止。(使用 'force: true' 來覆蓋此行為)。
bucket.deleteFiles({
prefix: `${userId}/images`
})
.catch( (err) => {
console.log(`Failed to delete all images of user ${userId}`);
});
官方文件:存儲 - 刪除檔案
這裡找到的另一個解決方案:點擊
(注意:我無法將此討論串標記為重複,因為我只有13點聲望,請停止刪除我的留言,因為其他人可能需要看到,謝謝)
答案#2
在雲端存儲中沒有「資料夾」的概念,它僅僅是每個檔案名稱的一部分。所以你必須刪除所有以 Folder1/ 開頭的檔案。一旦你這麼做,資料夾也就消失了。
另見:
- 如何使用 Node.js 獲取 Google Cloud Storage 資料夾中的檔案列表?(關於如何使用 Node.js 獲取 Cloud Storage 資料夾中的檔案列表)
- 如何在 Firebase 應用中獲取雲端存儲中所有檔案的列表?(Firebase SDK 沒有用於列出檔案的 API)
