Firebase Web:将文件从一个位置复制到同一存储桶中的另一个位置

时间:2020-10-25 16:23:57

标签: javascript reactjs firebase google-cloud-storage firebase-storage

我有一个react应用,显示一些存储在Firebase存储器中的图像。现在,在对与之相关的数据进行了一些更改之后,我想将此图像从一个文件夹复制到同一存储桶中的另一个文件夹。我曾尝试使用@ firebase / storage来执行此操作,但是当我什至没有显式使用fs时,它就会给我TypeError: Unexpected error determining execution environment: Failed to fetch,并在TypeError: fs.createReadStream is not a function处进行一些更改。

const copyImage = async (eventId, imageUrl) => {
        const srcBucketName = 'gs://some-random.appspot.com'
        console.log("srcBucketName :", srcBucketName);
        const destBucketName = 'gs://some-random.appspot.com'
        console.log("destBucketName :", destBucketName);
        const imageExtension = imageUrl.split('?')[0].split('.')[5]
        const srcFilename = `folder1/${eventId}.${imageExtension}`
        console.log("Filename:", srcFilename);
        const destFilename = `folder2/${eventId}.${imageExtension}`
        await storage
            .bucket(srcBucketName)
            .file(srcFilename)
            .copy(storage.bucket(destBucketName).file(destFilename))
        console.log(
            `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
        )
        return 'Successful'
    }

    async copyFile (imageURL, dest = 'folder2') {
        const bucketName = 'gs://some-random.appspot.com';
        let link = imageURL.split('?')[0];
        let location = link.split(/[/]+/g);
        dest = `${ dest }/${ location[location.length - 1] }`;
        let src = location.slice(4,).join('/');
        let file = await storage.bucket(bucketName).file(src).copy(storage.bucket(bucketName).file(dest));
        return 'Transfered image'
    }

0 个答案:

没有答案