1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | exports.Storage = void 0;
|
21 | var error_1 = require("../utils/error");
|
22 | var credential_internal_1 = require("../app/credential-internal");
|
23 | var utils = require("../utils/index");
|
24 | var validator = require("../utils/validator");
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var Storage = (function () {
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 | function Storage(app) {
|
37 | if (!validator.isNonNullObject(app) || !('options' in app)) {
|
38 | throw new error_1.FirebaseError({
|
39 | code: 'storage/invalid-argument',
|
40 | message: 'First argument passed to admin.storage() must be a valid Firebase app instance.',
|
41 | });
|
42 | }
|
43 | if (!process.env.STORAGE_EMULATOR_HOST && process.env.FIREBASE_STORAGE_EMULATOR_HOST) {
|
44 | var firebaseStorageEmulatorHost = process.env.FIREBASE_STORAGE_EMULATOR_HOST;
|
45 | if (firebaseStorageEmulatorHost.match(/https?:\/\//)) {
|
46 | throw new error_1.FirebaseError({
|
47 | code: 'storage/invalid-emulator-host',
|
48 | message: 'FIREBASE_STORAGE_EMULATOR_HOST should not contain a protocol (http or https).',
|
49 | });
|
50 | }
|
51 | process.env.STORAGE_EMULATOR_HOST = "http://" + process.env.FIREBASE_STORAGE_EMULATOR_HOST;
|
52 | }
|
53 | var storage;
|
54 | try {
|
55 | storage = require('@google-cloud/storage').Storage;
|
56 | }
|
57 | catch (err) {
|
58 | throw new error_1.FirebaseError({
|
59 | code: 'storage/missing-dependencies',
|
60 | message: 'Failed to import the Cloud Storage client library for Node.js. '
|
61 | + 'Make sure to install the "@google-cloud/storage" npm package. '
|
62 | + ("Original error: " + err),
|
63 | });
|
64 | }
|
65 | var projectId = utils.getExplicitProjectId(app);
|
66 | var credential = app.options.credential;
|
67 | if (credential instanceof credential_internal_1.ServiceAccountCredential) {
|
68 | this.storageClient = new storage({
|
69 |
|
70 |
|
71 | projectId: projectId,
|
72 | credentials: {
|
73 | private_key: credential.privateKey,
|
74 | client_email: credential.clientEmail,
|
75 | },
|
76 | });
|
77 | }
|
78 | else if (credential_internal_1.isApplicationDefault(app.options.credential)) {
|
79 |
|
80 | this.storageClient = new storage();
|
81 | }
|
82 | else {
|
83 | throw new error_1.FirebaseError({
|
84 | code: 'storage/invalid-credential',
|
85 | message: 'Failed to initialize Google Cloud Storage client with the available credential. ' +
|
86 | 'Must initialize the SDK with a certificate credential or application default credentials ' +
|
87 | 'to use Cloud Storage API.',
|
88 | });
|
89 | }
|
90 | this.appInternal = app;
|
91 | }
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | Storage.prototype.bucket = function (name) {
|
101 | var bucketName = (typeof name !== 'undefined')
|
102 | ? name : this.appInternal.options.storageBucket;
|
103 | if (validator.isNonEmptyString(bucketName)) {
|
104 | return this.storageClient.bucket(bucketName);
|
105 | }
|
106 | throw new error_1.FirebaseError({
|
107 | code: 'storage/invalid-argument',
|
108 | message: 'Bucket name not specified or invalid. Specify a valid bucket name via the ' +
|
109 | 'storageBucket option when initializing the app, or specify the bucket name ' +
|
110 | 'explicitly when calling the getBucket() method.',
|
111 | });
|
112 | };
|
113 | Object.defineProperty(Storage.prototype, "app", {
|
114 | |
115 |
|
116 |
|
117 |
|
118 | get: function () {
|
119 | return this.appInternal;
|
120 | },
|
121 | enumerable: false,
|
122 | configurable: true
|
123 | });
|
124 | return Storage;
|
125 | }());
|
126 | exports.Storage = Storage;
|