Upload Buffer Directly to Google Cloud Storage

Viewed 880

I am fetching PDF document as arraybuffer from third party vendor. I want to upload that arraybuffer directly to Google Storage without creating a local copy of the file.

const axios = require('axios');
const google_storage = new Storage({ keyFilename: 'path_to_keyFilename' });

const upload_file = async (req, res, next) => {
  try {
    let fetching_result = await axios({
      method: 'get',
      url: 'PDF_document_url',
      responseType: 'arraybuffer'
    });
    let upload_result = await google_storage.bucket('bucket_name').upload(fetching_result.data);
    return res.status(200).json(upload_result.data);
  } catch (error) {
    console.log('ERROR: ', error);
    return res.status(400).json(error);
  }
}

I am using the @google-cloud/storage package to do upload a file. But when I pass the arraybuffer to it, I got the following error:

ERROR: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received <Buffer 25 50 44 46 2d 31 2e 35 0a 25 46 69 6c ...

I also tried to convert arraybuffer to Uint8Array and pass that, but then I receive the following error:

ERROR: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received Uint8Array(116107) [ 37, 80, 68, 70, 45, 49, 46, 53, 10, 37, 216, 229 ...

Does anyone know what's happening here and how to solve this?

1 Answers

bucket.upload() expects a file name as first parameter, not the actual file you are trying to save.

File.createWriteStream() should work better:

const myData: Uint8Array = *something*;

const storage = new Storage();
const bucket = storage.bucket("myBucket");

const blob = bucket.file("myFile");
const blobStream = blob.createWriteStream({
    resumable: false,
});

blobStream.end(myData);
Related