Generate a compressed file from a readable source, insert a new document, and transfer the output

The objective is to obtain an archive from the client, include a file, and transfer it to Cloud Storage without generating a temporary file. Both the client and server utilize the archiver library. The issue with the code snippet provided is that the file named file2.txt does not get included in the archive.

Client:

import archiver from "archiver";

const archive = archiver("zip", {
    zlib: { level: 9 },
});

archive.append("string cheese!", { name: "file1.txt" });

await fetch(`/archive`, {
    method: "POST",
    body: archive,
});

Server:

import archiver from "archiver";
import { Storage } from "@google-cloud/storage";
import { Router } from "express";

const router = Router();
const storage = new Storage();

router.post("/", (req, res) => {
  const archive = archiver("zip", {
    zlib: { level: 9 },
  });

  const cloudFile = storage.bucket("archives").file("archive.zip");

  req.pipe(archive, {
    end: false,
  });

  archive.pipe(cloudFile.createWriteStream());

  req.on("end", async () => {
    archive.append("string cheese!", { name: "file2.txt" });
    archive.finalize();
    archive.end();
  });
});

Answer №1

After reviewing the documentation, it seems that they utilize fs.createReadStream(file1) to interact with the file system. However, you can achieve the same result by obtaining a Buffer from the received data.

const multer  = require('multer')
const upload = multer()

router.post("/", upload.none(), function (req, res, next) {

// append a file from buffer
const buffer = req.files[0].buffer
archive.append(buffer, { name: 'file3.txt' });
})

Additionally, if you are working in a serverless environment, chances are you may not have direct access to read, write, and clean the file system. In some cases, you may have access to the /temp directory. It is advisable to quickly investigate if this applies to your situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble getting the jquery ui datetimepicker to function properly in my node.js application using ejs and express

I am currently trying to implement the functionality found at this link: Below is an excerpt of my code: <html> <head> <script src="http://trentrichardson.com/examples/timepicker/js/jquery-1.7.1.min.js"> </script> < ...

How do I preserve data within $scope upon switching views using ng-include?

Can you please take a look at this jsFiddle? http://jsfiddle.net/mystikacid/b7hqcdfk/4/ This is the template code: <div ng-app="myApp"> <div ng-controller="dataCtrl"> <div>Data : {{data}} (Value outside views)</div> < ...

Blogger's homepage URL obtained using JSON data

Please note: I don't have a background in programming. I'm making an effort to learn as much as possible. As I was reading (and later experimenting with) the solution to this query, it dawned on me that having the same information in JSON form ...

Get a URL from the JSON data returned by the Wikipedia API

How can I retrieve the image URL from a JSON response and store it in a variable? I found helpful information on the MediaWiki API help page Following this example to extract image information from a page: https://commons.wikimedia.org/w/api.php?action= ...

Diverse Range of Exports Available in React Component Library

I have been working on developing a component library consisting of multiple independent components. My objective is to enable users to easily import and use these components in their projects, similar to the following: import One from 'component-lib ...

Creating an IntersectionObserver with the Composition API: A Step-by-Step Guide

I am currently in the process of incorporating an IntersectionOberver that will update the url once the viewport transitions into a new section. I came across This thread and now endeavoring to apply it in vue 3 compositon api. The script is being integra ...

Can phantomJS be used to interact with elements in protractor by clicking on them?

While attempting to click a button using PhantomJS as my browser of choice, I encountered numerous errors. On my first try, simply clicking the button: var button = $('#protractorTest'); button.click(); This resulted in the error: Element is ...

The node.js express application is logging a large chunk of data twice when using res.on('data')

I'm currently facing a challenge in my nodeJS project related to handling asynchronous tasks. Despite multiple attempts, I haven't been able to find a solution for my specific issue. The project involves setting up a server that fetches data fro ...

Unable to retrieve the field value from the Json Object

I have a JSON object that I need to parse and display in a data table, but I'm having trouble reading the contents of the object. Here is my JavaScript function: finalGrid: function(data){ console.log("Final Grid"); var strJson = JSON.strin ...

Using JQuery to toggle a fixed div at the bottom causes all other divs to shift upwards

I'm currently working on a chat feature using node JS, and while the functionality is perfect, I've run into an issue with the CSS aspect of it. The problem arises when we have multiple tabs and clicking on just one causes all the tabs to move u ...

React app experiencing freezing due to custom asynchronous function utilisation

I am currently facing an issue with my personal project where the application freezes under certain circumstances. The initial load works fine, but when I make changes to the code causing React to re-render, the app just freezes. It seems to be related to ...

Tips for validating date input in a TextBox using JQuery on an ASP.NET platform:

Here is some code I wrote: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datetime.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <tit ...

"Utilizing SocketIO in NodeJS to create a unique game mode emission

As a new socketIO user, I am working on a website with 2 game modes. Initially, my plan was to create separate scripts for each mode, but now I am considering consolidating everything into one script. Currently, my script emits data to all connected users, ...

Issue encountered while executing `gatsby build` with Kentico Kontent

I have been developing a project that utilizes both Gatsby and Kentico Kontent. During development on my Windows 10 machine, I have had success running gatsby develop and npm run develop. Typically, the output looks like this: $ npm run develop success o ...

Obtaining information from the parent table in SequelizeHow to access data from

I am working with Sequelize dependency I need to retrieve data from the parent table. In my database, there is a table called tblroute which has an association with routedetails. The foreign key in routedetails corresponds to the id of tblroute. router.g ...

The file was mistakenly interpreted as a document but was actually transferred with the MIME type text/csv from "http://localhost:3000/post"

Attempting to initiate a download of the file. res.attachment('./public/uploads/name-1540993539785.csv') The file is downloaded, however its content only shows "uploads/name-1540995323357.csv" instead of the actual data. An error message appe ...

How would you execute a simple protractor test with the provided file hierarchy?

I have attempted to follow a tutorial located HERE for running protractor tests. Despite my efforts, I am facing an issue where the tests do not seem to run when I attempt to start them. While my webdriver-manager is functioning correctly, nothing seems to ...

Mapping a list with sections can easily be achieved by breaking down the elements

I'm facing an issue with listing array data under sections using .map in React. I know how to use .map to list the entire array, but struggling to list each item under its respective section. Currently, I have to use .map separately for each section. ...

Set up an event listener for when geolocation permission is approved

My Setup: I've written some basic code snippet below: const onSuccess = () => { console.log('success'); } const onError = () => { console.log('error'); } navigator.geolocation.getCurrentPosition(onSuccess, onError) ...

Interconnected realms communication

I'm currently in the process of developing a Facebook iframe app. At one point, I initiate a friends dialog from Facebook and embed an HTML button to add some customized functionality for my app. dialog = FB.ui({ method:'fbml.di ...