Fox5150
Fox5150

Reputation: 2199

Upload a file, from an URL to Google Storage, in a cloud function

I try to find a way to upload a PDF file, generated by a php/MySQL server to my Google Storage bucket. The URL is simple : www.my_domain.com/file.pdf . I tried with the code below , but I'm having some issues to make it work. The error is : path (fs.createWriteStream(destination)) must be a string or Buffer. Thanks in advance for your help !

const http = require('http');
const fs = require('fs');
const {Storage} = require('@google-cloud/storage')
const gcs = new Storage({
    keyFilename: 'my_keyfile.json'
})
const bucket = gcs.bucket('my_bucket.appspot.com');
const destination = bucket.file('file.pdf');
var theURL = 'https://www.my_domain.com/file.pdf';

var download = function() {

    var file = fs.createWriteStream(destination);
    var request = http.get(theURL, function(response) {
        response.pipe(file);

        file.on('finish', function() {
            console.log("File uploaded to Storage")
            file.close();
        });
    });

}

Upvotes: 1

Views: 2477

Answers (3)

Wishmaster
Wishmaster

Reputation: 1172

I can suggest something like a hybrid from previous answers, that worked fine for me:

const {Storage} = require('@google-cloud/storage')
const https = require('https')
const gcs = new Storage({
    keyFilename: 'KEY.json'
})
const bucket = gcs.bucket('BUCKET_NAME')
const filePath = 'FILE_NAME'
const fileUrl = 'FILE_URL_TO_DOWNLOAD'

exports.fetchPrices = (req, res) => {
    https.get(fileUrl, {
        headers: {
            'Authorization': 'Bearer ' + TOKEN
        }
    }, resp => {
    
        const bucketFile = bucket.file(filePath);
        const fileWriteStream = bucketFile.createWriteStream();
        resp.pipe(fileWriteStream);
    
        fileWriteStream.on('finish', function () {
            res.send('Uploaded successfully')
        });
    });
}

Upvotes: 0

MentallyRetired
MentallyRetired

Reputation: 31

firebase-admin, as of v7.0.0, uses google-cloud/storage v2.3.0, which can no longer accept file URLs on bucket.upload.

I figured I would share my solution as well.

const rq = require('request');

// filePath = File location on google storage bucket
// fileUrl = URL of the remote file

const bucketFile = bucket.file(filePath);
const fileWriteStream = bucketFile.createWriteStream();
let rqPipe = rq(fileUrl).pipe(fileWriteStream);

// And if you want the file to be publicly readable
rqPipe.on('finish', async () => {
  await bucketFile.makePublic();
});

Upvotes: 3

Fox5150
Fox5150

Reputation: 2199

I finally found a solution :

const http = require('http');
const fs = require('fs');
const {Storage} = require('@google-cloud/storage')
const gcs = new Storage({
    keyFilename: 'my_keyfile.json'
})
const bucket = gcs.bucket('my_bucket.appspot.com');

const destination = os.tmpdir() + "/file.pdf";
const destinationStorage = path.join(os.tmpdir(), "file.pdf");

var theURL = 'https://www.my_domain.com/file.pdf';

var download = function () {

    var request = http.get(theURL, function (response) {
        if (response.statusCode === 200) {
            var file = fs.createWriteStream(destination);
            response.pipe(file);
            file.on('finish', function () {

                console.log('Pipe OK');

                bucket.upload(destinationStorage, {
                    destination: "file.pdf"
                }, (err, file) => {

                    console.log('File OK on Storage');
                });
                file.close();
            });
        }
    });

}

Upvotes: 3

Related Questions