我正在嘗試將圖像上傳到Firebase,然后生成2個(gè)縮略圖。我能夠做到這一點(diǎn)沒有問題。我目前的障礙是,當(dāng)我將URL寫入實(shí)時(shí)數(shù)據(jù)庫時(shí),我總是得到與初始上傳相同的URL。例如:第一次上傳時(shí),我上傳的圖像帶有該圖像的兩個(gè)適當(dāng)?shù)目s略圖第2次上傳我得到的上傳的圖像帶有前兩個(gè)縮略圖(第一個(gè)圖像)第三次上傳時(shí),我得到的上傳的圖片帶有第一個(gè)圖片的縮略圖... ...這將繼續(xù)復(fù)制第一個(gè)上傳的網(wǎng)址在我的存儲(chǔ)中,正在生成正確的縮略圖,但是URL始終是第一次上傳嗎?我不知道這是否是getSignedUrl()的問題,真的不知道這里發(fā)生了什么。這是我的云函數(shù): export const generateThumbs = functions.storage .object() .onFinalize(async object => { const bucket = gcs.bucket(object.bucket); // The Storage object. // console.log(object); console.log(object.name); const filePath = object.name; // File path in the bucket. const fileName = filePath.split('/').pop(); const bucketDir = dirname(filePath); const workingDir = join(tmpdir(), 'thumbs'); const tmpFilePath = join(workingDir, 'source.png'); if (fileName.includes('thumb@') || !object.contentType.includes('image')) { console.log('exiting function'); return false; } // 1. ensure thumbnail dir exists await fs.ensureDir(workingDir); // 2. Download Sounrce fileName await bucket.file(filePath).download({ destination: tmpFilePath }); //3. resize the images and define an array of upload promises const sizes = [64, 256]; const uploadPromises = sizes.map(async size => { const thumbName = `thumb@${size}_${fileName}`; const thumbPath = join(workingDir, thumbName); //Resize source image await sharp(tmpFilePath) .resize(size, size) .toFile(thumbPath); //upload to gcs return bucket.upload(thumbPath, { destination: join(bucketDir, thumbName), metadata: { contentType: 'image/jpeg' } })
Firebase云功能存儲(chǔ)可先觸發(fā)第一個(gè)縮略圖URL,然后再觸發(fā)與第一個(gè)相同的縮略圖URL
千萬里不及你
2021-04-12 13:14:40