Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uploading buffer or string #2334

Closed
p3sn opened this issue May 26, 2017 · 14 comments
Closed

uploading buffer or string #2334

p3sn opened this issue May 26, 2017 · 14 comments
Assignees
Labels
api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue.

Comments

@p3sn
Copy link

p3sn commented May 26, 2017

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
Copy link
Author

p3sn commented May 26, 2017

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

@stephenplusplus
Copy link
Contributor

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

@stephenplusplus stephenplusplus added api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue. labels May 26, 2017
@ngwanzhen
Copy link

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!

@drizzersilverberg
Copy link

drizzersilverberg commented Jun 8, 2018

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

@amit0shakya
Copy link

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

@ecklf
Copy link

ecklf commented Feb 21, 2019

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

@codekeyz
Copy link

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

@ecklf
Copy link

ecklf commented Mar 12, 2019

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

@manzooralam
Copy link

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

@bayerlse
Copy link

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?

@JustinBeckwith
Copy link
Contributor

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!

@aamirpinger
Copy link

aamirpinger commented May 19, 2021

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.

@Flaviano-Rodrigues
Copy link

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({})
})

@mqrten
Copy link

mqrten commented Oct 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the Cloud Storage API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests