Google-cloud-node: uploading buffer or string

Created on 26 May 2017  路  12Comments  路  Source: googleapis/google-cloud-node

Hi All,

I can only find examples to upload a local file to cloud storage. But I would like to upload a file as a base64 string or as a buffer to google cloud storage.

Is this possible with the google-cloud/storage package?

Regards, Peter

question storage

Most helpful comment

Solved:

    const bucket = gcs.bucket('bucket_name');
    const gcsname = 'test.pdf';
    const file = bucket.file(gcsname);
    var pdfdata = "binary_pdf_file_string";
    var buff = Buffer.from(pdfdata, 'binary').toString('utf-8');

    const stream = file.createWriteStream({
        metadata: {
            contentType: 'application/pdf'
        }
    });
    stream.on('error', (err) => {
        console.log(err);
    });
    stream.on('finish', () => {
        console.log(gcsname);
    });
    stream.end(new Buffer(buff, 'base64'));

All 12 comments

Solved:

    const bucket = gcs.bucket('bucket_name');
    const gcsname = 'test.pdf';
    const file = bucket.file(gcsname);
    var pdfdata = "binary_pdf_file_string";
    var buff = Buffer.from(pdfdata, 'binary').toString('utf-8');

    const stream = file.createWriteStream({
        metadata: {
            contentType: 'application/pdf'
        }
    });
    stream.on('error', (err) => {
        console.log(err);
    });
    stream.on('finish', () => {
        console.log(gcsname);
    });
    stream.end(new Buffer(buff, 'base64'));

Yes, also file.save() for a shortcut.

Thanks for this! I find the support for google cloud platform to be somewhat weaker for node (compared to python) so its great to find someone who's also facing (and solving) similar issues!

@stephenplusplus how to add metadata & content type before file.save() ? so I able to view or access the image in public ...

getting issue 'Bucket is requester pays bucket but no user project provided' to save images in GCS.

Pulled my hair on this while writing a firebase integration test. I'll leave an example for writing a .json from memory here (using admin sdk):

const bucket = admin.storage().bucket(testEnv.storageBucket);
const file = bucket.file(mockObject.name);
const contents = JSON.stringify(mockObject, null, 2);

file.save(contents, function(err) {
  if (!err) {
    // file written
  }
});

Any way to get saved file back in the callback ? I am using file.save() which works fine but the return response is just [].

Any way to get saved file back in the callback ? I am using file.save() which works fine but the return response is just [].

According to the documentation it will return a Promise so you can use:

file.save(contents).then(function() {
  file.get().then(function (data) {
    // use data
  }
});

Just you can call uploadFile() method passing base64 encode string & get uploaded url videos file

function uploadFile(media64Str: string): Promise<any> {
  const bucket = admin.storage().bucket('gs://xxxxxxx-bb282.appspot.com')
  const imageBuffer = Buffer.from(media64Str, 'base64')
  const imageByteArray = new Uint8Array(imageBuffer);
 const file = bucket.file('testImages/video.mp4');
  return file.save(imageByteArray)
    .then(() => {
      return file.getSignedUrl({
        action: 'read',
        expires: '03-09-2500',
      })
    })
    .then((urls: any[]) => {
      const url = urls[0];
      console.log(`media url = ${url}`)
      return url
    })
    .catch((err: any) => {
      console.log(`Unable to upload encoded file ${err}`)
    })
}

Hey there. For me all those methods are not working sadly.

Im trying to upload a base64 file via the same method as @manzooralam provided (except the signed url stuff). In the firebase console ui I'm also seeing that there was an upload, but I cant access the file:

image

Any hints for that?

Greetings! The best way to get help here is to open a new issue in this repo:
https://github.com/googleapis/nodejs-storage/

Best of luck!

Hey there. For me all those methods are not working sadly.

Im trying to upload a base64 file via the same method as @manzooralam provided (except the signed url stuff). In the firebase console ui I'm also seeing that there was an upload, but I cant access the file:

image

Any hints for that?

@bayerlse solution for your problem:

change the following line

return file.save(imageByteArray)

to

return file.save(imageByteArray, {
      metadata: {
        metadata: {
          firebaseStorageDownloadTokens: REPLACE_THIS_WITH_ANY_TEXT_VALUE_EXAMPLE_UUID,
        },
      },
    });

This above will create an access token to your file but be sure you wanted it as file then could be accessed by anyone using that access token.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mik115 picture mik115  路  5Comments

ddunkin picture ddunkin  路  3Comments

positlabs picture positlabs  路  3Comments

stephenplusplus picture stephenplusplus  路  4Comments

stephenplusplus picture stephenplusplus  路  3Comments