我正在使用Firebase函数删除文件,当删除一个firestore项目时,每个firestore条目都有两个文件:image.jpg和image_thumb.jpg
我试图使用bucket前缀删除这两个文件,但没有找到文件,我尝试在删除之前使用getFiles方法查看项目,但也没有返回任何结果。
const bucket = admin.storage().bucket()
const not_working = await bucket.getFiles({prefix: `photos/${userId}/${photoId}`})
const not_working = await bucket.deleteFiles({prefix: `photos/${userId}/${photoId}`})
如果我删除${photoId}引用并只查找文件夹,我会得到正确的结果
const bucket = admin.storage().bucket()
const working = await bucket.getFiles({prefix: `photos/${userId}/`})
这将返回以下数据
[
> [
> File {
> _events: [Object: null prototype] {},
> _eventsCount: 0,
> _maxListeners: undefined,
> metadata: [Object],
> baseUrl: '/o',
> parent: [Bucket],
> id: 'photos%2FkLxGzah8zc7Vbrl0c3iLic8tkqH2%2Fcad2ce98-dfa7-4611-9a88-913328ab4590.png',
> createMethod: undefined,
> methods: [Object],
> interceptors: [],
> pollIntervalMs: undefined,
> create: undefined,
> bucket: [Bucket],
> storage: [Storage],
> kmsKeyName: undefined,
> userProject: undefined,
> name: 'photos/kLxGzah8zc7Vbrl0c3iLic8tkqH2/cad2ce98-dfa7-4611-9a88-913328ab4590.png',
> acl: [Acl],
> instanceRetryValue: true,
> instancePreconditionOpts: undefined,
> [Symbol(kCapture)]: false
> },
> File {
> _events: [Object: null prototype] {},
> _eventsCount: 0,
> _maxListeners: undefined,
> metadata: [Object],
> baseUrl: '/o',
> parent: [Bucket],
> id: 'photos%2FkLxGzah8zc7Vbrl0c3iLic8tkqH2%2Fcad2ce98-dfa7-4611-9a88-913328ab4590_thumb.png',
> createMethod: undefined,
> methods: [Object],
> interceptors: [],
> pollIntervalMs: undefined,
> create: undefined,
> bucket: [Bucket],
> storage: [Storage],
> kmsKeyName: undefined,
> userProject: undefined,
> name: 'photos/kLxGzah8zc7Vbrl0c3iLic8tkqH2/cad2ce98-dfa7-4611-9a88-913328ab4590_thumb.png',
> acl: [Acl],
> instanceRetryValue: true,
> instancePreconditionOpts: undefined,
> [Symbol(kCapture)]: false
> }
> ]
> ]
如果我删除
${photoId}
引用并只查找文件夹获得正确的结果
这实际上是预期的行为,如云存储文档中所示(查看"Node.js"选项卡(:不应在prefix
值中包括文件名的最后一部分(即最后一个/
之后的部分,或者,如果我们使用目录树范式,则不应包括父目录的文件名本身(。
代码示例包含以下解释:
/**
* This can be used to list all blobs in a "folder", e.g. "public/".
*
* The delimiter argument can be used to restrict the results to only the
* "files" in the given "folder". Without the delimiter, the entire tree under
* the prefix is returned. For example, given these blobs:
*
* /a/1.txt
* /a/b/2.txt
*
* If you just specify prefix = 'a/', you'll get back:
*
* /a/1.txt
* /a/b/2.txt
*
* However, if you specify prefix='a/' and delimiter='/', you'll get back:
*
* /a/1.txt
*/