I'm using a Firebase function to delete files when a firestore item is deleted, each firestore entry has 2 files: image.jpg and image_thumb.jpg
I'm trying to remove both files using the bucket prefix, however the files are not found, I've tried using the getFiles method to see the items before deleting them, but this as well returns no results.
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}`})
If I remove the ${photoId} reference and only look for the folder, I get a correct result
const bucket = admin.storage().bucket()
const working = await bucket.getFiles({prefix: `photos/${userId}/`})
This returns the following data
[
> [
> 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
> }
> ]
> ]
If I remove the
${photoId}reference and only look for the folder, I get a correct result
This is actually the intended behaviour, as shown in the Cloud Storage documentation (look at the "Node.js" tab): you should not include, in the prefix value, the last part of the file name (i.e. the part after the last /, or, if we use the directories tree paradigm, the file name itself, without the parent directories).
The code example contains these explanations:
/**
* 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
*/