-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirestore_test.js
84 lines (71 loc) · 2.37 KB
/
firestore_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const path = require('path')
const firebase = require('firebase-admin')
console.log(path.join(__dirname, 'serviceAccountKey.json'))
var serviceAccount = require(path.join(__dirname, 'serviceAccountKey.json'))
const app = firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
storageBucket: 'ticketing-dlt-poc.appspot.com'
})
// const db = firebase.firestore(app)
// var citiesRef = db.collection('cities')
// const test = async () => {
// await citiesRef.doc('SF').set({
// name: 'San Francisco', state: 'CA', country: 'USA',
// capital: false, population: 860000
// })
// await citiesRef.doc('LA').set({
// name: 'Los Angeles', state: 'CA', country: 'USA',
// capital: false, population: 3900000
// })
// await citiesRef.doc('DC').set({
// name: 'Washington, D.C.', state: null, country: 'USA',
// capital: true, population: 680000
// })
// await citiesRef.doc('TOK').set({
// name: 'Tokyo', state: null, country: 'Japan',
// capital: true, population: 9000000
// })
// await citiesRef.doc('BJ').set({
// name: 'Beijing', state: null, country: 'China',
// capital: true, population: 21500000
// })
// return citiesRef.where('name', '==', 'Beijing').get()
// .then(snapshot => {
// snapshot.forEach(doc => {
// console.log(doc.id, '=>', doc.data())
// })
// })
// .catch(err => {
// console.log('Error getting documents', err)
// })
// }
// test().then(() => console.log('done'))
const UUID = require('uuid-v4');
const storage = firebase.storage()
const bucket = storage.bucket()
bucket.file('qr/myqr.png')
.getMetadata()
.then(metadata => console.log(JSON.stringify(metadata, null, 2)))
let uuid = UUID()
const fs = require('fs')
fs.readFileSync('logo.png')
const stream = bucket.file('qr/logo.png')
.createWriteStream({
uploadType: 'media',
metadata: {
contentType: 'image/png',
metadata: {
firebaseStorageDownloadTokens: uuid
}
}
})
stream.write(fs.readFileSync('logo.png'))
stream.on('finish', () => {
bucket.file('qr/logo.png')
.getMetadata()
.then(metadata => console.log(JSON.stringify(metadata, null, 2)))
.then(() =>{
console.log('https://firebasestorage.googleapis.com/v0/b/' + 'ticketing-dlt-poc.appspot.com' + '/o/' + encodeURIComponent('qr/logo.png') + '?alt=media&token=' + uuid)
})
})
stream.end()