storage.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*! firebase-admin v12.1.1 */
  2. "use strict";
  3. /*!
  4. * @license
  5. * Copyright 2017 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. Object.defineProperty(exports, "__esModule", { value: true });
  20. exports.Storage = void 0;
  21. const error_1 = require("../utils/error");
  22. const credential_internal_1 = require("../app/credential-internal");
  23. const utils = require("../utils/index");
  24. const validator = require("../utils/validator");
  25. /**
  26. * The default `Storage` service if no
  27. * app is provided or the `Storage` service associated with the provided
  28. * app.
  29. */
  30. class Storage {
  31. /**
  32. * @param app - The app for this Storage service.
  33. * @constructor
  34. * @internal
  35. */
  36. constructor(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. const 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. let 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. const projectId = utils.getExplicitProjectId(app);
  66. const credential = app.options.credential;
  67. if (credential instanceof credential_internal_1.ServiceAccountCredential) {
  68. this.storageClient = new storage({
  69. // When the SDK is initialized with ServiceAccountCredentials an explicit projectId is
  70. // guaranteed to be available.
  71. projectId: projectId,
  72. credentials: {
  73. private_key: credential.privateKey,
  74. client_email: credential.clientEmail,
  75. },
  76. });
  77. }
  78. else if ((0, credential_internal_1.isApplicationDefault)(app.options.credential)) {
  79. // Try to use the Google application default credentials.
  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. * Gets a reference to a Cloud Storage bucket.
  94. *
  95. * @param name - Optional name of the bucket to be retrieved. If name is not specified,
  96. * retrieves a reference to the default bucket.
  97. * @returns A {@link https://cloud.google.com/nodejs/docs/reference/storage/latest/Bucket | Bucket}
  98. * instance as defined in the `@google-cloud/storage` package.
  99. */
  100. bucket(name) {
  101. const 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. /**
  114. * Optional app whose `Storage` service to
  115. * return. If not provided, the default `Storage` service will be returned.
  116. */
  117. get app() {
  118. return this.appInternal;
  119. }
  120. }
  121. exports.Storage = Storage;