如何使用云功能删除文件?

时间:2017-04-26 13:21:34

标签: node.js firebase firebase-storage google-cloud-functions

当我删除Firebase数据库中的帖子时,我希望云功能相应地删除firebase存储中的帖子缩略图。我的问题是,当我试图删除缩略图时,我认为我没有正确找到图像文件。

以下是我的尝试:

const functions = require('firebase-functions')
const admin = require('firebase-admin')
const gcs = require('@google-cloud/storage')()

exports.deletePost = functions.database.ref('Posts/{pushId}').onWrite(event => {

  const original = event.data.val()
  const previous = event.data.previous.val()
  const pushId = event.params.pushId

  if (original === null)
    return

  const filePath = 'Posts/' + pushId + 'thumbnail.jpg'
  const bucket = gcs.bucket('postsapp-12312')
  const file = bucket.file(filePath)
  const pr = file.delete()


  return pr
});

这就是我在日志中获得的内容

  

ApiError:未找到       at Object.parseHttpRespBody(/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:192:30)       at Object.handleResp(/user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:132:18)       at /user_code/node_modules/@google-cloud/storage/node_modules/@google-cloud/common/src/util.js:465:12       在Request.onResponse [as _callback](/user_code/node_modules/@google-cloud/storage/node_modules/retry-request/index.js:120:7)       在Request.self.callback(/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:188:22)       在emitTwo(events.js:106:13)       在Request.emit(events.js:191:7)       在请求。 (/user_code/node_modules/@google-cloud/storage/node_modules/request/request.js:1171:10)       在emitOne(events.js:96:13)       在Request.emit(events.js:188:7)

5 个答案:

答案 0 :(得分:7)

我设法解决了这个问题。这里的问题是我写的桶地址错了;它应该是postsapp-12312.appspot.com而不是postsapp-12312

<强>更新 要更好地检查存储区地址@Robert answer

答案 1 :(得分:5)

您可以使用firebase功能环境配置来获取存储桶名称:

const bucket = gcs.bucket(functions.config().firebase.storageBucket)

答案 2 :(得分:4)

对于仍在抓挠中的其他人,我可以通过排除存储桶名称来删除我的存储桶(并因此选择默认的Firebase存储桶):

captureSettings

答案 3 :(得分:3)

据我所知,无法从云功能中删除firebase存储文件。您可以在存储桶上收听更改事件,但无法与其进行交互。

我设置此方法的方法是删除帖子&#39;使用.set(null)从数据库中。这将返回一个promise,当它结算时,您可以使用对firebase存储进行删除调用。

*function formSubmitReply(e) {
  **var userEmail = e.values[7];**
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  sheet.getRange(lastRow, getColIndexByName("Status")).setValue("New");
  MailApp.sendEmail(userEmail,
                    "Professional Development Request",
                    "Thanks for submitting your issue. \n\nI will start " +
                    "working on it as soon as possible. \n\nMrs. Frost",
                    {name:"PD Request"});
}​
function getColIndexByName(colName) {
  var spread = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spread.getSheets()[0];
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}
function emailStatusUpdates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  **var userEmail = sheet.getRange(row, getColIndexByName("contactmail")).getValue();**
  var subject = "Status" + row;
  var body = "Your Flexible Professional Development Day Request has been\n\nStatus: " + sheet.getRange(row, getColIndexByName("Status")).getValue();

  MailApp.sendEmail(userEmail, subject, body, {Name:"Status"});
}
function onOpen() {
  var subMenus = [{name:"Send Status Email", functionName: "emailStatusUpdates"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu("Professional Development Request Menu", subMenus);
}​

Firebase存储无法以递归方式删除整个存储桶,因此您需要逐个删除每个文件。

答案 4 :(得分:2)

看看我使用过的这个功能

const app = admin.initializeApp();

export const onDeleteItem = 
 functions.firestore.document('collectionItems/{collectionID}/items/{itemID}')
   .onDelete((snap, context) => {
     const { collectionID } = context.params;
     const { itemID } = context.params
     return deleteItemImage(collectionID, itemID)
     }
 )

async function deleteItemImage(collectionID: string, itemID: string) {
  const path = `images/${collectionID}/${itemID}`;
  const bucket = app.storage().bucket();
  return bucket.file(path).delete()
    .then(function () {
      console.log(`File deleted successfully in path: ${path}`)
    })
    .catch(function (error) {
      console.log(`File NOT deleted: ${path}`)
    })
}