Converting a file buffer to upload to Google Cloud Storage

I have been facing an issue while attempting to upload a file to Google Cloud using a buffer and the save function. The problem I am encountering is that even though the files are uploaded to Google Cloud, they are not in the correct format. When I try to download the files, they appear as .txt files of the buffer content.

I could really use some guidance on how to resolve this issue. Any assistance would be highly appreciated!

upload.ts

const upload = async (fileBuffer: any, filePath: string, fileName: string) => {
    const file = bucket.file(path.join(filePath, fileName));
    file.save(fileBuffer, (err: any) => {
        if (err) {
            console.log(err);
        }
    });
}

storage.ts

imageSender: async(req: Request, res: Response) => {
    try {
        const buffer = "";

        await upload(buffer, "folder", "file");

        res.status(200).send("The file was successfully uploaded.");

    } catch (err) {
        res.status(500).send(httpError(500, "Could not upload the file."));
    }
},

Answer №1

I realized that the data I was handling was a base64 string, not a buffer. To resolve this, I simply included

const buffer = Buffer.from(base64, 'base64');
in my code. The solution worked flawlessly from then on!

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

(node:19502) UnresolvedPromiseRejectionNotification: Promise rejection not handled (rejection id: 1): TypeError: Unable to access property 'indexOf' of undefined

I successfully integrated cucumber with nightwatch.js. Here is a snippet of my package.json file: { "name": "learning-nightwatch", "version": "1.0.0", "description": "learning nightwatch", "license": "UNLICENSED", "scripts": { "nightwatch": ...

Is it possible to iterate over a non-reactive array in a Vue template without having to store it in a data property or computed property?

Presented below is a static array data: const colors = ["Red", "Blue", "Green"]; To display these values in my markup, I can use the following method in React: import React from "react"; // Static array of colors const colors = ["Red", "Blue", "Green"] ...

The Ionic framework is showing an error that says "'LoginCtrl' is not defined as a function."

I encountered an issue while attempting to set up simple navigation in my ionic application. The error message I received was: "Argument 'LoginCtrl' is not a function, got undefined in the Ionic. What could be causing this problem?" Here is a sn ...

Observable doesn't respond to lazy loaded module subscriptions

I am trying to understand why my lazy loaded module, which loads the test component, does not allow the test component to subscribe to an observable injected by a test service. index.ts export { TestComponent } from './test.component'; export { ...

Encountering issues with returning values correctly when using module.exports in Node.js

function userLogin(username, password) { var status; var userid = username; User.findOne({ 'username': [userid], 'password': [password] }, function(err, user) { if (!user) { console.lo ...

Converting server scripts into ES6 with the integration of socket.io

We're diving into socket.io and express as beginners, and we've been following this tutorial to grasp socket.io: Now, we're trying to convert this line of code (using the old style): const index = require("./routes/index").defaul ...

Utilizing Nested Click Events in jQuery to Enhance User Interaction

I'm really struggling with this and can't seem to find a solution anywhere. I want to capture data when button A is clicked, and then submit that data via AJAX when button B is clicked. Here's what I've been considering: $(document).o ...

Using the class for jQuery validation as opposed to the name attribute

I am looking to implement form validation using the jquery validate plugin, but I am facing an issue with using the 'name' attribute in the html since it is also used by the server application. Specifically, I want to restrict the number of check ...

Can a single shield protect every part of an Angular application?

I have configured my application in a way where most components are protected, but the main page "/" is still accessible to users. I am looking for a solution that would automatically redirect unauthenticated users to "/login" without having to make every ...

Loading slides in bxSlider using ajax

Is there a method to dynamically insert a slide into bxSlider via ajax without affecting its smooth transition? I am looking to initially display the contents of only one slide upon page load, and then have subsequent slides loaded when the next or previo ...

In React, what is the correct term for "selection boxes" when choosing multiple items in Finder?

I'm in search of a React component that allows me to select multiple elements on the screen by clicking, holding, and forming a square around them. I've tried searching for terms like 'selection box' and 'React select elements,&apo ...

Issues with retrieving a result object from a MySQL query in NodeJS

I've been working on creating a logging system in NodeJS with a MySQL database. To establish the connection, I use the following code: const con = mysql.createConnection({ host : 'localhost', user : 'dbuser', passwor ...

Error 404 in NodeJS: Page Not Found

I recently started working with NodeJS to develop an ecommerce application. I have a ready-made design and all the front-end components built using AngularJS code. Everything seems to work fine - when clicking on any menu, the page content changes along wi ...

Determining the pageY value of a div element with overflow-y styling

Currently, I have implemented a script that tracks the mouse's position upon hover. My goal is to integrate this script within a div that has overflow-y: scroll The script currently utilizes pageY which identifies the position relative to the windo ...

The session is failing to update following a redirect

I'm encountering some unusual challenges while attempting to develop a login system. Here is the code snippet: const { email, password } = req.body; if(!email ||!password) return res.status(400).send("Missing required fields"); let user = ...

Is it possible for index.html to reference a .js file in an Angular.js 1.x version?

Authorized users are allowed to access the menu items. While User A requires the menu_1.js file, User B does not need it and should not have access to it. I am trying to determine how to include required js files in index.html for an angular.js version 1 ...

What are the steps to refreshing a table using AJAX?

Struggling to update a table from my database, I have been following a PHP guide but can't get it to work. In a separate file, the data is retrieved and displayed in a table. I am attempting to use JavaScript to refresh this file. This code snippet ...

Retrieving JSON information from a lone field in a MySQL table

I am currently working with a PHP REST Web service that is being accessed from JavaScript. Everything works fine when I run a MySQL SELECT statement to select text fields and then use json_encode to convert the returned array into JSON objects. However ...

Preventing duplicate namespace declarations in TypeScript

Let's say I have a variety of objects with the following data structure: { namespace: 'first'|'second'|'third' } Now, I need to include another object with the same data structure, but its namespace cannot be assigned ...

Steps for Exporting Objects to CSV with a Different Delimiter

Currently, I am working on a project that involves using MySQL and Node/Express to generate a CSV file. The output is successful, but the default delimiter being used is a comma. My goal is to change the delimiter to ^^ instead. I believe there may be a p ...