我的Firebase Cloud Function失败,并出现以下错误:
Error: EBUSY: resource busy or locked, rmdir '/tmp/thumbs' at Error (native)
使用我的功能,我为上传的图片创建缩略图并将其保存到Firebase存储中。我构建以下代码的方式可以正常运行(每个函数调用均失败)。但是我担心,由于tmp文件夹中的旧文件不会被删除,因此我的tmp文件夹中会填满不必要的文件。
import * as functions from 'firebase-functions'
import {tmpdir} from 'os'
import {join, dirname} from 'path'
import * as sharp from 'sharp'
import * as fs from 'fs-extra'
import * as admin from "firebase-admin"
const storage = admin.storage()
/* Saves thumbnails for all userPics uploaded to Google Cloud Storage */
export const thumbnailCreator = functions.storage
.object()
.onFinalize(async object => {
const bucket = storage.bucket(object.bucket)
const filePath = object.name
const fileName = filePath.split('/').pop()
const bucketDir = dirname(filePath)
const workingDir = join(tmpdir(), 'thumbs')
/* Temporary: Creates a random number for the sourceFilePath
* because currently bucket.file(filePath).download does not seem to overwrite
* existing files and fs.rmdir(workingDir) does throw that error... */
const randomNr = Math.random().toString(36).substring(2, 15) +
Math.random().toString(36).substring(2, 15)
const tmpFilePath = join(workingDir, `source_${randomNr}.png`)
if (!object.contentType.includes('image')) {
console.log('exiting function (no image)')
return false
}
if (fileName.includes('thumb@')) {
console.log('exiting function (already a thumbnail')
return false
}
// 1. Ensure thumbnail dir exists
await fs.ensureDir(workingDir)
// 2. Download Source File
await bucket.file(filePath).download({
destination: tmpFilePath
})
// 3. Resize the images and define an array of upload promises
const sizes = [64, 128, 256, 512]
const uploadPromises = sizes.map(async size => {
const thumbName = `thumb@${size}_${fileName}`
const thumbPath = join(workingDir, thumbName)
// Resize source image
await sharp(tmpFilePath)
.rotate()
.resize(size, size)
.toFile(thumbPath)
// Upload to GCS
const file = await bucket.upload(thumbPath, {
destination: join(bucketDir, thumbName),
predefinedAcl: 'publicRead'
})
})
// 4. Run the upload operations
await Promise.all(uploadPromises)
// 5. Cleanup remove the tmp/thumbs from the filesystem
return fs.rmdir(workingDir)
// TODO: This fails every time -> also tried with fs.remove(workingDir), same issue.
})
如您所见,我的处理方式是将GUID赋予source.png的文件名,因为下次调用该函数时,执行bucket.file(filePath).download()
时不会覆盖已下载的文件。 。
但是我想清理我的tmp文件夹,但我不知道为什么该文件夹“繁忙或锁定”。有没有办法在尝试删除它之前将其解锁?
更新-DOUG解决方案
如Doug在下面回答的那样,请先删除所有文件,然后再删除文件夹。所以我最终这样做:
// 5. Cleanup remove the tmp/thumbs from the filesystem
await fs.emptyDir(workingDir)
await fs.remove(workingDir)
答案 0 :(得分:1)
我不知道这是否会导致EBUSY,但是rmdir(节点的版本和具有相同名称的unix命令行)都要求在调用目录之前将其清空。您将文件留在那里,这可能会导致rmdir失败。尝试分别删除每个生成的文件,然后再删除目录。