【问题标题】:How to copy an image over to another reference path in Firebase Storage?如何将图像复制到 Firebase 存储中的另一个参考路径?
【发布时间】:2021-04-18 15:30:42
【问题描述】:

在使用 react-native-firebase (v5) 的 react-native 应用程序 (v61.5) 中,我的图像路径如下所示:

/message-attachments/{session-id}

我已安排在一段时间后删除消息,在此期间,任何已删除的消息会话也将被删除,并且附件也存储在此会话 ID 参考中。

问题是,如果会话被存档,则需要将该图像从该消息附件引用移动/复制到/{user}/archived-attachments/{session-id},以便保留预定的消息删除。

firebase 存储用于该项目,我试图从复制的角度来实现这一点,因此不需要完全重新设计参考路径。根据 v5 react-native-firebase 文档,putFile() 只能采用 device 文件路径。但由于图像的来源是 firebase 中的另一个位置,因此似乎无法正常工作。我能想到的唯一可能符合这些要求的方法是使用.downloadFile('${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg'),并且在同一个redux 操作中使用putFile('${firebase.storage.Native.DOCUMENT_DIRECTORY_PATH}/ok.jpeg') 将其放到存储中的正确引用路径。

这似乎很hacky,我更喜欢避免将图像下载到用户设备,还有其他方法吗?

【问题讨论】:

    标签: firebase react-native firebase-storage react-native-firebase


    【解决方案1】:

    下载并重新上传文件是不好的。从理论上讲,文件可能会变大,下载几兆字节的文件然后重新上传是一个不好的解决方案。

    相反,您可以创建一个云函数来执行移动(复制/删除)操作。

    Firebase 函数执行文件移动(复制/删除)

    const { Storage } = require('@google-cloud/storage');
    
    exports.moveFile = functions.region(region).https.onCall(async (data, context) => {
      // Simple mechanism to secure the function call to logged in users.
      // There are other methods to secure cloud functions using IAM and
      // you should always specify rules to the storage buckets to allow users
      // view manage just their own files.
    
      const { auth } = context || {};
      const { uid } = auth || {};
    
      if (!uid) {
        throw 'Unauthenticated';
      }
    
      const srcBucketName = 'src-bucket';
      const destBucketName = 'dest-bucket'; // Can be the same as source bucket
      // Retrieve the file that the RN app passed to archive.
      // We should also check if it's a valid file.
      const { archiveFile } = data;
      const srcFilename = `path/to/currentFile/${archiveFile}`;
      const destFilename = `${uid}/archived-attachments/{session-id}/${srcFilename}`;
    
      const storage = new Storage();
      storage
        .bucket(srcBucketName)
        .file(srcFilename)
        .copy(storage.bucket(destBucketName).file(destFilename))
        .then(() => {
          console.log(
            `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
          );
          storage
            .bucket(srcBucketName)
            .delete()
            .then(() => {
              // Done moving file
              return res.status(200);
            })
            .catch(err => {
              console.error('DELETE ERROR:', err);
            });
        })
        .catch(err => {
          console.error('COPY ERROR:', err);
        });
    });
    

    使用@react-native-firebase/functions在React native中调用函数

    import firebase from '@react-native-firebase/app';
    import '@react-native-firebase/functions';
    
    firebase.app().functions(region).httpsCallable('moveFile')({
      archiveFile: 'some-file.txt'
    }).then(() => {
      // Done moving file
    }).catch(error => {
      // Handle error
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 2017-03-08
      • 1970-01-01
      • 2010-12-31
      • 2020-12-09
      • 2019-01-04
      • 1970-01-01
      相关资源
      最近更新 更多