|
| 1 | +<table> |
| 2 | +<tr> |
| 3 | +<td><img src="images/firebase.png" width="116px" height="32px" alt="Firebase"/></td> |
| 4 | +<td>Storage</td> |
| 5 | +</tr> |
| 6 | +</table> |
| 7 | + |
| 8 | +## Enabling Storage |
| 9 | +Since plugin version 3.4.0 you can use Firebase _Storage_ features. |
| 10 | + |
| 11 | +_Storage_ lets you upload and download files to/from Google Cloud Storage which is connected to your Firebase instance. |
| 12 | + |
| 13 | +To enable support for Remote Config you need to manually adjust |
| 14 | +[Podfile](../platforms/ios/Podfile) and [include.gradle](../platforms/android/include.gradle). |
| 15 | + |
| 16 | +Just uncomment the relevant lines (one for each platform) to add the SDK's to your app. |
| 17 | + |
| 18 | +### Setting the storage bucket |
| 19 | +You need to tell Firebase what your storage bucket is. You can retrieve it |
| 20 | +from the Firebase console by pressing 'Storage' in the menu. |
| 21 | + |
| 22 | +You can either pass it to the `init()` function as shown below, |
| 23 | +or pass it as a property any time you're using a storage feature. |
| 24 | +In theory the former is a little more efficient because it's cached by the plugin. |
| 25 | + |
| 26 | +```js |
| 27 | + firebase.init({ |
| 28 | + storageBucket: 'gs://n-plugin-test.appspot.com' |
| 29 | + // any other options |
| 30 | + }); |
| 31 | +``` |
| 32 | + |
| 33 | +## Functions |
| 34 | + |
| 35 | +### uploadFile |
| 36 | +You can either pass in a full local path to a file, or (as a convenience) use the `file-system` module that comes shipped with {N} as standard. |
| 37 | + |
| 38 | +```js |
| 39 | + // init the file-system module |
| 40 | + var fs = require("file-system"); |
| 41 | + |
| 42 | + // grab a reference to the app folder |
| 43 | + var appPath = fs.knownFolders.currentApp().path; |
| 44 | + |
| 45 | + // determine the path to a file in the app/res folder |
| 46 | + var logoPath = appPath + "/res/telerik-logo.png"; |
| 47 | + |
| 48 | + // now upload the file with either of the options below: |
| 49 | + firebase.uploadFile({ |
| 50 | + // optional, can also be passed during init() as 'storageBucket' param so we can cache it (find it in the Firebase console) |
| 51 | + bucket: 'gs://n-plugin-test.appspot.com', |
| 52 | + // the full path of the file in your Firebase storage (folders will be created) |
| 53 | + remoteFullPath: 'uploads/images/telerik-logo-uploaded.png', |
| 54 | + // option 1: a file-system module File object |
| 55 | + localFile: fs.File.fromPath(logoPath), |
| 56 | + // option 2: a full file path (ignored if 'localFile' is set) |
| 57 | + localFullPath: logoPath |
| 58 | + }).then( |
| 59 | + function (uploadedFile) |
| 60 | + console.log("File uploaded: " + JSON.stringify(uploadedFile)); |
| 61 | + }, |
| 62 | + function (error) { |
| 63 | + console.log("File upload error: " + error); |
| 64 | + }; |
| 65 | + ); |
| 66 | +``` |
| 67 | + |
| 68 | +### downloadFile |
| 69 | +As with `uploadFile` you can either pass in a full local path to a file, or (as a convenience) use the `file-system` module that comes shipped with {N} as standard. |
| 70 | + |
| 71 | +In this example we'll download the previously uploaded file to a certain path on the local filesystem. |
| 72 | + |
| 73 | +```js |
| 74 | + // init the file-system module |
| 75 | + var fs = require("file-system"); |
| 76 | + |
| 77 | + // let's first determine where we'll create the file using the 'file-system' module |
| 78 | + var documents = fs.knownFolders.documents(); |
| 79 | + var logoPath = documents.path + "/telerik-logo-downloaded.png"; |
| 80 | + |
| 81 | + // this will create or overwrite a local file in the app's documents folder |
| 82 | + var localLogoFile = documents.getFile("telerik-logo-downloaded.png"); |
| 83 | + |
| 84 | + // now download the file with either of the options below: |
| 85 | + firebase.downloadFile({ |
| 86 | + // optional, can also be passed during init() as 'storageBucket' param so we can cache it |
| 87 | + bucket: 'gs://n-plugin-test.appspot.com', |
| 88 | + // the full path of an existing file in your Firebase storage |
| 89 | + remoteFullPath: 'uploads/images/telerik-logo-uploaded.png', |
| 90 | + // option 1: a file-system module File object |
| 91 | + localFile: fs.File.fromPath(logoPath), |
| 92 | + // option 2: a full file path (ignored if 'localFile' is set) |
| 93 | + localFullPath: logoPath |
| 94 | + }).then( |
| 95 | + function (uploadedFile) |
| 96 | + console.log("File downloaded to the requested location"); |
| 97 | + }, |
| 98 | + function (error) { |
| 99 | + console.log("File download error: " + error); |
| 100 | + }; |
| 101 | + ); |
| 102 | +``` |
| 103 | + |
| 104 | +### getDownloadUrl |
| 105 | +If you just want to know the remote URL of a file in remote storage so you can either share it or download the file by any other means than `downloadFile` then use this method. |
| 106 | + |
| 107 | +In this example we'll determine the remote URL of the previously uploaded file. |
| 108 | + |
| 109 | +```js |
| 110 | + firebase.getDownloadUrl({ |
| 111 | + // optional, can also be passed during init() as 'storageBucket' param so we can cache it |
| 112 | + bucket: 'gs://n-plugin-test.appspot.com', |
| 113 | + // the full path of an existing file in your Firebase storage |
| 114 | + remoteFullPath: 'uploads/images/telerik-logo-uploaded.png' |
| 115 | + }).then( |
| 116 | + function (url) |
| 117 | + console.log("Remote URL: " + url); |
| 118 | + }, |
| 119 | + function (error) { |
| 120 | + console.log("Error: " + error); |
| 121 | + }; |
| 122 | + ); |
| 123 | +``` |
0 commit comments