-
Notifications
You must be signed in to change notification settings - Fork 356
/
storage.ts
130 lines (120 loc) · 4.68 KB
/
storage.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*!
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {FirebaseApp} from '../firebase-app';
import {FirebaseError} from '../utils/error';
import {FirebaseServiceInterface, FirebaseServiceInternalsInterface} from '../firebase-service';
import {ServiceAccountCredential, ComputeEngineCredential} from '../auth/credential';
import {Bucket, Storage as StorageClient} from '@google-cloud/storage';
import * as utils from '../utils/index';
import * as validator from '../utils/validator';
/**
* Internals of a Storage instance.
*/
class StorageInternals implements FirebaseServiceInternalsInterface {
/**
* Deletes the service and its associated resources.
*
* @return {Promise<()>} An empty Promise that will be fulfilled when the service is deleted.
*/
public delete(): Promise<void> {
// There are no resources to clean up.
return Promise.resolve();
}
}
/**
* Storage service bound to the provided app.
*/
export class Storage implements FirebaseServiceInterface {
public readonly INTERNAL: StorageInternals = new StorageInternals();
private readonly appInternal: FirebaseApp;
private readonly storageClient: StorageClient;
/**
* @param {FirebaseApp} app The app for this Storage service.
* @constructor
*/
constructor(app: FirebaseApp) {
if (!validator.isNonNullObject(app) || !('options' in app)) {
throw new FirebaseError({
code: 'storage/invalid-argument',
message: 'First argument passed to admin.storage() must be a valid Firebase app instance.',
});
}
let storage: typeof StorageClient;
try {
storage = require('@google-cloud/storage').Storage;
} catch (err) {
throw new FirebaseError({
code: 'storage/missing-dependencies',
message: 'Failed to import the Cloud Storage client library for Node.js. '
+ 'Make sure to install the "@google-cloud/storage" npm package. '
+ `Original error: ${err}`,
});
}
const projectId: string | null = utils.getProjectId(app);
const credential = app.options.credential;
if (credential instanceof ServiceAccountCredential) {
this.storageClient = new storage({
// When the SDK is initialized with ServiceAccountCredentials projectId is guaranteed to
// be available.
projectId: projectId!,
credentials: {
private_key: credential.privateKey,
client_email: credential.clientEmail,
},
});
} else if (app.options.credential instanceof ComputeEngineCredential) {
// Try to use the Google application default credentials.
this.storageClient = new storage();
} else {
throw new FirebaseError({
code: 'storage/invalid-credential',
message: 'Failed to initialize Google Cloud Storage client with the available credential. ' +
'Must initialize the SDK with a certificate credential or application default credentials ' +
'to use Cloud Storage API.',
});
}
this.appInternal = app;
}
/**
* Returns a reference to a Google Cloud Storage bucket. Returned reference can be used to upload
* and download content from Google Cloud Storage.
*
* @param {string=} name Optional name of the bucket to be retrieved. If name is not specified,
* retrieves a reference to the default bucket.
* @return {Bucket} A Bucket object from the @google-cloud/storage library.
*/
public bucket(name?: string): Bucket {
const bucketName = (typeof name !== 'undefined')
? name : this.appInternal.options.storageBucket;
if (validator.isNonEmptyString(bucketName)) {
return this.storageClient.bucket(bucketName);
}
throw new FirebaseError({
code: 'storage/invalid-argument',
message: 'Bucket name not specified or invalid. Specify a valid bucket name via the ' +
'storageBucket option when initializing the app, or specify the bucket name ' +
'explicitly when calling the getBucket() method.',
});
}
/**
* Returns the app associated with this Storage instance.
*
* @return {FirebaseApp} The app associated with this Storage instance.
*/
get app(): FirebaseApp {
return this.appInternal;
}
}