13

I am trying to use a google cloud function in Node.js to download a file for wordpress repo then send the file into a google cloud bucket. I have the wordpress file downloading but it fails to write to the google bucket.

function writeToBucket(jsonObject){

/*
 *  Google API authentication
 */

var gcs = require('@google-cloud/storage')({
         projectId: 'wp-media-cdn',
         keyFilename: 'wp-media-cdn-d9d7c61bfad9.json'
});

/*
 *  rename image file with image size, format: size X size imgName
 */

var pluginUrl = "https://downloads.wordpress.org/plugin/bbpress.2.5.14.zip";
    newPluginName = "bbpress";

/*
 *  Read image into stream, upload image to bucket
 */

var request = require('request');
var fs = require('fs'); //used for createWriteString()

var myBucket = gcs.bucket('test_buckyy'); //PUT BUCKET NAME HERE
var file = myBucket.file(nnewPluginName);

// file.exists() returns true if file already in bucket, then returns file url, exits function
if(file.exists()){
    return 'https://storage.googleapis.com/${test_buckyy}/${file}';
}

//pipes image data into fileStream
var fileStream = myBucket.file(newImageName).createWriteStream();
request(imgUrl).pipe(fileStream)
    .on('error', function(err) {
        console.log('upload failed');
    })
    .on('finish', function() {
        console.log('file uploaded');
    });
/*
 *  return image url
 *  use getSignedUrl
 */


    return 'https://storage.googleapis.com/${test_buckyy}/${file}';

}
3
  • Please, post errors messages. Look into log information. Nov 18, 2017 at 20:22
  • Hello, did you ever find a solution? If so, could you add an answer?
    – ThaDon
    Jan 8, 2019 at 15:40
  • 1
    The only problem I can see is a typo of newPluginName here: var file = myBucket.file(nnewPluginName);
    – ThaDon
    Jan 8, 2019 at 15:42

2 Answers 2

11

I just replicated your use case scenario and I successfully downloaded the file into the temporary folder of a Cloud Function and from there I copied this file into a bucket.

In order to achieve this, I downloaded the file using createWriteStream into the /tmp folder since is the only folder where we can store files in a Cloud Function, as stated in the Cloud Functions Execution Environment documentation.

After that, I just copied the file to a bucket following this Cloud Storage Uploading Objects documentation.

You can take a look of my sample function

Index.js

const {Storage} = require('@google-cloud/storage');

exports.writeToBucket = (req, res) => {
const http = require('http');
const fs = require('fs');

const file = fs.createWriteStream("/tmp/yourfile.jpg");
const request = http.get("YOUR_URL_TO_DOWNLOAD_A_FILE", function(response) {
  response.pipe(file);
});

console.log('file downloaded');

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();
const bucketName = 'YOUR_BUCKET_NAME';
const filename = '/tmp/yourfile.jpg';

// Uploads a local file to the bucket
storage.bucket(bucketName).upload(filename, {
  gzip: true,
  metadata: {
    cacheControl: 'no-cache',
  },
});

res.status(200).send(`${filename} uploaded to ${bucketName}.`);

};


package.json

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
        "@google-cloud/storage": "^3.0.3"
    }
}
7

Using Chris32's answer I've created a similar version but avoiding the download of the image to the tmp folder. Hope it's useful !

'use strict';

const http = require('http');
const {Storage} = require('@google-cloud/storage');

exports.http = (request, response) => {
  const imageUrl = request.body.url;
  const fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);

  const storage = new Storage({keyFilename: "keyfile.json"});
  const bucket = storage.bucket('MY_BUCKET_NAME');
  const file = bucket.file(fileName);

  console.log('Uploading image')
  http.get(imageUrl, function(res) {
    res.pipe(
      file.createWriteStream({
        resumable: false,
        public: true,
        metadata: {
          contentType: res.headers["content-type"]
        }
      })
    );
  });

  console.log('Image uploaded')
  response.status(201).send('Image successful uploaded!');
};

exports.event = (event, callback) => {
  callback();
};
6
  • Just what I needed! I was having some image encoding issues when downloading and uploading Mar 11, 2020 at 15:22
  • @Maximilliano De Lorenzo what's the http package you are using? it doesn't exist on npm: npmjs.com/package/http
    – mesqueeb
    Jun 30, 2020 at 2:16
  • Could we swap out http with a package like got from sindresorhus ?
    – mesqueeb
    Jun 30, 2020 at 2:17
  • PERFECT! Thanks. I've been looking for this during hours.
    – Broda Noel
    May 10, 2022 at 21:09
  • 1
    @cryanbhu it creates a file stream directly to the bucket, so as it downloads the bytes it uploads them into the bucket Jun 27, 2023 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.