【问题标题】:Firebase storage item not found using getFiles or deleteFiles使用 getFiles 或 deleteFiles 找不到 Firebase 存储项
【发布时间】:2022-01-05 02:59:27
【问题描述】:

我正在使用 Firebase 函数在删除 Firestore 项目时删除文件,每个 Firestore 条目有 2 个文件:image.jpg 和 image_thumb.jpg

我正在尝试使用存储桶前缀删除这两个文件,但是找不到文件,我尝试在删除它们之前使用 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
>      }
>    ]
>  ]

【问题讨论】:

    标签: javascript firebase google-cloud-firestore google-cloud-functions google-cloud-storage


    【解决方案1】:

    如果我删除 ${photoId} 引用并仅查找文件夹,我 得到正确的结果

    这实际上是预期的行为,如Cloud Storage documentation 所示(查看“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
       */
    

    【讨论】:

    • 嘿@MiguelStevens,你有时间看一下提议的答案吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2016-11-23
    • 2018-11-20
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多