0

I have a small assignment where I will have a URL to a document or a file like google drive link or dropbox link.

I have to use this link to store that file or doc in firebase using nodejs. How should i start?

Little head's up might help. What should i use? Please help I'm stuck here.

Dennis Alund
  • 2,916
  • 1
  • 13
  • 34
Balkrushna
  • 41
  • 2
  • 9

1 Answers1

3

The documentation for using the admin SDK is mostly covered in GCP documentation.

Here's a snippet of code that shows how you could upload a image directly to Cloud Storage if you have a URL for it. Any public link works, whether it's shared from Dropbox or somewhere else on the internet.

Edit 2020-06-01 The option to upload directly from URL was dropped in v2.0 of the SDK (4 September 2018): https://github.com/googleapis/nodejs-storage/releases/tag/v2.0.0

const fileUrl = 'https://www.dropbox.com/some/file/download/link.jpg';
const opts = {
  destination: 'path/to/file.jpg',
  metadata: {
    contentType: 'image/jpeg'
  }
};

firebase.storage().bucket().upload(fileUrl, opts);

This example is using the default bucket in your application and the opts object provides file upload options for the API call.

  • destination is the path that your file will be uploaded to in Google Cloud Storage
  • metadata should describe the file that you're uploading (see more examples here)
    • contentType is the file MIME type that you are uploading
Dennis Alund
  • 2,916
  • 1
  • 13
  • 34
  • Any idea why when I'm running it from my local `firebase serve` is changing the path to my local directory? (eg. C:\Documents\Project\cloud-functions\functions\https:\myimageurl.com) When I deploy it to Firebase Functions it's loading the right url but with an error that there isn't such file or directory. – Kosmonaft May 14 '19 at 07:39
  • I think I'd need a bit more information on how you've implemented and configured it. Could you post it as a new question with full context and then I can have a look at it. – Dennis Alund May 15 '19 at 06:41
  • 1
    I just tested this and no you can't just give it a url and have it uploaded directly from web to fb storage. – pseudozach May 31 '20 at 05:56
  • 1
    Thanks for pointing that out. I looked it up and this functionality was dropped in [v2.0 of the storage SDK](https://github.com/googleapis/nodejs-storage/releases/tag/v2.0.0). I see that my answer was actually already outdated at the time of writing. I'll update the answer to clarify – Dennis Alund Jun 01 '20 at 02:50