Skip to content

Commit 523d92d

Browse files
fix(taskqueues-backup-images): Use buffer to save image
This commit fixes a CI error in the taskqueues-backup-images function by reading the image into a buffer and then writing the buffer to the file. This avoids the streaming type issues between web streams and Node.js streams. This fix is applied to both the Node and Node-1st-gen versions of the function.
1 parent 8027b13 commit 523d92d

File tree

2 files changed

+7
-12
lines changed
  • Node-1st-gen/quickstarts/taskqueues-backup-images/functions
  • Node/taskqueues-backup-images/functions

2 files changed

+7
-12
lines changed

Node-1st-gen/quickstarts/taskqueues-backup-images/functions/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
const path = require("path");
1818
const functions = require('firebase-functions/v1');
1919
const {initializeApp} = require("firebase-admin/app");
20-
const {Readable} = require("stream");
21-
const {pipeline} = require("stream/promises");
2220
const {getFunctions} = require("firebase-admin/functions");
2321
const {getStorage} = require("firebase-admin/storage");
2422
const logger = functions.logger;
@@ -81,14 +79,13 @@ exports.backupApod = functions
8179
logger.info(`Fetched ${picUrl} from NASA API for date ${date}.`);
8280

8381
const picResp = await fetch(picUrl);
82+
const imageBuffer = await picResp.arrayBuffer();
83+
const buffer = Buffer.from(imageBuffer);
8484
const dest = getStorage()
8585
.bucket(BACKUP_BUCKET)
8686
.file(`apod/${date}${path.extname(picUrl)}`);
8787
try {
88-
await pipeline(
89-
Readable.fromWeb(picResp.body),
90-
dest.createWriteStream()
91-
);
88+
await dest.save(buffer);
9289
} catch (err) {
9390
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
9491
throw new HttpsError("internal", "Uh-oh. Something broke.");

Node/taskqueues-backup-images/functions/index.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ const path = require("path");
2626
const {initializeApp} = require("firebase-admin/app");
2727
const {getStorage} = require("firebase-admin/storage");
2828
const {GoogleAuth} = require("google-auth-library");
29-
const {Readable} = require("stream");
30-
const {pipeline} = require("stream/promises");
3129
// [END imports]
3230
initializeApp();
3331

@@ -86,14 +84,14 @@ exports.backupapod = onTaskDispatched(
8684
logger.info(`Fetched ${picUrl} from NASA API for date ${date}.`);
8785

8886
const picResp = await fetch(picUrl);
87+
const picResp = await fetch(picUrl);
88+
const imageBuffer = await picResp.arrayBuffer();
89+
const buffer = Buffer.from(imageBuffer);
8990
const dest = getStorage()
9091
.bucket(BACKUP_BUCKET)
9192
.file(`apod/${date}${path.extname(picUrl)}`);
9293
try {
93-
await pipeline(
94-
Readable.fromWeb(picResp.body),
95-
dest.createWriteStream()
96-
);
94+
await dest.save(buffer);
9795
} catch (err) {
9896
logger.error(`Failed to upload ${picUrl} to ${dest.name}`, err);
9997
throw new HttpsError("internal", "Uh-oh. Something broke.");

0 commit comments

Comments
 (0)