Skip to content

Add Perf and Storage snippets #90

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

Merged
merged 11 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions perf-next/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ const firebaseApp = initializeApp({
});
const perf = getInstance(firebaseApp);

function intialize() {
// [START perf_initialize]
const { initializeApp } = require("firebase/app");
const { getPerformance } = require("firebase/performance");

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/setup#config-object
const firebaseConfig = {
// ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Initialize Performance Monitoring and get a reference to the service
const perf = getPerformance(app);
// [END perf_initialize]
}

export function getInstance(firebaseApp) {
// [START perf_get_instance]
const { getPerformance } = require("firebase/performance");
Expand Down
16 changes: 16 additions & 0 deletions perf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ import "firebase/performance";

const perf = firebase.performance();

function intialize() {
// [START perf_initialize]
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/setup#config-object
var firebaseConfig = {
// ...
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

// Initialize Performance Monitoring and get a reference to the service
var perf = firebase.performance();
// [END perf_initialize]
}

function addCustomTrace() {
// [START perf_add_custom_trace]
const trace = perf.trace("CUSTOM_TRACE_NAME");
Expand Down
21 changes: 21 additions & 0 deletions snippets/perf-next/index/perf_initialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This snippet file was generated by processing the source file:
// ./perf-next/index.js
//
// To make edits to the snippets in this file, please edit the source

// [START perf_initialize_modular]
import { initializeApp } from "firebase/app";
import { getPerformance } from "firebase/performance";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/setup#config-object
const firebaseConfig = {
// ...
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

// Initialize Performance Monitoring and get a reference to the service
const perf = getPerformance(app);
// [END perf_initialize_modular]
95 changes: 95 additions & 0 deletions storage/create-reference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import firebase from "firebase/app";
import "firebase/storage";

function createRef() {
// [START storage_create_ref]
// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();

// Create a storage reference from our storage service
var storageRef = storage.ref();
// [END storage_create_ref]
}

function createRefChild() {
const storageRef = firebase.storage().ref();

// [START storage_create_ref_child]
// Create a child reference
var imagesRef = storageRef.child('images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
var spaceRef = storageRef.child('images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
// [END storage_create_ref_child]
}

function navigateRef() {
const spaceRef = firebase.storage().ref().child('images/space.jpg');

// [START storage_navigate_ref]
// Parent allows us to move to the parent of a reference
var imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
var rootRef = spaceRef.root;
// rootRef now points to the root
// [END storage_navigate_ref]
}

function navigateRefChain() {
const spaceRef = firebase.storage().ref().child('images/space.jpg');

// [START storage_navigate_ref_chain]
// References can be chained together multiple times
var earthRef = spaceRef.parent.child('earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
var nullRef = spaceRef.root.parent;
// [END storage_navigate_ref_chain]
}

function refProperties() {
const spaceRef = firebase.storage().ref().child('images/space.jpg');

// [START storage_ref_properties]
// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;
// [END storage_ref_properties]
}

function refFullExample() {
// [START storage_ref_full_example]
// Points to the root reference
var storageRef = firebase.storage().ref();

// Points to 'images'
var imagesRef = storageRef.child('images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
var fileName = 'space.jpg';
var spaceRef = imagesRef.child(fileName);

// File path is 'images/space.jpg'
var path = spaceRef.fullPath;

// File name is 'space.jpg'
var name = spaceRef.name;

// Points to 'images'
var imagesRef = spaceRef.parent;
// [END storage_ref_full_example]
}
18 changes: 18 additions & 0 deletions storage/delete-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import firebase from "firebase/app";
import "firebase/storage";

function deleteFile() {
const storageRef = firebase.storage().ref();

// [START storage_delete_file]
// Create a reference to the file to delete
var desertRef = storageRef.child('images/desert.jpg');

// Delete the file
desertRef.delete().then(() => {
// File deleted successfully
}).catch((error) => {
// Uh-oh, an error occurred!
});
// [END storage_delete_file]
}
80 changes: 80 additions & 0 deletions storage/download-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import firebase from "firebase/app";
import "firebase/storage";

function downloadCreateRef() {
// [START storage_download_create_ref]
// Create a reference with an initial file path and name
var storage = firebase.storage();
var pathReference = storage.ref('images/stars.jpg');

// Create a reference from a Google Cloud Storage URI
var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg');

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
// [END storage_download_create_ref]
}

function downloadViaUrl() {
const storageRef = firebase.storage().ref();

// [START storage_download_via_url]
storageRef.child('images/stars.jpg').getDownloadURL()
.then((url) => {
// `url` is the download URL for 'images/stars.jpg'

// This can be downloaded directly:
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = (event) => {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();

// Or inserted into an <img> element
var img = document.getElementById('myimg');
img.setAttribute('src', url);
})
.catch((error) => {
// Handle any errors
});
// [END storage_download_via_url]
}

function downloadFullExample() {
const storageRef = firebase.storage().ref();

// [START storage_download_full_example]
// Create a reference to the file we want to download
var starsRef = storageRef.child('images/stars.jpg');

// Get the download URL
starsRef.getDownloadURL()
.then((url) => {
// Insert url into an <img> tag to "download"
})
.catch((error) => {
// A full list of error codes is available at
// https://firebase.google.com/docs/storage/web/handle-errors
switch (error.code) {
case 'storage/object-not-found':
// File doesn't exist
break;
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;

// ...

case 'storage/unknown':
// Unknown error occurred, inspect the server response
break;
}
});
// [END storage_download_full_example]
}
75 changes: 75 additions & 0 deletions storage/file-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import firebase from "firebase/app";
import "firebase/storage";

function getMetadata() {
const storageRef = firebase.storage().ref();

// [START storage_get_metadata]
// Create a reference to the file whose metadata we want to retrieve
var forestRef = storageRef.child('images/forest.jpg');

// Get metadata properties
forestRef.getMetadata()
.then((metadata) => {
// Metadata now contains the metadata for 'images/forest.jpg'
})
.catch((error) => {
// Uh-oh, an error occurred!
});
// [END storage_get_metadata]
}

function updateMetadata() {
const storageRef = firebase.storage().ref();

// [START storage_update_metadata]
// Create a reference to the file whose metadata we want to change
var forestRef = storageRef.child('images/forest.jpg');

// Create file metadata to update
var newMetadata = {
cacheControl: 'public,max-age=300',
contentType: 'image/jpeg'
};

// Update metadata properties
forestRef.updateMetadata(newMetadata)
.then((metadata) => {
// Updated metadata for 'images/forest.jpg' is returned in the Promise
}).catch((error) => {
// Uh-oh, an error occurred!
});
// [END storage_update_metadata]
}

function deleteMetadata() {
const storageRef = firebase.storage().ref();
const forestRef = storageRef.child('images/forest.jpg');

// [START storage_delete_metadata]

// Create file metadata with property to delete
var deleteMetadata = {
contentType: null
};

// Delete the metadata property
forestRef.updateMetadata(deleteMetadata)
.then((metadata) => {
// metadata.contentType should be null
}).catch((error) => {
// Uh-oh, an error occurred!
});
// [END storage_delete_metadata]
}

function customMetadata() {
// [START storage_custom_metadata]
var metadata = {
customMetadata: {
'location': 'Yosemite, CA, USA',
'activity': 'Hiking'
}
};
// [END storage_custom_metadata]
}
38 changes: 38 additions & 0 deletions storage/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
import firebase from "firebase/app";
import "firebase/storage";

function initialize() {
// [START storage_initialize]
// Set the configuration for your app
// TODO: Replace with your app's config object
var firebaseConfig = {
apiKey: '<your-api-key>',
authDomain: '<your-auth-domain>',
databaseURL: '<your-database-url>',
storageBucket: '<your-storage-bucket-url>'
};
firebase.initializeApp(firebaseConfig);

// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();
// [END storage_initialize]
}

function multipleBuckets() {
// [START storage_multiple_buckets]
// Get a non-default Storage bucket
var storage = firebase.app().storage("gs://my-custom-bucket");
// [END storage_multiple_buckets]
}

function storageCustomApp() {
const customApp = firebase.initializeApp({
// ... custom stuff
});

// [START storage_custom_app]
// Get the default bucket from a custom firebase.app.App
var storage = customApp.storage();

// Get a non-default bucket from a custom firebase.app.App
var storage = customApp.storage("gs://my-custom-bucket");
// [END storage_custom_app]
}

/**
* @param {File} file
*/
Expand Down
Loading