Nodejs-storage: How to upload an image from an external link to google cloud storage ?

Created on 6 Nov 2018  路  5Comments  路  Source: googleapis/nodejs-storage

I am using google-cloud-storage on Node.js . Trying to find a solution to programmatically download an image from an external url then upload to my default GCS bucket.

I'm using fetch API to get the image and turn it into a blob

 fetch('image.jpg').then( res => {
        var blob = res.blob()
    }) 

I've also tried this, without success

bucket.upload(urlImage, function(err, file){
            const blobStream = file.createWriteStream();
            blobStream.on('error', (err) => {
              console.log('error',err)
            });

            blobStream.on('finish', () => {
              console.log('finished with success')

              });

              blobStream.end(file);
          })

All tutorials I've found on the web deal with form upload using multer. But nothing about my case.

Now what's the right solution to upload this blob on my GCS bucket ?

storage question

Most helpful comment

This should work for your case:

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

const storage = new Storage();
const bucket = storage.bucket('my-bucket');

// Create a WritableStream from the File
const file = bucket.file('my-image.jpg');
const writeStream = file.createWriteStream();

fetch('image.jpg')
  .then(res => {
    res.body.pipe(writeStream);
  });

bucket.upload takes a local file as the first argument, uploads it and invokes the callback to give you back the File object, with the content already uploaded.

All 5 comments

This should work for your case:

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

const storage = new Storage();
const bucket = storage.bucket('my-bucket');

// Create a WritableStream from the File
const file = bucket.file('my-image.jpg');
const writeStream = file.createWriteStream();

fetch('image.jpg')
  .then(res => {
    res.body.pipe(writeStream);
  });

bucket.upload takes a local file as the first argument, uploads it and invokes the callback to give you back the File object, with the content already uploaded.

Closing for now - please re-open if there are anymore questions :)

@rabble greetings! We're sorry you're having trouble - if you're still running into issues, please open a new bug!

Independent of the issue here - we decided to remove this post because it violates our code of conduct:
https://github.com/googleapis/nodejs-storage/blob/master/CODE_OF_CONDUCT.md

Trying to achieve the same thing with axios here. Im geting the blob data of an image retrieved from an external url like this:

async getBlob(url) {
  const response = await axios.get(url, { responseType: "blob" })
  return response.data;
}

@jkwlui how would i apply the write tream in this case? I don't understand what the res.body.pipe(writeStream) is doing in your example.

Hi @dcts I know this is old but if someone else is interested the responseType: 'stream' is needed :

    const writeStream = file.createWriteStream();
    const stream = await axios({ url, method: 'GET', responseType: 'stream'})
    stream.data.pipe(writeStream)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hstemplewski picture hstemplewski  路  4Comments

Steven4294 picture Steven4294  路  7Comments

WaldoJeffers picture WaldoJeffers  路  7Comments

KKHAN1991 picture KKHAN1991  路  3Comments

muhammadbilal87 picture muhammadbilal87  路  6Comments