How do I upload a base64 encoded image (string) directly to a Google Cloud Storage bucket using Node.js?

前端 未结 6 1742
野性不改
野性不改 2020-11-30 04:51

Currently, I am using the @google-cloud/storage NPM package to upload a file directly to a Google Cloud Storage bucket. This requires some trickery as I only have the image\

6条回答
  •  再見小時候
    2020-11-30 05:11

    I was able to get the base64 string over to my Cloud Storage bucket with just one line of code.

    var decodedImage = new Buffer(poster64, 'base64');
            
    
    // Store Poster to storage
    let posterFile = await client.file(decodedImage, `poster_${path}.jpeg`, { path: 'submissions/dev/', isBuffer: true, raw: true });
    let posterUpload = await client.upload(posterFile, { metadata: { cacheControl: 'max-age=604800' }, public: true, overwrite: true });
    let permalink = posterUpload.permalink
    

    Something to be aware of is that if you are inside of a Nodejs environment you wont be able to use atob().

    The top answer of this post showed me the errors of my ways! NodeJS base64 image encoding/decoding not quite working

提交回复
热议问题