google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

uploading buffer or string

Open p3sn opened this issue 9 years ago • 14 comments

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

p3sn avatar May 26 '17 10:05 p3sn

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'));

p3sn avatar May 26 '17 12:05 p3sn

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

stephenplusplus avatar May 26 '17 12:05 stephenplusplus

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!

ngwanzhen avatar Mar 20 '18 13:03 ngwanzhen

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

drizzersilverberg avatar Jun 08 '18 07:06 drizzersilverberg

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

amit0shakya avatar Jul 23 '18 15:07 amit0shakya

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
  }
});

ecklf avatar Feb 21 '19 15:02 ecklf

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

codekeyz avatar Mar 12 '19 01:03 codekeyz

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
  }
});

ecklf avatar Mar 12 '19 07:03 ecklf

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}`)
    })
}

manzooralam avatar Apr 02 '20 12:04 manzooralam

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 avatar Mar 17 '21 22:03 bayerlse

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!

JustinBeckwith avatar Mar 17 '21 22:03 JustinBeckwith

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.

aamirpinger avatar May 19 '21 07:05 aamirpinger

Hello. Help me Please! I'm trying to use the methods mentioned by you and I get this result:

image


Sending base64 to backEnd:

const reader = new FileReader()
reader.readAsDataURL(image)
reader.onload = (res) => {
  BackEnd('arquivos/setFile', {
    image: {
      name: image.name,
      type: image.type,
      size: image.size
    },
    base64: res.target.result,
  })
}

Sending base64 to Firebase:

const bucket = admin.storage().bucket()
const imageBuffer = Buffer.from(base64, 'base64')
const imageByteArray = new Uint8Array(imageBuffer)
const file = bucket.file(`arquivos/${hash}.png`)

file.save(imageByteArray, {
  metadata: {
    metadata: {
      firebaseStorageDownloadTokens: hash
    }
  }
}).then(() => {
  console.log('Arquivo salvo com sucesso')
  return res.status(200).json({})
})

Flaviano-Rodrigues avatar Jan 18 '23 01:01 Flaviano-Rodrigues

Make sure to remove the URI prefix from the base64:

function removeURIPrefix(imageBase64: string) { return imageBase64.includes(",") ? imageBase64.split(",")[1] : imageBase64; }

The image will remain broken in Firebase Storage but works when fetching it in your app via path.

mqrten avatar Oct 08 '24 17:10 mqrten