bucket.js 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import { ServiceObject, util, } from './nodejs-common/index.js';
  15. import { paginator } from '@google-cloud/paginator';
  16. import { promisifyAll } from '@google-cloud/promisify';
  17. import * as fs from 'fs';
  18. import mime from 'mime';
  19. import * as path from 'path';
  20. import pLimit from 'p-limit';
  21. import { promisify } from 'util';
  22. import AsyncRetry from 'async-retry';
  23. import { convertObjKeysToSnakeCase } from './util.js';
  24. import { Acl } from './acl.js';
  25. import { File, } from './file.js';
  26. import { Iam } from './iam.js';
  27. import { Notification } from './notification.js';
  28. import { IdempotencyStrategy, } from './storage.js';
  29. import { URLSigner, } from './signer.js';
  30. import { Readable } from 'stream';
  31. import { URL } from 'url';
  32. export var BucketActionToHTTPMethod;
  33. (function (BucketActionToHTTPMethod) {
  34. BucketActionToHTTPMethod["list"] = "GET";
  35. })(BucketActionToHTTPMethod || (BucketActionToHTTPMethod = {}));
  36. export var AvailableServiceObjectMethods;
  37. (function (AvailableServiceObjectMethods) {
  38. AvailableServiceObjectMethods[AvailableServiceObjectMethods["setMetadata"] = 0] = "setMetadata";
  39. AvailableServiceObjectMethods[AvailableServiceObjectMethods["delete"] = 1] = "delete";
  40. })(AvailableServiceObjectMethods || (AvailableServiceObjectMethods = {}));
  41. export var BucketExceptionMessages;
  42. (function (BucketExceptionMessages) {
  43. BucketExceptionMessages["PROVIDE_SOURCE_FILE"] = "You must provide at least one source file.";
  44. BucketExceptionMessages["DESTINATION_FILE_NOT_SPECIFIED"] = "A destination file must be specified.";
  45. BucketExceptionMessages["CHANNEL_ID_REQUIRED"] = "An ID is required to create a channel.";
  46. BucketExceptionMessages["TOPIC_NAME_REQUIRED"] = "A valid topic name is required.";
  47. BucketExceptionMessages["CONFIGURATION_OBJECT_PREFIX_REQUIRED"] = "A configuration object with a prefix is required.";
  48. BucketExceptionMessages["SPECIFY_FILE_NAME"] = "A file name must be specified.";
  49. BucketExceptionMessages["METAGENERATION_NOT_PROVIDED"] = "A metageneration must be provided.";
  50. BucketExceptionMessages["SUPPLY_NOTIFICATION_ID"] = "You must supply a notification ID.";
  51. })(BucketExceptionMessages || (BucketExceptionMessages = {}));
  52. /**
  53. * @callback Crc32cGeneratorToStringCallback
  54. * A method returning the CRC32C as a base64-encoded string.
  55. *
  56. * @returns {string}
  57. *
  58. * @example
  59. * Hashing the string 'data' should return 'rth90Q=='
  60. *
  61. * ```js
  62. * const buffer = Buffer.from('data');
  63. * crc32c.update(buffer);
  64. * crc32c.toString(); // 'rth90Q=='
  65. * ```
  66. **/
  67. /**
  68. * @callback Crc32cGeneratorValidateCallback
  69. * A method validating a base64-encoded CRC32C string.
  70. *
  71. * @param {string} [value] base64-encoded CRC32C string to validate
  72. * @returns {boolean}
  73. *
  74. * @example
  75. * Should return `true` if the value matches, `false` otherwise
  76. *
  77. * ```js
  78. * const buffer = Buffer.from('data');
  79. * crc32c.update(buffer);
  80. * crc32c.validate('DkjKuA=='); // false
  81. * crc32c.validate('rth90Q=='); // true
  82. * ```
  83. **/
  84. /**
  85. * @callback Crc32cGeneratorUpdateCallback
  86. * A method for passing `Buffer`s for CRC32C generation.
  87. *
  88. * @param {Buffer} [data] data to update CRC32C value with
  89. * @returns {undefined}
  90. *
  91. * @example
  92. * Hashing buffers from 'some ' and 'text\n'
  93. *
  94. * ```js
  95. * const buffer1 = Buffer.from('some ');
  96. * crc32c.update(buffer1);
  97. *
  98. * const buffer2 = Buffer.from('text\n');
  99. * crc32c.update(buffer2);
  100. *
  101. * crc32c.toString(); // 'DkjKuA=='
  102. * ```
  103. **/
  104. /**
  105. * @typedef {object} CRC32CValidator
  106. * @property {Crc32cGeneratorToStringCallback}
  107. * @property {Crc32cGeneratorValidateCallback}
  108. * @property {Crc32cGeneratorUpdateCallback}
  109. */
  110. /**
  111. * A function that generates a CRC32C Validator. Defaults to {@link CRC32C}
  112. *
  113. * @name Bucket#crc32cGenerator
  114. * @type {CRC32CValidator}
  115. */
  116. /**
  117. * Get and set IAM policies for your bucket.
  118. *
  119. * @name Bucket#iam
  120. * @mixes Iam
  121. *
  122. * See {@link https://cloud.google.com/storage/docs/access-control/iam#short_title_iam_management| Cloud Storage IAM Management}
  123. * See {@link https://cloud.google.com/iam/docs/granting-changing-revoking-access| Granting, Changing, and Revoking Access}
  124. * See {@link https://cloud.google.com/iam/docs/understanding-roles| IAM Roles}
  125. *
  126. * @example
  127. * ```
  128. * const {Storage} = require('@google-cloud/storage');
  129. * const storage = new Storage();
  130. * const bucket = storage.bucket('albums');
  131. *
  132. * //-
  133. * // Get the IAM policy for your bucket.
  134. * //-
  135. * bucket.iam.getPolicy(function(err, policy) {
  136. * console.log(policy);
  137. * });
  138. *
  139. * //-
  140. * // If the callback is omitted, we'll return a Promise.
  141. * //-
  142. * bucket.iam.getPolicy().then(function(data) {
  143. * const policy = data[0];
  144. * const apiResponse = data[1];
  145. * });
  146. *
  147. * ```
  148. * @example <caption>include:samples/iam.js</caption>
  149. * region_tag:storage_view_bucket_iam_members
  150. * Example of retrieving a bucket's IAM policy:
  151. *
  152. * @example <caption>include:samples/iam.js</caption>
  153. * region_tag:storage_add_bucket_iam_member
  154. * Example of adding to a bucket's IAM policy:
  155. *
  156. * @example <caption>include:samples/iam.js</caption>
  157. * region_tag:storage_remove_bucket_iam_member
  158. * Example of removing from a bucket's IAM policy:
  159. */
  160. /**
  161. * Cloud Storage uses access control lists (ACLs) to manage object and
  162. * bucket access. ACLs are the mechanism you use to share objects with other
  163. * users and allow other users to access your buckets and objects.
  164. *
  165. * An ACL consists of one or more entries, where each entry grants permissions
  166. * to an entity. Permissions define the actions that can be performed against
  167. * an object or bucket (for example, `READ` or `WRITE`); the entity defines
  168. * who the permission applies to (for example, a specific user or group of
  169. * users).
  170. *
  171. * The `acl` object on a Bucket instance provides methods to get you a list of
  172. * the ACLs defined on your bucket, as well as set, update, and delete them.
  173. *
  174. * Buckets also have
  175. * {@link https://cloud.google.com/storage/docs/access-control/lists#default| default ACLs}
  176. * for all created files. Default ACLs specify permissions that all new
  177. * objects added to the bucket will inherit by default. You can add, delete,
  178. * get, and update entities and permissions for these as well with
  179. * {@link Bucket#acl.default}.
  180. *
  181. * See {@link http://goo.gl/6qBBPO| About Access Control Lists}
  182. * See {@link https://cloud.google.com/storage/docs/access-control/lists#default| Default ACLs}
  183. *
  184. * @name Bucket#acl
  185. * @mixes Acl
  186. * @property {Acl} default Cloud Storage Buckets have
  187. * {@link https://cloud.google.com/storage/docs/access-control/lists#default| default ACLs}
  188. * for all created files. You can add, delete, get, and update entities and
  189. * permissions for these as well. The method signatures and examples are all
  190. * the same, after only prefixing the method call with `default`.
  191. *
  192. * @example
  193. * ```
  194. * const {Storage} = require('@google-cloud/storage');
  195. * const storage = new Storage();
  196. *
  197. * //-
  198. * // Make a bucket's contents publicly readable.
  199. * //-
  200. * const myBucket = storage.bucket('my-bucket');
  201. *
  202. * const options = {
  203. * entity: 'allUsers',
  204. * role: storage.acl.READER_ROLE
  205. * };
  206. *
  207. * myBucket.acl.add(options, function(err, aclObject) {});
  208. *
  209. * //-
  210. * // If the callback is omitted, we'll return a Promise.
  211. * //-
  212. * myBucket.acl.add(options).then(function(data) {
  213. * const aclObject = data[0];
  214. * const apiResponse = data[1];
  215. * });
  216. *
  217. * ```
  218. * @example <caption>include:samples/acl.js</caption>
  219. * region_tag:storage_print_bucket_acl
  220. * Example of printing a bucket's ACL:
  221. *
  222. * @example <caption>include:samples/acl.js</caption>
  223. * region_tag:storage_print_bucket_acl_for_user
  224. * Example of printing a bucket's ACL for a specific user:
  225. *
  226. * @example <caption>include:samples/acl.js</caption>
  227. * region_tag:storage_add_bucket_owner
  228. * Example of adding an owner to a bucket:
  229. *
  230. * @example <caption>include:samples/acl.js</caption>
  231. * region_tag:storage_remove_bucket_owner
  232. * Example of removing an owner from a bucket:
  233. *
  234. * @example <caption>include:samples/acl.js</caption>
  235. * region_tag:storage_add_bucket_default_owner
  236. * Example of adding a default owner to a bucket:
  237. *
  238. * @example <caption>include:samples/acl.js</caption>
  239. * region_tag:storage_remove_bucket_default_owner
  240. * Example of removing a default owner from a bucket:
  241. */
  242. /**
  243. * The API-formatted resource description of the bucket.
  244. *
  245. * Note: This is not guaranteed to be up-to-date when accessed. To get the
  246. * latest record, call the `getMetadata()` method.
  247. *
  248. * @name Bucket#metadata
  249. * @type {object}
  250. */
  251. /**
  252. * The bucket's name.
  253. * @name Bucket#name
  254. * @type {string}
  255. */
  256. /**
  257. * Get {@link File} objects for the files currently in the bucket as a
  258. * readable object stream.
  259. *
  260. * @method Bucket#getFilesStream
  261. * @param {GetFilesOptions} [query] Query object for listing files.
  262. * @returns {ReadableStream} A readable stream that emits {@link File} instances.
  263. *
  264. * @example
  265. * ```
  266. * const {Storage} = require('@google-cloud/storage');
  267. * const storage = new Storage();
  268. * const bucket = storage.bucket('albums');
  269. *
  270. * bucket.getFilesStream()
  271. * .on('error', console.error)
  272. * .on('data', function(file) {
  273. * // file is a File object.
  274. * })
  275. * .on('end', function() {
  276. * // All files retrieved.
  277. * });
  278. *
  279. * //-
  280. * // If you anticipate many results, you can end a stream early to prevent
  281. * // unnecessary processing and API requests.
  282. * //-
  283. * bucket.getFilesStream()
  284. * .on('data', function(file) {
  285. * this.end();
  286. * });
  287. *
  288. * //-
  289. * // If you're filtering files with a delimiter, you should use
  290. * // {@link Bucket#getFiles} and set `autoPaginate: false` in order to
  291. * // preserve the `apiResponse` argument.
  292. * //-
  293. * const prefixes = [];
  294. *
  295. * function callback(err, files, nextQuery, apiResponse) {
  296. * prefixes = prefixes.concat(apiResponse.prefixes);
  297. *
  298. * if (nextQuery) {
  299. * bucket.getFiles(nextQuery, callback);
  300. * } else {
  301. * // prefixes = The finished array of prefixes.
  302. * }
  303. * }
  304. *
  305. * bucket.getFiles({
  306. * autoPaginate: false,
  307. * delimiter: '/'
  308. * }, callback);
  309. * ```
  310. */
  311. /**
  312. * Create a Bucket object to interact with a Cloud Storage bucket.
  313. *
  314. * @class
  315. * @hideconstructor
  316. *
  317. * @param {Storage} storage A {@link Storage} instance.
  318. * @param {string} name The name of the bucket.
  319. * @param {object} [options] Configuration object.
  320. * @param {string} [options.userProject] User project.
  321. *
  322. * @example
  323. * ```
  324. * const {Storage} = require('@google-cloud/storage');
  325. * const storage = new Storage();
  326. * const bucket = storage.bucket('albums');
  327. * ```
  328. */
  329. class Bucket extends ServiceObject {
  330. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  331. getFilesStream(query) {
  332. // placeholder body, overwritten in constructor
  333. return new Readable();
  334. }
  335. constructor(storage, name, options) {
  336. var _a, _b, _c, _d;
  337. options = options || {};
  338. // Allow for "gs://"-style input, and strip any trailing slashes.
  339. name = name.replace(/^gs:\/\//, '').replace(/\/+$/, '');
  340. const requestQueryObject = {};
  341. if ((_a = options === null || options === void 0 ? void 0 : options.preconditionOpts) === null || _a === void 0 ? void 0 : _a.ifGenerationMatch) {
  342. requestQueryObject.ifGenerationMatch =
  343. options.preconditionOpts.ifGenerationMatch;
  344. }
  345. if ((_b = options === null || options === void 0 ? void 0 : options.preconditionOpts) === null || _b === void 0 ? void 0 : _b.ifGenerationNotMatch) {
  346. requestQueryObject.ifGenerationNotMatch =
  347. options.preconditionOpts.ifGenerationNotMatch;
  348. }
  349. if ((_c = options === null || options === void 0 ? void 0 : options.preconditionOpts) === null || _c === void 0 ? void 0 : _c.ifMetagenerationMatch) {
  350. requestQueryObject.ifMetagenerationMatch =
  351. options.preconditionOpts.ifMetagenerationMatch;
  352. }
  353. if ((_d = options === null || options === void 0 ? void 0 : options.preconditionOpts) === null || _d === void 0 ? void 0 : _d.ifMetagenerationNotMatch) {
  354. requestQueryObject.ifMetagenerationNotMatch =
  355. options.preconditionOpts.ifMetagenerationNotMatch;
  356. }
  357. const userProject = options.userProject;
  358. if (typeof userProject === 'string') {
  359. requestQueryObject.userProject = userProject;
  360. }
  361. const methods = {
  362. /**
  363. * Create a bucket.
  364. *
  365. * @method Bucket#create
  366. * @param {CreateBucketRequest} [metadata] Metadata to set for the bucket.
  367. * @param {CreateBucketCallback} [callback] Callback function.
  368. * @returns {Promise<CreateBucketResponse>}
  369. *
  370. * @example
  371. * ```
  372. * const {Storage} = require('@google-cloud/storage');
  373. * const storage = new Storage();
  374. * const bucket = storage.bucket('albums');
  375. * bucket.create(function(err, bucket, apiResponse) {
  376. * if (!err) {
  377. * // The bucket was created successfully.
  378. * }
  379. * });
  380. *
  381. * //-
  382. * // If the callback is omitted, we'll return a Promise.
  383. * //-
  384. * bucket.create().then(function(data) {
  385. * const bucket = data[0];
  386. * const apiResponse = data[1];
  387. * });
  388. * ```
  389. */
  390. create: {
  391. reqOpts: {
  392. qs: requestQueryObject,
  393. },
  394. },
  395. /**
  396. * IamDeleteBucketOptions Configuration options.
  397. * @property {boolean} [ignoreNotFound = false] Ignore an error if
  398. * the bucket does not exist.
  399. * @property {string} [userProject] The ID of the project which will be
  400. * billed for the request.
  401. */
  402. /**
  403. * @typedef {array} DeleteBucketResponse
  404. * @property {object} 0 The full API response.
  405. */
  406. /**
  407. * @callback DeleteBucketCallback
  408. * @param {?Error} err Request error, if any.
  409. * @param {object} apiResponse The full API response.
  410. */
  411. /**
  412. * Delete the bucket.
  413. *
  414. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/delete| Buckets: delete API Documentation}
  415. *
  416. * @method Bucket#delete
  417. * @param {DeleteBucketOptions} [options] Configuration options.
  418. * @param {boolean} [options.ignoreNotFound = false] Ignore an error if
  419. * the bucket does not exist.
  420. * @param {string} [options.userProject] The ID of the project which will be
  421. * billed for the request.
  422. * @param {DeleteBucketCallback} [callback] Callback function.
  423. * @returns {Promise<DeleteBucketResponse>}
  424. *
  425. * @example
  426. * ```
  427. * const {Storage} = require('@google-cloud/storage');
  428. * const storage = new Storage();
  429. * const bucket = storage.bucket('albums');
  430. * bucket.delete(function(err, apiResponse) {});
  431. *
  432. * //-
  433. * // If the callback is omitted, we'll return a Promise.
  434. * //-
  435. * bucket.delete().then(function(data) {
  436. * const apiResponse = data[0];
  437. * });
  438. *
  439. * ```
  440. * @example <caption>include:samples/buckets.js</caption>
  441. * region_tag:storage_delete_bucket
  442. * Another example:
  443. */
  444. delete: {
  445. reqOpts: {
  446. qs: requestQueryObject,
  447. },
  448. },
  449. /**
  450. * @typedef {object} BucketExistsOptions Configuration options for Bucket#exists().
  451. * @property {string} [userProject] The ID of the project which will be
  452. * billed for the request.
  453. */
  454. /**
  455. * @typedef {array} BucketExistsResponse
  456. * @property {boolean} 0 Whether the {@link Bucket} exists.
  457. */
  458. /**
  459. * @callback BucketExistsCallback
  460. * @param {?Error} err Request error, if any.
  461. * @param {boolean} exists Whether the {@link Bucket} exists.
  462. */
  463. /**
  464. * Check if the bucket exists.
  465. *
  466. * @method Bucket#exists
  467. * @param {BucketExistsOptions} [options] Configuration options.
  468. * @param {string} [options.userProject] The ID of the project which will be
  469. * billed for the request.
  470. * @param {BucketExistsCallback} [callback] Callback function.
  471. * @returns {Promise<BucketExistsResponse>}
  472. *
  473. * @example
  474. * ```
  475. * const {Storage} = require('@google-cloud/storage');
  476. * const storage = new Storage();
  477. * const bucket = storage.bucket('albums');
  478. *
  479. * bucket.exists(function(err, exists) {});
  480. *
  481. * //-
  482. * // If the callback is omitted, we'll return a Promise.
  483. * //-
  484. * bucket.exists().then(function(data) {
  485. * const exists = data[0];
  486. * });
  487. * ```
  488. */
  489. exists: {
  490. reqOpts: {
  491. qs: requestQueryObject,
  492. },
  493. },
  494. /**
  495. * @typedef {object} [GetBucketOptions] Configuration options for Bucket#get()
  496. * @property {boolean} [autoCreate] Automatically create the object if
  497. * it does not exist. Default: `false`
  498. * @property {string} [userProject] The ID of the project which will be
  499. * billed for the request.
  500. */
  501. /**
  502. * @typedef {array} GetBucketResponse
  503. * @property {Bucket} 0 The {@link Bucket}.
  504. * @property {object} 1 The full API response.
  505. */
  506. /**
  507. * @callback GetBucketCallback
  508. * @param {?Error} err Request error, if any.
  509. * @param {Bucket} bucket The {@link Bucket}.
  510. * @param {object} apiResponse The full API response.
  511. */
  512. /**
  513. * Get a bucket if it exists.
  514. *
  515. * You may optionally use this to "get or create" an object by providing
  516. * an object with `autoCreate` set to `true`. Any extra configuration that
  517. * is normally required for the `create` method must be contained within
  518. * this object as well.
  519. *
  520. * @method Bucket#get
  521. * @param {GetBucketOptions} [options] Configuration options.
  522. * @param {boolean} [options.autoCreate] Automatically create the object if
  523. * it does not exist. Default: `false`
  524. * @param {string} [options.userProject] The ID of the project which will be
  525. * billed for the request.
  526. * @param {GetBucketCallback} [callback] Callback function.
  527. * @returns {Promise<GetBucketResponse>}
  528. *
  529. * @example
  530. * ```
  531. * const {Storage} = require('@google-cloud/storage');
  532. * const storage = new Storage();
  533. * const bucket = storage.bucket('albums');
  534. *
  535. * bucket.get(function(err, bucket, apiResponse) {
  536. * // `bucket.metadata` has been populated.
  537. * });
  538. *
  539. * //-
  540. * // If the callback is omitted, we'll return a Promise.
  541. * //-
  542. * bucket.get().then(function(data) {
  543. * const bucket = data[0];
  544. * const apiResponse = data[1];
  545. * });
  546. * ```
  547. */
  548. get: {
  549. reqOpts: {
  550. qs: requestQueryObject,
  551. },
  552. },
  553. /**
  554. * @typedef {array} GetBucketMetadataResponse
  555. * @property {object} 0 The bucket metadata.
  556. * @property {object} 1 The full API response.
  557. */
  558. /**
  559. * @callback GetBucketMetadataCallback
  560. * @param {?Error} err Request error, if any.
  561. * @param {object} metadata The bucket metadata.
  562. * @param {object} apiResponse The full API response.
  563. */
  564. /**
  565. * @typedef {object} GetBucketMetadataOptions Configuration options for Bucket#getMetadata().
  566. * @property {string} [userProject] The ID of the project which will be
  567. * billed for the request.
  568. */
  569. /**
  570. * Get the bucket's metadata.
  571. *
  572. * To set metadata, see {@link Bucket#setMetadata}.
  573. *
  574. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/get| Buckets: get API Documentation}
  575. *
  576. * @method Bucket#getMetadata
  577. * @param {GetBucketMetadataOptions} [options] Configuration options.
  578. * @param {string} [options.userProject] The ID of the project which will be
  579. * billed for the request.
  580. * @param {GetBucketMetadataCallback} [callback] Callback function.
  581. * @returns {Promise<GetBucketMetadataResponse>}
  582. *
  583. * @example
  584. * ```
  585. * const {Storage} = require('@google-cloud/storage');
  586. * const storage = new Storage();
  587. * const bucket = storage.bucket('albums');
  588. *
  589. * bucket.getMetadata(function(err, metadata, apiResponse) {});
  590. *
  591. * //-
  592. * // If the callback is omitted, we'll return a Promise.
  593. * //-
  594. * bucket.getMetadata().then(function(data) {
  595. * const metadata = data[0];
  596. * const apiResponse = data[1];
  597. * });
  598. *
  599. * ```
  600. * @example <caption>include:samples/requesterPays.js</caption>
  601. * region_tag:storage_get_requester_pays_status
  602. * Example of retrieving the requester pays status of a bucket:
  603. */
  604. getMetadata: {
  605. reqOpts: {
  606. qs: requestQueryObject,
  607. },
  608. },
  609. /**
  610. * @typedef {object} SetBucketMetadataOptions Configuration options for Bucket#setMetadata().
  611. * @property {string} [userProject] The ID of the project which will be
  612. * billed for the request.
  613. */
  614. /**
  615. * @typedef {array} SetBucketMetadataResponse
  616. * @property {object} apiResponse The full API response.
  617. */
  618. /**
  619. * @callback SetBucketMetadataCallback
  620. * @param {?Error} err Request error, if any.
  621. * @param {object} metadata The bucket metadata.
  622. */
  623. /**
  624. * Set the bucket's metadata.
  625. *
  626. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/patch| Buckets: patch API Documentation}
  627. *
  628. * @method Bucket#setMetadata
  629. * @param {object<string, *>} metadata The metadata you wish to set.
  630. * @param {SetBucketMetadataOptions} [options] Configuration options.
  631. * @param {string} [options.userProject] The ID of the project which will be
  632. * billed for the request.
  633. * @param {SetBucketMetadataCallback} [callback] Callback function.
  634. * @returns {Promise<SetBucketMetadataResponse>}
  635. *
  636. * @example
  637. * ```
  638. * const {Storage} = require('@google-cloud/storage');
  639. * const storage = new Storage();
  640. * const bucket = storage.bucket('albums');
  641. *
  642. * //-
  643. * // Set website metadata field on the bucket.
  644. * //-
  645. * const metadata = {
  646. * website: {
  647. * mainPageSuffix: 'http://example.com',
  648. * notFoundPage: 'http://example.com/404.html'
  649. * }
  650. * };
  651. *
  652. * bucket.setMetadata(metadata, function(err, apiResponse) {});
  653. *
  654. * //-
  655. * // Enable versioning for your bucket.
  656. * //-
  657. * bucket.setMetadata({
  658. * versioning: {
  659. * enabled: true
  660. * }
  661. * }, function(err, apiResponse) {});
  662. *
  663. * //-
  664. * // Enable KMS encryption for objects within this bucket.
  665. * //-
  666. * bucket.setMetadata({
  667. * encryption: {
  668. * defaultKmsKeyName: 'projects/grape-spaceship-123/...'
  669. * }
  670. * }, function(err, apiResponse) {});
  671. *
  672. * //-
  673. * // Set the default event-based hold value for new objects in this
  674. * // bucket.
  675. * //-
  676. * bucket.setMetadata({
  677. * defaultEventBasedHold: true
  678. * }, function(err, apiResponse) {});
  679. *
  680. * //-
  681. * // Remove object lifecycle rules.
  682. * //-
  683. * bucket.setMetadata({
  684. * lifecycle: null
  685. * }, function(err, apiResponse) {});
  686. *
  687. * //-
  688. * // If the callback is omitted, we'll return a Promise.
  689. * //-
  690. * bucket.setMetadata(metadata).then(function(data) {
  691. * const apiResponse = data[0];
  692. * });
  693. * ```
  694. */
  695. setMetadata: {
  696. reqOpts: {
  697. qs: requestQueryObject,
  698. },
  699. },
  700. };
  701. super({
  702. parent: storage,
  703. baseUrl: '/b',
  704. id: name,
  705. createMethod: storage.createBucket.bind(storage),
  706. methods,
  707. });
  708. this.name = name;
  709. this.storage = storage;
  710. this.userProject = options.userProject;
  711. this.acl = new Acl({
  712. request: this.request.bind(this),
  713. pathPrefix: '/acl',
  714. });
  715. this.acl.default = new Acl({
  716. request: this.request.bind(this),
  717. pathPrefix: '/defaultObjectAcl',
  718. });
  719. this.crc32cGenerator =
  720. options.crc32cGenerator || this.storage.crc32cGenerator;
  721. this.iam = new Iam(this);
  722. this.getFilesStream = paginator.streamify('getFiles');
  723. this.instanceRetryValue = storage.retryOptions.autoRetry;
  724. this.instancePreconditionOpts = options === null || options === void 0 ? void 0 : options.preconditionOpts;
  725. }
  726. /**
  727. * The bucket's Cloud Storage URI (`gs://`)
  728. *
  729. * @example
  730. * ```ts
  731. * const {Storage} = require('@google-cloud/storage');
  732. * const storage = new Storage();
  733. * const bucket = storage.bucket('my-bucket');
  734. *
  735. * // `gs://my-bucket`
  736. * const href = bucket.cloudStorageURI.href;
  737. * ```
  738. */
  739. get cloudStorageURI() {
  740. const uri = new URL('gs://');
  741. uri.host = this.name;
  742. return uri;
  743. }
  744. /**
  745. * @typedef {object} AddLifecycleRuleOptions Configuration options for Bucket#addLifecycleRule().
  746. * @property {boolean} [append=true] The new rules will be appended to any
  747. * pre-existing rules.
  748. */
  749. /**
  750. *
  751. * @typedef {object} LifecycleRule The new lifecycle rule to be added to objects
  752. * in this bucket.
  753. * @property {string|object} action The action to be taken upon matching of
  754. * all the conditions 'delete', 'setStorageClass', or 'AbortIncompleteMultipartUpload'.
  755. * **Note**: For configuring a raw-formatted rule object to be passed as `action`
  756. * please refer to the [examples]{@link https://cloud.google.com/storage/docs/managing-lifecycles#configexamples}.
  757. * @property {object} condition Condition a bucket must meet before the
  758. * action occurs on the bucket. Refer to following supported [conditions]{@link https://cloud.google.com/storage/docs/lifecycle#conditions}.
  759. * @property {string} [storageClass] When using the `setStorageClass`
  760. * action, provide this option to dictate which storage class the object
  761. * should update to. Please see
  762. * [SetStorageClass option documentation]{@link https://cloud.google.com/storage/docs/lifecycle#setstorageclass} for supported transitions.
  763. */
  764. /**
  765. * Add an object lifecycle management rule to the bucket.
  766. *
  767. * By default, an Object Lifecycle Management rule provided to this method
  768. * will be included to the existing policy. To replace all existing rules,
  769. * supply the `options` argument, setting `append` to `false`.
  770. *
  771. * To add multiple rules, pass a list to the `rule` parameter. Calling this
  772. * function multiple times asynchronously does not guarantee that all rules
  773. * are added correctly.
  774. *
  775. * See {@link https://cloud.google.com/storage/docs/lifecycle| Object Lifecycle Management}
  776. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/patch| Buckets: patch API Documentation}
  777. *
  778. * @param {LifecycleRule|LifecycleRule[]} rule The new lifecycle rule or rules to be added to objects
  779. * in this bucket.
  780. * @param {string|object} rule.action The action to be taken upon matching of
  781. * all the conditions 'delete', 'setStorageClass', or 'AbortIncompleteMultipartUpload'.
  782. * **Note**: For configuring a raw-formatted rule object to be passed as `action`
  783. * please refer to the [examples]{@link https://cloud.google.com/storage/docs/managing-lifecycles#configexamples}.
  784. * @param {object} rule.condition Condition a bucket must meet before the
  785. * action occurson the bucket. Refer to followitn supported [conditions]{@link https://cloud.google.com/storage/docs/lifecycle#conditions}.
  786. * @param {string} [rule.storageClass] When using the `setStorageClass`
  787. * action, provide this option to dictate which storage class the object
  788. * should update to.
  789. * @param {AddLifecycleRuleOptions} [options] Configuration object.
  790. * @param {boolean} [options.append=true] Append the new rule to the existing
  791. * policy.
  792. * @param {SetBucketMetadataCallback} [callback] Callback function.
  793. * @returns {Promise<SetBucketMetadataResponse>}
  794. *
  795. * @example
  796. * ```
  797. * const {Storage} = require('@google-cloud/storage');
  798. * const storage = new Storage();
  799. * const bucket = storage.bucket('albums');
  800. *
  801. * //-
  802. * // Automatically have an object deleted from this bucket once it is 3 years
  803. * // of age.
  804. * //-
  805. * bucket.addLifecycleRule({
  806. * action: 'delete',
  807. * condition: {
  808. * age: 365 * 3 // Specified in days.
  809. * }
  810. * }, function(err, apiResponse) {
  811. * if (err) {
  812. * // Error handling omitted.
  813. * }
  814. *
  815. * const lifecycleRules = bucket.metadata.lifecycle.rule;
  816. *
  817. * // Iterate over the Object Lifecycle Management rules on this bucket.
  818. * lifecycleRules.forEach(lifecycleRule => {});
  819. * });
  820. *
  821. * //-
  822. * // By default, the rule you provide will be added to the existing policy.
  823. * // Optionally, you can disable this behavior to replace all of the
  824. * // pre-existing rules.
  825. * //-
  826. * const options = {
  827. * append: false
  828. * };
  829. *
  830. * bucket.addLifecycleRule({
  831. * action: 'delete',
  832. * condition: {
  833. * age: 365 * 3 // Specified in days.
  834. * }
  835. * }, options, function(err, apiResponse) {
  836. * if (err) {
  837. * // Error handling omitted.
  838. * }
  839. *
  840. * // All rules have been replaced with the new "delete" rule.
  841. *
  842. * // Iterate over the Object Lifecycle Management rules on this bucket.
  843. * lifecycleRules.forEach(lifecycleRule => {});
  844. * });
  845. *
  846. * //-
  847. * // For objects created before 2018, "downgrade" the storage class.
  848. * //-
  849. * bucket.addLifecycleRule({
  850. * action: 'setStorageClass',
  851. * storageClass: 'COLDLINE',
  852. * condition: {
  853. * createdBefore: new Date('2018')
  854. * }
  855. * }, function(err, apiResponse) {});
  856. *
  857. * //-
  858. * // Delete objects created before 2016 which have the Coldline storage
  859. * // class.
  860. * //-
  861. * bucket.addLifecycleRule({
  862. * action: 'delete',
  863. * condition: {
  864. * matchesStorageClass: [
  865. * 'COLDLINE'
  866. * ],
  867. * createdBefore: new Date('2016')
  868. * }
  869. * }, function(err, apiResponse) {});
  870. *
  871. * //-
  872. * // Delete object that has a noncurrent timestamp that is at least 100 days.
  873. * //-
  874. * bucket.addLifecycleRule({
  875. * action: 'delete',
  876. * condition: {
  877. * daysSinceNoncurrentTime: 100
  878. * }
  879. * }, function(err, apiResponse) {});
  880. *
  881. * //-
  882. * // Delete object that has a noncurrent timestamp before 2020-01-01.
  883. * //-
  884. * bucket.addLifecycleRule({
  885. * action: 'delete',
  886. * condition: {
  887. * noncurrentTimeBefore: new Date('2020-01-01')
  888. * }
  889. * }, function(err, apiResponse) {});
  890. *
  891. * //-
  892. * // Delete object that has a customTime that is at least 100 days.
  893. * //-
  894. * bucket.addLifecycleRule({
  895. * action: 'delete',
  896. * condition: {
  897. * daysSinceCustomTime: 100
  898. * }
  899. * }, function(err, apiResponse) ());
  900. *
  901. * //-
  902. * // Delete object that has a customTime before 2020-01-01.
  903. * //-
  904. * bucket.addLifecycleRule({
  905. * action: 'delete',
  906. * condition: {
  907. * customTimeBefore: new Date('2020-01-01')
  908. * }
  909. * }, function(err, apiResponse) {});
  910. * ```
  911. */
  912. addLifecycleRule(rule, optionsOrCallback, callback) {
  913. let options = {};
  914. if (typeof optionsOrCallback === 'function') {
  915. callback = optionsOrCallback;
  916. }
  917. else if (optionsOrCallback) {
  918. options = optionsOrCallback;
  919. }
  920. options = options || {};
  921. const rules = Array.isArray(rule) ? rule : [rule];
  922. for (const curRule of rules) {
  923. if (curRule.condition.createdBefore instanceof Date) {
  924. curRule.condition.createdBefore = curRule.condition.createdBefore
  925. .toISOString()
  926. .replace(/T.+$/, '');
  927. }
  928. if (curRule.condition.customTimeBefore instanceof Date) {
  929. curRule.condition.customTimeBefore = curRule.condition.customTimeBefore
  930. .toISOString()
  931. .replace(/T.+$/, '');
  932. }
  933. if (curRule.condition.noncurrentTimeBefore instanceof Date) {
  934. curRule.condition.noncurrentTimeBefore =
  935. curRule.condition.noncurrentTimeBefore
  936. .toISOString()
  937. .replace(/T.+$/, '');
  938. }
  939. }
  940. if (options.append === false) {
  941. this.setMetadata({ lifecycle: { rule: rules } }, options, callback);
  942. return;
  943. }
  944. // The default behavior appends the previously-defined lifecycle rules with
  945. // the new ones just passed in by the user.
  946. this.getMetadata((err, metadata) => {
  947. var _a, _b;
  948. if (err) {
  949. callback(err);
  950. return;
  951. }
  952. const currentLifecycleRules = Array.isArray((_a = metadata.lifecycle) === null || _a === void 0 ? void 0 : _a.rule)
  953. ? (_b = metadata.lifecycle) === null || _b === void 0 ? void 0 : _b.rule
  954. : [];
  955. this.setMetadata({
  956. lifecycle: { rule: currentLifecycleRules.concat(rules) },
  957. }, options, callback);
  958. });
  959. }
  960. /**
  961. * @typedef {object} CombineOptions
  962. * @property {string} [kmsKeyName] Resource name of the Cloud KMS key, of
  963. * the form
  964. * `projects/my-project/locations/location/keyRings/my-kr/cryptoKeys/my-key`,
  965. * that will be used to encrypt the object. Overwrites the object
  966. * metadata's `kms_key_name` value, if any.
  967. * @property {string} [userProject] The ID of the project which will be
  968. * billed for the request.
  969. */
  970. /**
  971. * @callback CombineCallback
  972. * @param {?Error} err Request error, if any.
  973. * @param {File} newFile The new {@link File}.
  974. * @param {object} apiResponse The full API response.
  975. */
  976. /**
  977. * @typedef {array} CombineResponse
  978. * @property {File} 0 The new {@link File}.
  979. * @property {object} 1 The full API response.
  980. */
  981. /**
  982. * Combine multiple files into one new file.
  983. *
  984. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/compose| Objects: compose API Documentation}
  985. *
  986. * @throws {Error} if a non-array is provided as sources argument.
  987. * @throws {Error} if no sources are provided.
  988. * @throws {Error} if no destination is provided.
  989. *
  990. * @param {string[]|File[]} sources The source files that will be
  991. * combined.
  992. * @param {string|File} destination The file you would like the
  993. * source files combined into.
  994. * @param {CombineOptions} [options] Configuration options.
  995. * @param {string} [options.kmsKeyName] Resource name of the Cloud KMS key, of
  996. * the form
  997. * `projects/my-project/locations/location/keyRings/my-kr/cryptoKeys/my-key`,
  998. * that will be used to encrypt the object. Overwrites the object
  999. * metadata's `kms_key_name` value, if any.
  1000. * @param {string} [options.userProject] The ID of the project which will be
  1001. * billed for the request.
  1002. * @param {CombineCallback} [callback] Callback function.
  1003. * @returns {Promise<CombineResponse>}
  1004. *
  1005. * @example
  1006. * ```
  1007. * const logBucket = storage.bucket('log-bucket');
  1008. *
  1009. * const sources = [
  1010. * logBucket.file('2013-logs.txt'),
  1011. * logBucket.file('2014-logs.txt')
  1012. * ];
  1013. *
  1014. * const allLogs = logBucket.file('all-logs.txt');
  1015. *
  1016. * logBucket.combine(sources, allLogs, function(err, newFile, apiResponse) {
  1017. * // newFile === allLogs
  1018. * });
  1019. *
  1020. * //-
  1021. * // If the callback is omitted, we'll return a Promise.
  1022. * //-
  1023. * logBucket.combine(sources, allLogs).then(function(data) {
  1024. * const newFile = data[0];
  1025. * const apiResponse = data[1];
  1026. * });
  1027. * ```
  1028. */
  1029. combine(sources, destination, optionsOrCallback, callback) {
  1030. var _a;
  1031. if (!Array.isArray(sources) || sources.length === 0) {
  1032. throw new Error(BucketExceptionMessages.PROVIDE_SOURCE_FILE);
  1033. }
  1034. if (!destination) {
  1035. throw new Error(BucketExceptionMessages.DESTINATION_FILE_NOT_SPECIFIED);
  1036. }
  1037. let options = {};
  1038. if (typeof optionsOrCallback === 'function') {
  1039. callback = optionsOrCallback;
  1040. }
  1041. else if (optionsOrCallback) {
  1042. options = optionsOrCallback;
  1043. }
  1044. this.disableAutoRetryConditionallyIdempotent_(this.methods.setMetadata, // Not relevant but param is required
  1045. AvailableServiceObjectMethods.setMetadata, // Same as above
  1046. options);
  1047. const convertToFile = (file) => {
  1048. if (file instanceof File) {
  1049. return file;
  1050. }
  1051. return this.file(file);
  1052. };
  1053. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1054. sources = sources.map(convertToFile);
  1055. const destinationFile = convertToFile(destination);
  1056. callback = callback || util.noop;
  1057. if (!destinationFile.metadata.contentType) {
  1058. const destinationContentType = mime.getType(destinationFile.name) || undefined;
  1059. if (destinationContentType) {
  1060. destinationFile.metadata.contentType = destinationContentType;
  1061. }
  1062. }
  1063. let maxRetries = this.storage.retryOptions.maxRetries;
  1064. if ((((_a = destinationFile === null || destinationFile === void 0 ? void 0 : destinationFile.instancePreconditionOpts) === null || _a === void 0 ? void 0 : _a.ifGenerationMatch) ===
  1065. undefined &&
  1066. options.ifGenerationMatch === undefined &&
  1067. this.storage.retryOptions.idempotencyStrategy ===
  1068. IdempotencyStrategy.RetryConditional) ||
  1069. this.storage.retryOptions.idempotencyStrategy ===
  1070. IdempotencyStrategy.RetryNever) {
  1071. maxRetries = 0;
  1072. }
  1073. if (options.ifGenerationMatch === undefined) {
  1074. Object.assign(options, destinationFile.instancePreconditionOpts, options);
  1075. }
  1076. // Make the request from the destination File object.
  1077. destinationFile.request({
  1078. method: 'POST',
  1079. uri: '/compose',
  1080. maxRetries,
  1081. json: {
  1082. destination: {
  1083. contentType: destinationFile.metadata.contentType,
  1084. contentEncoding: destinationFile.metadata.contentEncoding,
  1085. },
  1086. sourceObjects: sources.map(source => {
  1087. const sourceObject = {
  1088. name: source.name,
  1089. };
  1090. if (source.metadata && source.metadata.generation) {
  1091. sourceObject.generation = parseInt(source.metadata.generation.toString());
  1092. }
  1093. return sourceObject;
  1094. }),
  1095. },
  1096. qs: options,
  1097. }, (err, resp) => {
  1098. this.storage.retryOptions.autoRetry = this.instanceRetryValue;
  1099. if (err) {
  1100. callback(err, null, resp);
  1101. return;
  1102. }
  1103. callback(null, destinationFile, resp);
  1104. });
  1105. }
  1106. /**
  1107. * See a {@link https://cloud.google.com/storage/docs/json_api/v1/objects/watchAll| Objects: watchAll request body}.
  1108. *
  1109. * @typedef {object} CreateChannelConfig
  1110. * @property {string} address The address where notifications are
  1111. * delivered for this channel.
  1112. * @property {string} [delimiter] Returns results in a directory-like mode.
  1113. * @property {number} [maxResults] Maximum number of `items` plus `prefixes`
  1114. * to return in a single page of responses.
  1115. * @property {string} [pageToken] A previously-returned page token
  1116. * representing part of the larger set of results to view.
  1117. * @property {string} [prefix] Filter results to objects whose names begin
  1118. * with this prefix.
  1119. * @property {string} [projection=noAcl] Set of properties to return.
  1120. * @property {string} [userProject] The ID of the project which will be
  1121. * billed for the request.
  1122. * @property {boolean} [versions=false] If `true`, lists all versions of an object
  1123. * as distinct results.
  1124. */
  1125. /**
  1126. * @typedef {object} CreateChannelOptions
  1127. * @property {string} [userProject] The ID of the project which will be
  1128. * billed for the request.
  1129. */
  1130. /**
  1131. * @typedef {array} CreateChannelResponse
  1132. * @property {Channel} 0 The new {@link Channel}.
  1133. * @property {object} 1 The full API response.
  1134. */
  1135. /**
  1136. * @callback CreateChannelCallback
  1137. * @param {?Error} err Request error, if any.
  1138. * @param {Channel} channel The new {@link Channel}.
  1139. * @param {object} apiResponse The full API response.
  1140. */
  1141. /**
  1142. * Create a channel that will be notified when objects in this bucket changes.
  1143. *
  1144. * @throws {Error} If an ID is not provided.
  1145. * @throws {Error} If an address is not provided.
  1146. *
  1147. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/watchAll| Objects: watchAll API Documentation}
  1148. *
  1149. * @param {string} id The ID of the channel to create.
  1150. * @param {CreateChannelConfig} config Configuration for creating channel.
  1151. * @param {string} config.address The address where notifications are
  1152. * delivered for this channel.
  1153. * @param {string} [config.delimiter] Returns results in a directory-like mode.
  1154. * @param {number} [config.maxResults] Maximum number of `items` plus `prefixes`
  1155. * to return in a single page of responses.
  1156. * @param {string} [config.pageToken] A previously-returned page token
  1157. * representing part of the larger set of results to view.
  1158. * @param {string} [config.prefix] Filter results to objects whose names begin
  1159. * with this prefix.
  1160. * @param {string} [config.projection=noAcl] Set of properties to return.
  1161. * @param {string} [config.userProject] The ID of the project which will be
  1162. * billed for the request.
  1163. * @param {boolean} [config.versions=false] If `true`, lists all versions of an object
  1164. * as distinct results.
  1165. * @param {CreateChannelOptions} [options] Configuration options.
  1166. * @param {string} [options.userProject] The ID of the project which will be
  1167. * billed for the request.
  1168. * @param {CreateChannelCallback} [callback] Callback function.
  1169. * @returns {Promise<CreateChannelResponse>}
  1170. *
  1171. * @example
  1172. * ```
  1173. * const {Storage} = require('@google-cloud/storage');
  1174. * const storage = new Storage();
  1175. * const bucket = storage.bucket('albums');
  1176. * const id = 'new-channel-id';
  1177. *
  1178. * const config = {
  1179. * address: 'https://...'
  1180. * };
  1181. *
  1182. * bucket.createChannel(id, config, function(err, channel, apiResponse) {
  1183. * if (!err) {
  1184. * // Channel created successfully.
  1185. * }
  1186. * });
  1187. *
  1188. * //-
  1189. * // If the callback is omitted, we'll return a Promise.
  1190. * //-
  1191. * bucket.createChannel(id, config).then(function(data) {
  1192. * const channel = data[0];
  1193. * const apiResponse = data[1];
  1194. * });
  1195. * ```
  1196. */
  1197. createChannel(id, config, optionsOrCallback, callback) {
  1198. if (typeof id !== 'string') {
  1199. throw new Error(BucketExceptionMessages.CHANNEL_ID_REQUIRED);
  1200. }
  1201. let options = {};
  1202. if (typeof optionsOrCallback === 'function') {
  1203. callback = optionsOrCallback;
  1204. }
  1205. else if (optionsOrCallback) {
  1206. options = optionsOrCallback;
  1207. }
  1208. this.request({
  1209. method: 'POST',
  1210. uri: '/o/watch',
  1211. json: Object.assign({
  1212. id,
  1213. type: 'web_hook',
  1214. }, config),
  1215. qs: options,
  1216. }, (err, apiResponse) => {
  1217. if (err) {
  1218. callback(err, null, apiResponse);
  1219. return;
  1220. }
  1221. const resourceId = apiResponse.resourceId;
  1222. const channel = this.storage.channel(id, resourceId);
  1223. channel.metadata = apiResponse;
  1224. callback(null, channel, apiResponse);
  1225. });
  1226. }
  1227. /**
  1228. * Metadata to set for the Notification.
  1229. *
  1230. * @typedef {object} CreateNotificationOptions
  1231. * @property {object} [customAttributes] An optional list of additional
  1232. * attributes to attach to each Cloud PubSub message published for this
  1233. * notification subscription.
  1234. * @property {string[]} [eventTypes] If present, only send notifications about
  1235. * listed event types. If empty, sent notifications for all event types.
  1236. * @property {string} [objectNamePrefix] If present, only apply this
  1237. * notification configuration to object names that begin with this prefix.
  1238. * @property {string} [payloadFormat] The desired content of the Payload.
  1239. * Defaults to `JSON_API_V1`.
  1240. *
  1241. * Acceptable values are:
  1242. * - `JSON_API_V1`
  1243. *
  1244. * - `NONE`
  1245. * @property {string} [userProject] The ID of the project which will be
  1246. * billed for the request.
  1247. */
  1248. /**
  1249. * @callback CreateNotificationCallback
  1250. * @param {?Error} err Request error, if any.
  1251. * @param {Notification} notification The new {@link Notification}.
  1252. * @param {object} apiResponse The full API response.
  1253. */
  1254. /**
  1255. * @typedef {array} CreateNotificationResponse
  1256. * @property {Notification} 0 The new {@link Notification}.
  1257. * @property {object} 1 The full API response.
  1258. */
  1259. /**
  1260. * Creates a notification subscription for the bucket.
  1261. *
  1262. * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/insert| Notifications: insert}
  1263. *
  1264. * @param {Topic|string} topic The Cloud PubSub topic to which this
  1265. * subscription publishes. If the project ID is omitted, the current
  1266. * project ID will be used.
  1267. *
  1268. * Acceptable formats are:
  1269. * - `projects/grape-spaceship-123/topics/my-topic`
  1270. *
  1271. * - `my-topic`
  1272. * @param {CreateNotificationOptions} [options] Metadata to set for the
  1273. * notification.
  1274. * @param {object} [options.customAttributes] An optional list of additional
  1275. * attributes to attach to each Cloud PubSub message published for this
  1276. * notification subscription.
  1277. * @param {string[]} [options.eventTypes] If present, only send notifications about
  1278. * listed event types. If empty, sent notifications for all event types.
  1279. * @param {string} [options.objectNamePrefix] If present, only apply this
  1280. * notification configuration to object names that begin with this prefix.
  1281. * @param {string} [options.payloadFormat] The desired content of the Payload.
  1282. * Defaults to `JSON_API_V1`.
  1283. *
  1284. * Acceptable values are:
  1285. * - `JSON_API_V1`
  1286. *
  1287. * - `NONE`
  1288. * @param {string} [options.userProject] The ID of the project which will be
  1289. * billed for the request.
  1290. * @param {CreateNotificationCallback} [callback] Callback function.
  1291. * @returns {Promise<CreateNotificationResponse>}
  1292. * @throws {Error} If a valid topic is not provided.
  1293. * @see Notification#create
  1294. *
  1295. * @example
  1296. * ```
  1297. * const {Storage} = require('@google-cloud/storage');
  1298. * const storage = new Storage();
  1299. * const myBucket = storage.bucket('my-bucket');
  1300. *
  1301. * const callback = function(err, notification, apiResponse) {
  1302. * if (!err) {
  1303. * // The notification was created successfully.
  1304. * }
  1305. * };
  1306. *
  1307. * myBucket.createNotification('my-topic', callback);
  1308. *
  1309. * //-
  1310. * // Configure the nofiication by providing Notification metadata.
  1311. * //-
  1312. * const metadata = {
  1313. * objectNamePrefix: 'prefix-'
  1314. * };
  1315. *
  1316. * myBucket.createNotification('my-topic', metadata, callback);
  1317. *
  1318. * //-
  1319. * // If the callback is omitted, we'll return a Promise.
  1320. * //-
  1321. * myBucket.createNotification('my-topic').then(function(data) {
  1322. * const notification = data[0];
  1323. * const apiResponse = data[1];
  1324. * });
  1325. *
  1326. * ```
  1327. * @example <caption>include:samples/createNotification.js</caption>
  1328. * region_tag:storage_create_bucket_notifications
  1329. * Another example:
  1330. */
  1331. createNotification(topic, optionsOrCallback, callback) {
  1332. let options = {};
  1333. if (typeof optionsOrCallback === 'function') {
  1334. callback = optionsOrCallback;
  1335. }
  1336. else if (optionsOrCallback) {
  1337. options = optionsOrCallback;
  1338. }
  1339. const topicIsObject = topic !== null && typeof topic === 'object';
  1340. if (topicIsObject && util.isCustomType(topic, 'pubsub/topic')) {
  1341. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1342. topic = topic.name;
  1343. }
  1344. if (typeof topic !== 'string') {
  1345. throw new Error(BucketExceptionMessages.TOPIC_NAME_REQUIRED);
  1346. }
  1347. const body = Object.assign({ topic }, options);
  1348. if (body.topic.indexOf('projects') !== 0) {
  1349. body.topic = 'projects/{{projectId}}/topics/' + body.topic;
  1350. }
  1351. body.topic = `//pubsub.${this.storage.universeDomain}/` + body.topic;
  1352. if (!body.payloadFormat) {
  1353. body.payloadFormat = 'JSON_API_V1';
  1354. }
  1355. const query = {};
  1356. if (body.userProject) {
  1357. query.userProject = body.userProject;
  1358. delete body.userProject;
  1359. }
  1360. this.request({
  1361. method: 'POST',
  1362. uri: '/notificationConfigs',
  1363. json: convertObjKeysToSnakeCase(body),
  1364. qs: query,
  1365. maxRetries: 0, //explicitly set this value since this is a non-idempotent function
  1366. }, (err, apiResponse) => {
  1367. if (err) {
  1368. callback(err, null, apiResponse);
  1369. return;
  1370. }
  1371. const notification = this.notification(apiResponse.id);
  1372. notification.metadata = apiResponse;
  1373. callback(null, notification, apiResponse);
  1374. });
  1375. }
  1376. /**
  1377. * @typedef {object} DeleteFilesOptions Query object. See {@link Bucket#getFiles}
  1378. * for all of the supported properties.
  1379. * @property {boolean} [force] Suppress errors until all files have been
  1380. * processed.
  1381. */
  1382. /**
  1383. * @callback DeleteFilesCallback
  1384. * @param {?Error|?Error[]} err Request error, if any, or array of errors from
  1385. * files that were not able to be deleted.
  1386. * @param {object} [apiResponse] The full API response.
  1387. */
  1388. /**
  1389. * Iterate over the bucket's files, calling `file.delete()` on each.
  1390. *
  1391. * <strong>This is not an atomic request.</strong> A delete attempt will be
  1392. * made for each file individually. Any one can fail, in which case only a
  1393. * portion of the files you intended to be deleted would have.
  1394. *
  1395. * Operations are performed in parallel, up to 10 at once. The first error
  1396. * breaks the loop and will execute the provided callback with it. Specify
  1397. * `{ force: true }` to suppress the errors until all files have had a chance
  1398. * to be processed.
  1399. *
  1400. * File preconditions cannot be passed to this function. It will not retry unless
  1401. * the idempotency strategy is set to retry always.
  1402. *
  1403. * The `query` object passed as the first argument will also be passed to
  1404. * {@link Bucket#getFiles}.
  1405. *
  1406. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/delete| Objects: delete API Documentation}
  1407. *
  1408. * @param {DeleteFilesOptions} [query] Query object. See {@link Bucket#getFiles}
  1409. * @param {boolean} [query.force] Suppress errors until all files have been
  1410. * processed.
  1411. * @param {DeleteFilesCallback} [callback] Callback function.
  1412. * @returns {Promise}
  1413. *
  1414. * @example
  1415. * ```
  1416. * const {Storage} = require('@google-cloud/storage');
  1417. * const storage = new Storage();
  1418. * const bucket = storage.bucket('albums');
  1419. *
  1420. * //-
  1421. * // Delete all of the files in the bucket.
  1422. * //-
  1423. * bucket.deleteFiles(function(err) {});
  1424. *
  1425. * //-
  1426. * // By default, if a file cannot be deleted, this method will stop deleting
  1427. * // files from your bucket. You can override this setting with `force:
  1428. * // true`.
  1429. * //-
  1430. * bucket.deleteFiles({
  1431. * force: true
  1432. * }, function(errors) {
  1433. * // `errors`:
  1434. * // Array of errors if any occurred, otherwise null.
  1435. * });
  1436. *
  1437. * //-
  1438. * // The first argument to this method acts as a query to
  1439. * // {@link Bucket#getFiles}. As an example, you can delete files
  1440. * // which match a prefix.
  1441. * //-
  1442. * bucket.deleteFiles({
  1443. * prefix: 'images/'
  1444. * }, function(err) {
  1445. * if (!err) {
  1446. * // All files in the `images` directory have been deleted.
  1447. * }
  1448. * });
  1449. *
  1450. * //-
  1451. * // If the callback is omitted, we'll return a Promise.
  1452. * //-
  1453. * bucket.deleteFiles().then(function() {});
  1454. * ```
  1455. */
  1456. deleteFiles(queryOrCallback, callback) {
  1457. let query = {};
  1458. if (typeof queryOrCallback === 'function') {
  1459. callback = queryOrCallback;
  1460. }
  1461. else if (queryOrCallback) {
  1462. query = queryOrCallback;
  1463. }
  1464. const MAX_PARALLEL_LIMIT = 10;
  1465. const MAX_QUEUE_SIZE = 1000;
  1466. const errors = [];
  1467. const deleteFile = (file) => {
  1468. return file.delete(query).catch(err => {
  1469. if (!query.force) {
  1470. throw err;
  1471. }
  1472. errors.push(err);
  1473. });
  1474. };
  1475. (async () => {
  1476. try {
  1477. let promises = [];
  1478. const limit = pLimit(MAX_PARALLEL_LIMIT);
  1479. const filesStream = this.getFilesStream(query);
  1480. for await (const curFile of filesStream) {
  1481. if (promises.length >= MAX_QUEUE_SIZE) {
  1482. await Promise.all(promises);
  1483. promises = [];
  1484. }
  1485. promises.push(limit(() => deleteFile(curFile)).catch(e => {
  1486. filesStream.destroy();
  1487. throw e;
  1488. }));
  1489. }
  1490. await Promise.all(promises);
  1491. callback(errors.length > 0 ? errors : null);
  1492. }
  1493. catch (e) {
  1494. callback(e);
  1495. return;
  1496. }
  1497. })();
  1498. }
  1499. /**
  1500. * @deprecated
  1501. * @typedef {array} DeleteLabelsResponse
  1502. * @property {object} 0 The full API response.
  1503. */
  1504. /**
  1505. * @deprecated
  1506. * @callback DeleteLabelsCallback
  1507. * @param {?Error} err Request error, if any.
  1508. * @param {object} metadata Bucket's metadata.
  1509. */
  1510. /**
  1511. * @deprecated Use setMetadata directly
  1512. * Delete one or more labels from this bucket.
  1513. *
  1514. * @param {string|string[]} [labels] The labels to delete. If no labels are
  1515. * provided, all of the labels are removed.
  1516. * @param {DeleteLabelsCallback} [callback] Callback function.
  1517. * @param {DeleteLabelsOptions} [options] Options, including precondition options
  1518. * @returns {Promise<DeleteLabelsResponse>}
  1519. *
  1520. * @example
  1521. * ```
  1522. * const {Storage} = require('@google-cloud/storage');
  1523. * const storage = new Storage();
  1524. * const bucket = storage.bucket('albums');
  1525. *
  1526. * //-
  1527. * // Delete all of the labels from this bucket.
  1528. * //-
  1529. * bucket.deleteLabels(function(err, apiResponse) {});
  1530. *
  1531. * //-
  1532. * // Delete a single label.
  1533. * //-
  1534. * bucket.deleteLabels('labelone', function(err, apiResponse) {});
  1535. *
  1536. * //-
  1537. * // Delete a specific set of labels.
  1538. * //-
  1539. * bucket.deleteLabels([
  1540. * 'labelone',
  1541. * 'labeltwo'
  1542. * ], function(err, apiResponse) {});
  1543. *
  1544. * //-
  1545. * // If the callback is omitted, we'll return a Promise.
  1546. * //-
  1547. * bucket.deleteLabels().then(function(data) {
  1548. * const apiResponse = data[0];
  1549. * });
  1550. * ```
  1551. */
  1552. deleteLabels(labelsOrCallbackOrOptions, optionsOrCallback, callback) {
  1553. let labels = new Array();
  1554. let options = {};
  1555. if (typeof labelsOrCallbackOrOptions === 'function') {
  1556. callback = labelsOrCallbackOrOptions;
  1557. }
  1558. else if (typeof labelsOrCallbackOrOptions === 'string') {
  1559. labels = [labelsOrCallbackOrOptions];
  1560. }
  1561. else if (Array.isArray(labelsOrCallbackOrOptions)) {
  1562. labels = labelsOrCallbackOrOptions;
  1563. }
  1564. else if (labelsOrCallbackOrOptions) {
  1565. options = labelsOrCallbackOrOptions;
  1566. }
  1567. if (typeof optionsOrCallback === 'function') {
  1568. callback = optionsOrCallback;
  1569. }
  1570. else if (optionsOrCallback) {
  1571. options = optionsOrCallback;
  1572. }
  1573. const deleteLabels = (labels) => {
  1574. const nullLabelMap = labels.reduce((nullLabelMap, labelKey) => {
  1575. nullLabelMap[labelKey] = null;
  1576. return nullLabelMap;
  1577. }, {});
  1578. if ((options === null || options === void 0 ? void 0 : options.ifMetagenerationMatch) !== undefined) {
  1579. this.setLabels(nullLabelMap, options, callback);
  1580. }
  1581. else {
  1582. this.setLabels(nullLabelMap, callback);
  1583. }
  1584. };
  1585. if (labels.length === 0) {
  1586. this.getLabels((err, labels) => {
  1587. if (err) {
  1588. callback(err);
  1589. return;
  1590. }
  1591. deleteLabels(Object.keys(labels));
  1592. });
  1593. }
  1594. else {
  1595. deleteLabels(labels);
  1596. }
  1597. }
  1598. /**
  1599. * @typedef {array} DisableRequesterPaysResponse
  1600. * @property {object} 0 The full API response.
  1601. */
  1602. /**
  1603. * @callback DisableRequesterPaysCallback
  1604. * @param {?Error} err Request error, if any.
  1605. * @param {object} apiResponse The full API response.
  1606. */
  1607. /**
  1608. * <div class="notice">
  1609. * <strong>Early Access Testers Only</strong>
  1610. * <p>
  1611. * This feature is not yet widely-available.
  1612. * </p>
  1613. * </div>
  1614. *
  1615. * Disable `requesterPays` functionality from this bucket.
  1616. *
  1617. * @param {DisableRequesterPaysCallback} [callback] Callback function.
  1618. * @param {DisableRequesterPaysOptions} [options] Options, including precondition options
  1619. * @returns {Promise<DisableRequesterPaysCallback>}
  1620. *
  1621. * @example
  1622. * ```
  1623. * const {Storage} = require('@google-cloud/storage');
  1624. * const storage = new Storage();
  1625. * const bucket = storage.bucket('albums');
  1626. *
  1627. * bucket.disableRequesterPays(function(err, apiResponse) {
  1628. * if (!err) {
  1629. * // requesterPays functionality disabled successfully.
  1630. * }
  1631. * });
  1632. *
  1633. * //-
  1634. * // If the callback is omitted, we'll return a Promise.
  1635. * //-
  1636. * bucket.disableRequesterPays().then(function(data) {
  1637. * const apiResponse = data[0];
  1638. * });
  1639. *
  1640. * ```
  1641. * @example <caption>include:samples/requesterPays.js</caption>
  1642. * region_tag:storage_disable_requester_pays
  1643. * Example of disabling requester pays:
  1644. */
  1645. disableRequesterPays(optionsOrCallback, callback) {
  1646. let options = {};
  1647. if (typeof optionsOrCallback === 'function') {
  1648. callback = optionsOrCallback;
  1649. }
  1650. else if (optionsOrCallback) {
  1651. options = optionsOrCallback;
  1652. }
  1653. this.setMetadata({
  1654. billing: {
  1655. requesterPays: false,
  1656. },
  1657. }, options, callback);
  1658. }
  1659. /**
  1660. * Configuration object for enabling logging.
  1661. *
  1662. * @typedef {object} EnableLoggingOptions
  1663. * @property {string|Bucket} [bucket] The bucket for the log entries. By
  1664. * default, the current bucket is used.
  1665. * @property {string} prefix A unique prefix for log object names.
  1666. */
  1667. /**
  1668. * Enable logging functionality for this bucket. This will make two API
  1669. * requests, first to grant Cloud Storage WRITE permission to the bucket, then
  1670. * to set the appropriate configuration on the Bucket's metadata.
  1671. *
  1672. * @param {EnableLoggingOptions} config Configuration options.
  1673. * @param {string|Bucket} [config.bucket] The bucket for the log entries. By
  1674. * default, the current bucket is used.
  1675. * @param {string} config.prefix A unique prefix for log object names.
  1676. * @param {SetBucketMetadataCallback} [callback] Callback function.
  1677. * @returns {Promise<SetBucketMetadataResponse>}
  1678. *
  1679. * @example
  1680. * ```
  1681. * const {Storage} = require('@google-cloud/storage');
  1682. * const storage = new Storage();
  1683. * const bucket = storage.bucket('albums');
  1684. *
  1685. * const config = {
  1686. * prefix: 'log'
  1687. * };
  1688. *
  1689. * bucket.enableLogging(config, function(err, apiResponse) {
  1690. * if (!err) {
  1691. * // Logging functionality enabled successfully.
  1692. * }
  1693. * });
  1694. *
  1695. * ```
  1696. * @example
  1697. * Optionally, provide a destination bucket.
  1698. * ```
  1699. * const config = {
  1700. * prefix: 'log',
  1701. * bucket: 'destination-bucket'
  1702. * };
  1703. *
  1704. * bucket.enableLogging(config, function(err, apiResponse) {});
  1705. * ```
  1706. *
  1707. * @example
  1708. * If the callback is omitted, we'll return a Promise.
  1709. * ```
  1710. * bucket.enableLogging(config).then(function(data) {
  1711. * const apiResponse = data[0];
  1712. * });
  1713. * ```
  1714. */
  1715. enableLogging(config, callback) {
  1716. if (!config ||
  1717. typeof config === 'function' ||
  1718. typeof config.prefix === 'undefined') {
  1719. throw new Error(BucketExceptionMessages.CONFIGURATION_OBJECT_PREFIX_REQUIRED);
  1720. }
  1721. let logBucket = this.id;
  1722. if (config.bucket && config.bucket instanceof Bucket) {
  1723. logBucket = config.bucket.id;
  1724. }
  1725. else if (config.bucket && typeof config.bucket === 'string') {
  1726. logBucket = config.bucket;
  1727. }
  1728. const options = {};
  1729. if (config === null || config === void 0 ? void 0 : config.ifMetagenerationMatch) {
  1730. options.ifMetagenerationMatch = config.ifMetagenerationMatch;
  1731. }
  1732. if (config === null || config === void 0 ? void 0 : config.ifMetagenerationNotMatch) {
  1733. options.ifMetagenerationNotMatch = config.ifMetagenerationNotMatch;
  1734. }
  1735. (async () => {
  1736. try {
  1737. const [policy] = await this.iam.getPolicy();
  1738. policy.bindings.push({
  1739. members: ['group:cloud-storage-analytics@google.com'],
  1740. role: 'roles/storage.objectCreator',
  1741. });
  1742. await this.iam.setPolicy(policy);
  1743. this.setMetadata({
  1744. logging: {
  1745. logBucket,
  1746. logObjectPrefix: config.prefix,
  1747. },
  1748. }, options, callback);
  1749. }
  1750. catch (e) {
  1751. callback(e);
  1752. return;
  1753. }
  1754. })();
  1755. }
  1756. /**
  1757. * @typedef {array} EnableRequesterPaysResponse
  1758. * @property {object} 0 The full API response.
  1759. */
  1760. /**
  1761. * @callback EnableRequesterPaysCallback
  1762. * @param {?Error} err Request error, if any.
  1763. * @param {object} apiResponse The full API response.
  1764. */
  1765. /**
  1766. * <div class="notice">
  1767. * <strong>Early Access Testers Only</strong>
  1768. * <p>
  1769. * This feature is not yet widely-available.
  1770. * </p>
  1771. * </div>
  1772. *
  1773. * Enable `requesterPays` functionality for this bucket. This enables you, the
  1774. * bucket owner, to have the requesting user assume the charges for the access
  1775. * to your bucket and its contents.
  1776. *
  1777. * @param {EnableRequesterPaysCallback | EnableRequesterPaysOptions} [optionsOrCallback]
  1778. * Callback function or precondition options.
  1779. * @returns {Promise<EnableRequesterPaysResponse>}
  1780. *
  1781. * @example
  1782. * ```
  1783. * const {Storage} = require('@google-cloud/storage');
  1784. * const storage = new Storage();
  1785. * const bucket = storage.bucket('albums');
  1786. *
  1787. * bucket.enableRequesterPays(function(err, apiResponse) {
  1788. * if (!err) {
  1789. * // requesterPays functionality enabled successfully.
  1790. * }
  1791. * });
  1792. *
  1793. * //-
  1794. * // If the callback is omitted, we'll return a Promise.
  1795. * //-
  1796. * bucket.enableRequesterPays().then(function(data) {
  1797. * const apiResponse = data[0];
  1798. * });
  1799. *
  1800. * ```
  1801. * @example <caption>include:samples/requesterPays.js</caption>
  1802. * region_tag:storage_enable_requester_pays
  1803. * Example of enabling requester pays:
  1804. */
  1805. enableRequesterPays(optionsOrCallback, cb) {
  1806. let options = {};
  1807. if (typeof optionsOrCallback === 'function') {
  1808. cb = optionsOrCallback;
  1809. }
  1810. else if (optionsOrCallback) {
  1811. options = optionsOrCallback;
  1812. }
  1813. this.setMetadata({
  1814. billing: {
  1815. requesterPays: true,
  1816. },
  1817. }, options, cb);
  1818. }
  1819. /**
  1820. * Create a {@link File} object. See {@link File} to see how to handle
  1821. * the different use cases you may have.
  1822. *
  1823. * @param {string} name The name of the file in this bucket.
  1824. * @param {FileOptions} [options] Configuration options.
  1825. * @param {string|number} [options.generation] Only use a specific revision of
  1826. * this file.
  1827. * @param {string} [options.encryptionKey] A custom encryption key. See
  1828. * {@link https://cloud.google.com/storage/docs/encryption#customer-supplied| Customer-supplied Encryption Keys}.
  1829. * @param {string} [options.kmsKeyName] The name of the Cloud KMS key that will
  1830. * be used to encrypt the object. Must be in the format:
  1831. * `projects/my-project/locations/location/keyRings/my-kr/cryptoKeys/my-key`.
  1832. * KMS key ring must use the same location as the bucket.
  1833. * @param {string} [options.userProject] The ID of the project which will be
  1834. * billed for all requests made from File object.
  1835. * @returns {File}
  1836. *
  1837. * @example
  1838. * ```
  1839. * const {Storage} = require('@google-cloud/storage');
  1840. * const storage = new Storage();
  1841. * const bucket = storage.bucket('albums');
  1842. * const file = bucket.file('my-existing-file.png');
  1843. * ```
  1844. */
  1845. file(name, options) {
  1846. if (!name) {
  1847. throw Error(BucketExceptionMessages.SPECIFY_FILE_NAME);
  1848. }
  1849. return new File(this, name, options);
  1850. }
  1851. /**
  1852. * @typedef {array} GetFilesResponse
  1853. * @property {File[]} 0 Array of {@link File} instances.
  1854. * @param {object} nextQuery 1 A query object to receive more results.
  1855. * @param {object} apiResponse 2 The full API response.
  1856. */
  1857. /**
  1858. * @callback GetFilesCallback
  1859. * @param {?Error} err Request error, if any.
  1860. * @param {File[]} files Array of {@link File} instances.
  1861. * @param {object} nextQuery A query object to receive more results.
  1862. * @param {object} apiResponse The full API response.
  1863. */
  1864. /**
  1865. * Query object for listing files.
  1866. *
  1867. * @typedef {object} GetFilesOptions
  1868. * @property {boolean} [autoPaginate=true] Have pagination handled
  1869. * automatically.
  1870. * @property {string} [delimiter] Results will contain only objects whose
  1871. * names, aside from the prefix, do not contain delimiter. Objects whose
  1872. * names, aside from the prefix, contain delimiter will have their name
  1873. * truncated after the delimiter, returned in `apiResponse.prefixes`.
  1874. * Duplicate prefixes are omitted.
  1875. * @property {string} [endOffset] Filter results to objects whose names are
  1876. * lexicographically before endOffset. If startOffset is also set, the objects
  1877. * listed have names between startOffset (inclusive) and endOffset (exclusive).
  1878. * @property {boolean} [includeFoldersAsPrefixes] If true, includes folders and
  1879. * managed folders in the set of prefixes returned by the query. Only applicable if
  1880. * delimiter is set to / and autoPaginate is set to false.
  1881. * See: https://cloud.google.com/storage/docs/managed-folders
  1882. * @property {boolean} [includeTrailingDelimiter] If true, objects that end in
  1883. * exactly one instance of delimiter have their metadata included in items[]
  1884. * in addition to the relevant part of the object name appearing in prefixes[].
  1885. * @property {string} [prefix] Filter results to objects whose names begin
  1886. * with this prefix.
  1887. * @property {string} [matchGlob] A glob pattern used to filter results,
  1888. * for example foo*bar
  1889. * @property {number} [maxApiCalls] Maximum number of API calls to make.
  1890. * @property {number} [maxResults] Maximum number of items plus prefixes to
  1891. * return per call.
  1892. * Note: By default will handle pagination automatically
  1893. * if more than 1 page worth of results are requested per call.
  1894. * When `autoPaginate` is set to `false` the smaller of `maxResults`
  1895. * or 1 page of results will be returned per call.
  1896. * @property {string} [pageToken] A previously-returned page token
  1897. * representing part of the larger set of results to view.
  1898. * @property {boolean} [softDeleted] If true, only soft-deleted object versions will be
  1899. * listed as distinct results in order of generation number. Note `soft_deleted` and
  1900. * `versions` cannot be set to true simultaneously.
  1901. * @property {string} [startOffset] Filter results to objects whose names are
  1902. * lexicographically equal to or after startOffset. If endOffset is also set,
  1903. * the objects listed have names between startOffset (inclusive) and endOffset (exclusive).
  1904. * @property {string} [userProject] The ID of the project which will be
  1905. * billed for the request.
  1906. * @property {boolean} [versions] If true, returns File objects scoped to
  1907. * their versions.
  1908. */
  1909. /**
  1910. * Get {@link File} objects for the files currently in the bucket.
  1911. *
  1912. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/list| Objects: list API Documentation}
  1913. *
  1914. * @param {GetFilesOptions} [query] Query object for listing files.
  1915. * @param {boolean} [query.autoPaginate=true] Have pagination handled
  1916. * automatically.
  1917. * @param {string} [query.delimiter] Results will contain only objects whose
  1918. * names, aside from the prefix, do not contain delimiter. Objects whose
  1919. * names, aside from the prefix, contain delimiter will have their name
  1920. * truncated after the delimiter, returned in `apiResponse.prefixes`.
  1921. * Duplicate prefixes are omitted.
  1922. * @param {string} [query.endOffset] Filter results to objects whose names are
  1923. * lexicographically before endOffset. If startOffset is also set, the objects
  1924. * listed have names between startOffset (inclusive) and endOffset (exclusive).
  1925. * @param {boolean} [query.includeFoldersAsPrefixes] If true, includes folders and
  1926. * managed folders in the set of prefixes returned by the query. Only applicable if
  1927. * delimiter is set to / and autoPaginate is set to false.
  1928. * See: https://cloud.google.com/storage/docs/managed-folders
  1929. * @param {boolean} [query.includeTrailingDelimiter] If true, objects that end in
  1930. * exactly one instance of delimiter have their metadata included in items[]
  1931. * in addition to the relevant part of the object name appearing in prefixes[].
  1932. * @param {string} [query.prefix] Filter results to objects whose names begin
  1933. * with this prefix.
  1934. * @param {number} [query.maxApiCalls] Maximum number of API calls to make.
  1935. * @param {number} [query.maxResults] Maximum number of items plus prefixes to
  1936. * return per call.
  1937. * Note: By default will handle pagination automatically
  1938. * if more than 1 page worth of results are requested per call.
  1939. * When `autoPaginate` is set to `false` the smaller of `maxResults`
  1940. * or 1 page of results will be returned per call.
  1941. * @param {string} [query.pageToken] A previously-returned page token
  1942. * representing part of the larger set of results to view.
  1943. * @param {boolean} [query.softDeleted] If true, only soft-deleted object versions will be
  1944. * listed as distinct results in order of generation number. Note `soft_deleted` and
  1945. * `versions` cannot be set to true simultaneously.
  1946. * @param {string} [query.startOffset] Filter results to objects whose names are
  1947. * lexicographically equal to or after startOffset. If endOffset is also set,
  1948. * the objects listed have names between startOffset (inclusive) and endOffset (exclusive).
  1949. * @param {string} [query.userProject] The ID of the project which will be
  1950. * billed for the request.
  1951. * @param {boolean} [query.versions] If true, returns File objects scoped to
  1952. * their versions.
  1953. * @param {GetFilesCallback} [callback] Callback function.
  1954. * @returns {Promise<GetFilesResponse>}
  1955. *
  1956. * @example
  1957. * ```
  1958. * const {Storage} = require('@google-cloud/storage');
  1959. * const storage = new Storage();
  1960. * const bucket = storage.bucket('albums');
  1961. *
  1962. * bucket.getFiles(function(err, files) {
  1963. * if (!err) {
  1964. * // files is an array of File objects.
  1965. * }
  1966. * });
  1967. *
  1968. * //-
  1969. * // If your bucket has versioning enabled, you can get all of your files
  1970. * // scoped to their generation.
  1971. * //-
  1972. * bucket.getFiles({
  1973. * versions: true
  1974. * }, function(err, files) {
  1975. * // Each file is scoped to its generation.
  1976. * });
  1977. *
  1978. * //-
  1979. * // To control how many API requests are made and page through the results
  1980. * // manually, set `autoPaginate` to `false`.
  1981. * //-
  1982. * const callback = function(err, files, nextQuery, apiResponse) {
  1983. * if (nextQuery) {
  1984. * // More results exist.
  1985. * bucket.getFiles(nextQuery, callback);
  1986. * }
  1987. *
  1988. * // The `metadata` property is populated for you with the metadata at the
  1989. * // time of fetching.
  1990. * files[0].metadata;
  1991. *
  1992. * // However, in cases where you are concerned the metadata could have
  1993. * // changed, use the `getMetadata` method.
  1994. * files[0].getMetadata(function(err, metadata) {});
  1995. * };
  1996. *
  1997. * bucket.getFiles({
  1998. * autoPaginate: false
  1999. * }, callback);
  2000. *
  2001. * //-
  2002. * // If the callback is omitted, we'll return a Promise.
  2003. * //-
  2004. * bucket.getFiles().then(function(data) {
  2005. * const files = data[0];
  2006. * });
  2007. *
  2008. * ```
  2009. * @example
  2010. * <h6>Simulating a File System</h6><p>With `autoPaginate: false`, it's possible to iterate over files which incorporate a common structure using a delimiter.</p><p>Consider the following remote objects:</p><ol><li>"a"</li><li>"a/b/c/d"</li><li>"b/d/e"</li></ol><p>Using a delimiter of `/` will return a single file, "a".</p><p>`apiResponse.prefixes` will return the "sub-directories" that were found:</p><ol><li>"a/"</li><li>"b/"</li></ol>
  2011. * ```
  2012. * bucket.getFiles({
  2013. * autoPaginate: false,
  2014. * delimiter: '/'
  2015. * }, function(err, files, nextQuery, apiResponse) {
  2016. * // files = [
  2017. * // {File} // File object for file "a"
  2018. * // ]
  2019. *
  2020. * // apiResponse.prefixes = [
  2021. * // 'a/',
  2022. * // 'b/'
  2023. * // ]
  2024. * });
  2025. * ```
  2026. *
  2027. * @example
  2028. * Using prefixes, it's now possible to simulate a file system with follow-up requests.
  2029. * ```
  2030. * bucket.getFiles({
  2031. * autoPaginate: false,
  2032. * delimiter: '/',
  2033. * prefix: 'a/'
  2034. * }, function(err, files, nextQuery, apiResponse) {
  2035. * // No files found within "directory" a.
  2036. * // files = []
  2037. *
  2038. * // However, a "sub-directory" was found.
  2039. * // This prefix can be used to continue traversing the "file system".
  2040. * // apiResponse.prefixes = [
  2041. * // 'a/b/'
  2042. * // ]
  2043. * });
  2044. * ```
  2045. *
  2046. * @example <caption>include:samples/files.js</caption>
  2047. * region_tag:storage_list_files
  2048. * Another example:
  2049. *
  2050. * @example <caption>include:samples/files.js</caption>
  2051. * region_tag:storage_list_files_with_prefix
  2052. * Example of listing files, filtered by a prefix:
  2053. */
  2054. getFiles(queryOrCallback, callback) {
  2055. let query = typeof queryOrCallback === 'object' ? queryOrCallback : {};
  2056. if (!callback) {
  2057. callback = queryOrCallback;
  2058. }
  2059. query = Object.assign({}, query);
  2060. this.request({
  2061. uri: '/o',
  2062. qs: query,
  2063. }, (err, resp) => {
  2064. if (err) {
  2065. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2066. callback(err, null, null, resp);
  2067. return;
  2068. }
  2069. const itemsArray = resp.items ? resp.items : [];
  2070. const files = itemsArray.map((file) => {
  2071. const options = {};
  2072. if (query.fields) {
  2073. const fileInstance = file;
  2074. return fileInstance;
  2075. }
  2076. if (query.versions) {
  2077. options.generation = file.generation;
  2078. }
  2079. if (file.kmsKeyName) {
  2080. options.kmsKeyName = file.kmsKeyName;
  2081. }
  2082. const fileInstance = this.file(file.name, options);
  2083. fileInstance.metadata = file;
  2084. return fileInstance;
  2085. });
  2086. let nextQuery = null;
  2087. if (resp.nextPageToken) {
  2088. nextQuery = Object.assign({}, query, {
  2089. pageToken: resp.nextPageToken,
  2090. });
  2091. }
  2092. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2093. callback(null, files, nextQuery, resp);
  2094. });
  2095. }
  2096. /**
  2097. * @deprecated
  2098. * @typedef {object} GetLabelsOptions Configuration options for Bucket#getLabels().
  2099. * @param {string} [userProject] The ID of the project which will be
  2100. * billed for the request.
  2101. */
  2102. /**
  2103. * @deprecated
  2104. * @typedef {array} GetLabelsResponse
  2105. * @property {object} 0 Object of labels currently set on this bucket.
  2106. */
  2107. /**
  2108. * @deprecated
  2109. * @callback GetLabelsCallback
  2110. * @param {?Error} err Request error, if any.
  2111. * @param {object} labels Object of labels currently set on this bucket.
  2112. */
  2113. /**
  2114. * @deprecated Use getMetadata directly.
  2115. * Get the labels currently set on this bucket.
  2116. *
  2117. * @param {object} [options] Configuration options.
  2118. * @param {string} [options.userProject] The ID of the project which will be
  2119. * billed for the request.
  2120. * @param {GetLabelsCallback} [callback] Callback function.
  2121. * @returns {Promise<GetLabelsCallback>}
  2122. *
  2123. * @example
  2124. * ```
  2125. * const {Storage} = require('@google-cloud/storage');
  2126. * const storage = new Storage();
  2127. * const bucket = storage.bucket('albums');
  2128. *
  2129. * bucket.getLabels(function(err, labels) {
  2130. * if (err) {
  2131. * // Error handling omitted.
  2132. * }
  2133. *
  2134. * // labels = {
  2135. * // label: 'labelValue',
  2136. * // ...
  2137. * // }
  2138. * });
  2139. *
  2140. * //-
  2141. * // If the callback is omitted, we'll return a Promise.
  2142. * //-
  2143. * bucket.getLabels().then(function(data) {
  2144. * const labels = data[0];
  2145. * });
  2146. * ```
  2147. */
  2148. getLabels(optionsOrCallback, callback) {
  2149. let options = {};
  2150. if (typeof optionsOrCallback === 'function') {
  2151. callback = optionsOrCallback;
  2152. }
  2153. else if (optionsOrCallback) {
  2154. options = optionsOrCallback;
  2155. }
  2156. this.getMetadata(options, (err, metadata) => {
  2157. if (err) {
  2158. callback(err, null);
  2159. return;
  2160. }
  2161. callback(null, (metadata === null || metadata === void 0 ? void 0 : metadata.labels) || {});
  2162. });
  2163. }
  2164. /**
  2165. * @typedef {object} GetNotificationsOptions Configuration options for Bucket#getNotification().
  2166. * @property {string} [userProject] The ID of the project which will be
  2167. * billed for the request.
  2168. */
  2169. /**
  2170. * @callback GetNotificationsCallback
  2171. * @param {?Error} err Request error, if any.
  2172. * @param {Notification[]} notifications Array of {@link Notification}
  2173. * instances.
  2174. * @param {object} apiResponse The full API response.
  2175. */
  2176. /**
  2177. * @typedef {array} GetNotificationsResponse
  2178. * @property {Notification[]} 0 Array of {@link Notification} instances.
  2179. * @property {object} 1 The full API response.
  2180. */
  2181. /**
  2182. * Retrieves a list of notification subscriptions for a given bucket.
  2183. *
  2184. * See {@link https://cloud.google.com/storage/docs/json_api/v1/notifications/list| Notifications: list}
  2185. *
  2186. * @param {GetNotificationsOptions} [options] Configuration options.
  2187. * @param {string} [options.userProject] The ID of the project which will be
  2188. * billed for the request.
  2189. * @param {GetNotificationsCallback} [callback] Callback function.
  2190. * @returns {Promise<GetNotificationsResponse>}
  2191. *
  2192. * @example
  2193. * ```
  2194. * const {Storage} = require('@google-cloud/storage');
  2195. * const storage = new Storage();
  2196. * const bucket = storage.bucket('my-bucket');
  2197. *
  2198. * bucket.getNotifications(function(err, notifications, apiResponse) {
  2199. * if (!err) {
  2200. * // notifications is an array of Notification objects.
  2201. * }
  2202. * });
  2203. *
  2204. * //-
  2205. * // If the callback is omitted, we'll return a Promise.
  2206. * //-
  2207. * bucket.getNotifications().then(function(data) {
  2208. * const notifications = data[0];
  2209. * const apiResponse = data[1];
  2210. * });
  2211. *
  2212. * ```
  2213. * @example <caption>include:samples/listNotifications.js</caption>
  2214. * region_tag:storage_list_bucket_notifications
  2215. * Another example:
  2216. */
  2217. getNotifications(optionsOrCallback, callback) {
  2218. let options = {};
  2219. if (typeof optionsOrCallback === 'function') {
  2220. callback = optionsOrCallback;
  2221. }
  2222. else if (optionsOrCallback) {
  2223. options = optionsOrCallback;
  2224. }
  2225. this.request({
  2226. uri: '/notificationConfigs',
  2227. qs: options,
  2228. }, (err, resp) => {
  2229. if (err) {
  2230. callback(err, null, resp);
  2231. return;
  2232. }
  2233. const itemsArray = resp.items ? resp.items : [];
  2234. const notifications = itemsArray.map((notification) => {
  2235. const notificationInstance = this.notification(notification.id);
  2236. notificationInstance.metadata = notification;
  2237. return notificationInstance;
  2238. });
  2239. callback(null, notifications, resp);
  2240. });
  2241. }
  2242. /**
  2243. * @typedef {array} GetSignedUrlResponse
  2244. * @property {object} 0 The signed URL.
  2245. */
  2246. /**
  2247. * @callback GetSignedUrlCallback
  2248. * @param {?Error} err Request error, if any.
  2249. * @param {object} url The signed URL.
  2250. */
  2251. /**
  2252. * @typedef {object} GetBucketSignedUrlConfig
  2253. * @property {string} action Only listing objects within a bucket (HTTP: GET) is supported for bucket-level signed URLs.
  2254. * @property {*} expires A timestamp when this link will expire. Any value
  2255. * given is passed to `new Date()`.
  2256. * Note: 'v4' supports maximum duration of 7 days (604800 seconds) from now.
  2257. * @property {string} [version='v2'] The signing version to use, either
  2258. * 'v2' or 'v4'.
  2259. * @property {boolean} [virtualHostedStyle=false] Use virtual hosted-style
  2260. * URLs ('https://mybucket.storage.googleapis.com/...') instead of path-style
  2261. * ('https://storage.googleapis.com/mybucket/...'). Virtual hosted-style URLs
  2262. * should generally be preferred instaed of path-style URL.
  2263. * Currently defaults to `false` for path-style, although this may change in a
  2264. * future major-version release.
  2265. * @property {string} [cname] The cname for this bucket, i.e.,
  2266. * "https://cdn.example.com".
  2267. * See [reference]{@link https://cloud.google.com/storage/docs/access-control/signed-urls#example}
  2268. * @property {object} [extensionHeaders] If these headers are used, the
  2269. * server will check to make sure that the client provides matching
  2270. * values. See {@link https://cloud.google.com/storage/docs/access-control/signed-urls#about-canonical-extension-headers| Canonical extension headers}
  2271. * for the requirements of this feature, most notably:
  2272. * - The header name must be prefixed with `x-goog-`
  2273. * - The header name must be all lowercase
  2274. *
  2275. * Note: Multi-valued header passed as an array in the extensionHeaders
  2276. * object is converted into a string, delimited by `,` with
  2277. * no space. Requests made using the signed URL will need to
  2278. * delimit multi-valued headers using a single `,` as well, or
  2279. * else the server will report a mismatched signature.
  2280. * @property {object} [queryParams] Additional query parameters to include
  2281. * in the signed URL.
  2282. */
  2283. /**
  2284. * Get a signed URL to allow limited time access to a bucket.
  2285. *
  2286. * In Google Cloud Platform environments, such as Cloud Functions and App
  2287. * Engine, you usually don't provide a `keyFilename` or `credentials` during
  2288. * instantiation. In those environments, we call the
  2289. * {@link https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signBlob| signBlob API}
  2290. * to create a signed URL. That API requires either the
  2291. * `https://www.googleapis.com/auth/iam` or
  2292. * `https://www.googleapis.com/auth/cloud-platform` scope, so be sure they are
  2293. * enabled.
  2294. *
  2295. * See {@link https://cloud.google.com/storage/docs/access-control/signed-urls| Signed URLs Reference}
  2296. *
  2297. * @throws {Error} if an expiration timestamp from the past is given.
  2298. *
  2299. * @param {GetBucketSignedUrlConfig} config Configuration object.
  2300. * @param {string} config.action Currently only supports "list" (HTTP: GET).
  2301. * @param {*} config.expires A timestamp when this link will expire. Any value
  2302. * given is passed to `new Date()`.
  2303. * Note: 'v4' supports maximum duration of 7 days (604800 seconds) from now.
  2304. * @param {string} [config.version='v2'] The signing version to use, either
  2305. * 'v2' or 'v4'.
  2306. * @param {boolean} [config.virtualHostedStyle=false] Use virtual hosted-style
  2307. * URLs ('https://mybucket.storage.googleapis.com/...') instead of path-style
  2308. * ('https://storage.googleapis.com/mybucket/...'). Virtual hosted-style URLs
  2309. * should generally be preferred instaed of path-style URL.
  2310. * Currently defaults to `false` for path-style, although this may change in a
  2311. * future major-version release.
  2312. * @param {string} [config.cname] The cname for this bucket, i.e.,
  2313. * "https://cdn.example.com".
  2314. * See [reference]{@link https://cloud.google.com/storage/docs/access-control/signed-urls#example}
  2315. * @param {object} [config.extensionHeaders] If these headers are used, the
  2316. * server will check to make sure that the client provides matching
  2317. * values. See {@link https://cloud.google.com/storage/docs/access-control/signed-urls#about-canonical-extension-headers| Canonical extension headers}
  2318. * for the requirements of this feature, most notably:
  2319. * - The header name must be prefixed with `x-goog-`
  2320. * - The header name must be all lowercase
  2321. *
  2322. * Note: Multi-valued header passed as an array in the extensionHeaders
  2323. * object is converted into a string, delimited by `,` with
  2324. * no space. Requests made using the signed URL will need to
  2325. * delimit multi-valued headers using a single `,` as well, or
  2326. * else the server will report a mismatched signature.
  2327. * @property {object} [config.queryParams] Additional query parameters to include
  2328. * in the signed URL.
  2329. * @param {GetSignedUrlCallback} [callback] Callback function.
  2330. * @returns {Promise<GetSignedUrlResponse>}
  2331. *
  2332. * @example
  2333. * ```
  2334. * const {Storage} = require('@google-cloud/storage');
  2335. * const storage = new Storage();
  2336. * const myBucket = storage.bucket('my-bucket');
  2337. *
  2338. * //-
  2339. * // Generate a URL that allows temporary access to list files in a bucket.
  2340. * //-
  2341. * const request = require('request');
  2342. *
  2343. * const config = {
  2344. * action: 'list',
  2345. * expires: '03-17-2025'
  2346. * };
  2347. *
  2348. * bucket.getSignedUrl(config, function(err, url) {
  2349. * if (err) {
  2350. * console.error(err);
  2351. * return;
  2352. * }
  2353. *
  2354. * // The bucket is now available to be listed from this URL.
  2355. * request(url, function(err, resp) {
  2356. * // resp.statusCode = 200
  2357. * });
  2358. * });
  2359. *
  2360. * //-
  2361. * // If the callback is omitted, we'll return a Promise.
  2362. * //-
  2363. * bucket.getSignedUrl(config).then(function(data) {
  2364. * const url = data[0];
  2365. * });
  2366. * ```
  2367. */
  2368. getSignedUrl(cfg, callback) {
  2369. const method = BucketActionToHTTPMethod[cfg.action];
  2370. const signConfig = {
  2371. method,
  2372. expires: cfg.expires,
  2373. version: cfg.version,
  2374. cname: cfg.cname,
  2375. extensionHeaders: cfg.extensionHeaders || {},
  2376. queryParams: cfg.queryParams || {},
  2377. host: cfg.host,
  2378. signingEndpoint: cfg.signingEndpoint,
  2379. };
  2380. if (!this.signer) {
  2381. this.signer = new URLSigner(this.storage.authClient, this, undefined, this.storage);
  2382. }
  2383. this.signer
  2384. .getSignedUrl(signConfig)
  2385. .then(signedUrl => callback(null, signedUrl), callback);
  2386. }
  2387. /**
  2388. * @callback BucketLockCallback
  2389. * @param {?Error} err Request error, if any.
  2390. * @param {object} apiResponse The full API response.
  2391. */
  2392. /**
  2393. * Lock a previously-defined retention policy. This will prevent changes to
  2394. * the policy.
  2395. *
  2396. * @throws {Error} if a metageneration is not provided.
  2397. *
  2398. * @param {number|string} metageneration The bucket's metageneration. This is
  2399. * accesssible from calling {@link File#getMetadata}.
  2400. * @param {BucketLockCallback} [callback] Callback function.
  2401. * @returns {Promise<BucketLockResponse>}
  2402. *
  2403. * @example
  2404. * ```
  2405. * const storage = require('@google-cloud/storage')();
  2406. * const bucket = storage.bucket('albums');
  2407. *
  2408. * const metageneration = 2;
  2409. *
  2410. * bucket.lock(metageneration, function(err, apiResponse) {});
  2411. *
  2412. * //-
  2413. * // If the callback is omitted, we'll return a Promise.
  2414. * //-
  2415. * bucket.lock(metageneration).then(function(data) {
  2416. * const apiResponse = data[0];
  2417. * });
  2418. * ```
  2419. */
  2420. lock(metageneration, callback) {
  2421. const metatype = typeof metageneration;
  2422. if (metatype !== 'number' && metatype !== 'string') {
  2423. throw new Error(BucketExceptionMessages.METAGENERATION_NOT_PROVIDED);
  2424. }
  2425. this.request({
  2426. method: 'POST',
  2427. uri: '/lockRetentionPolicy',
  2428. qs: {
  2429. ifMetagenerationMatch: metageneration,
  2430. },
  2431. }, callback);
  2432. }
  2433. /**
  2434. * @typedef {array} MakeBucketPrivateResponse
  2435. * @property {File[]} 0 List of files made private.
  2436. */
  2437. /**
  2438. * @callback MakeBucketPrivateCallback
  2439. * @param {?Error} err Request error, if any.
  2440. * @param {File[]} files List of files made private.
  2441. */
  2442. /**
  2443. * @typedef {object} MakeBucketPrivateOptions
  2444. * @property {boolean} [includeFiles=false] Make each file in the bucket
  2445. * private.
  2446. * @property {Metadata} [metadata] Define custom metadata properties to define
  2447. * along with the operation.
  2448. * @property {boolean} [force] Queue errors occurred while making files
  2449. * private until all files have been processed.
  2450. * @property {string} [userProject] The ID of the project which will be
  2451. * billed for the request.
  2452. */
  2453. /**
  2454. * Make the bucket listing private.
  2455. *
  2456. * You may also choose to make the contents of the bucket private by
  2457. * specifying `includeFiles: true`. This will automatically run
  2458. * {@link File#makePrivate} for every file in the bucket.
  2459. *
  2460. * When specifying `includeFiles: true`, use `force: true` to delay execution
  2461. * of your callback until all files have been processed. By default, the
  2462. * callback is executed after the first error. Use `force` to queue such
  2463. * errors until all files have been processed, after which they will be
  2464. * returned as an array as the first argument to your callback.
  2465. *
  2466. * NOTE: This may cause the process to be long-running and use a high number
  2467. * of requests. Use with caution.
  2468. *
  2469. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/patch| Buckets: patch API Documentation}
  2470. *
  2471. * @param {MakeBucketPrivateOptions} [options] Configuration options.
  2472. * @param {boolean} [options.includeFiles=false] Make each file in the bucket
  2473. * private.
  2474. * @param {Metadata} [options.metadata] Define custom metadata properties to define
  2475. * along with the operation.
  2476. * @param {boolean} [options.force] Queue errors occurred while making files
  2477. * private until all files have been processed.
  2478. * @param {string} [options.userProject] The ID of the project which will be
  2479. * billed for the request.
  2480. * @param {MakeBucketPrivateCallback} [callback] Callback function.
  2481. * @returns {Promise<MakeBucketPrivateResponse>}
  2482. *
  2483. * @example
  2484. * ```
  2485. * const {Storage} = require('@google-cloud/storage');
  2486. * const storage = new Storage();
  2487. * const bucket = storage.bucket('albums');
  2488. *
  2489. * //-
  2490. * // Make the bucket private.
  2491. * //-
  2492. * bucket.makePrivate(function(err) {});
  2493. *
  2494. * //-
  2495. * // Make the bucket and its contents private.
  2496. * //-
  2497. * const opts = {
  2498. * includeFiles: true
  2499. * };
  2500. *
  2501. * bucket.makePrivate(opts, function(err, files) {
  2502. * // `err`:
  2503. * // The first error to occur, otherwise null.
  2504. * //
  2505. * // `files`:
  2506. * // Array of files successfully made private in the bucket.
  2507. * });
  2508. *
  2509. * //-
  2510. * // Make the bucket and its contents private, using force to suppress errors
  2511. * // until all files have been processed.
  2512. * //-
  2513. * const opts = {
  2514. * includeFiles: true,
  2515. * force: true
  2516. * };
  2517. *
  2518. * bucket.makePrivate(opts, function(errors, files) {
  2519. * // `errors`:
  2520. * // Array of errors if any occurred, otherwise null.
  2521. * //
  2522. * // `files`:
  2523. * // Array of files successfully made private in the bucket.
  2524. * });
  2525. *
  2526. * //-
  2527. * // If the callback is omitted, we'll return a Promise.
  2528. * //-
  2529. * bucket.makePrivate(opts).then(function(data) {
  2530. * const files = data[0];
  2531. * });
  2532. * ```
  2533. */
  2534. makePrivate(optionsOrCallback, callback) {
  2535. var _a, _b, _c, _d;
  2536. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2537. callback =
  2538. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2539. options.private = true;
  2540. const query = {
  2541. predefinedAcl: 'projectPrivate',
  2542. };
  2543. if (options.userProject) {
  2544. query.userProject = options.userProject;
  2545. }
  2546. if ((_a = options.preconditionOpts) === null || _a === void 0 ? void 0 : _a.ifGenerationMatch) {
  2547. query.ifGenerationMatch = options.preconditionOpts.ifGenerationMatch;
  2548. }
  2549. if ((_b = options.preconditionOpts) === null || _b === void 0 ? void 0 : _b.ifGenerationNotMatch) {
  2550. query.ifGenerationNotMatch =
  2551. options.preconditionOpts.ifGenerationNotMatch;
  2552. }
  2553. if ((_c = options.preconditionOpts) === null || _c === void 0 ? void 0 : _c.ifMetagenerationMatch) {
  2554. query.ifMetagenerationMatch =
  2555. options.preconditionOpts.ifMetagenerationMatch;
  2556. }
  2557. if ((_d = options.preconditionOpts) === null || _d === void 0 ? void 0 : _d.ifMetagenerationNotMatch) {
  2558. query.ifMetagenerationNotMatch =
  2559. options.preconditionOpts.ifMetagenerationNotMatch;
  2560. }
  2561. // You aren't allowed to set both predefinedAcl & acl properties on a bucket
  2562. // so acl must explicitly be nullified.
  2563. const metadata = { ...options.metadata, acl: null };
  2564. this.setMetadata(metadata, query, (err) => {
  2565. if (err) {
  2566. callback(err);
  2567. }
  2568. const internalCall = () => {
  2569. if (options.includeFiles) {
  2570. return promisify(this.makeAllFilesPublicPrivate_).call(this, options);
  2571. }
  2572. return Promise.resolve([]);
  2573. };
  2574. internalCall()
  2575. .then(files => callback(null, files))
  2576. .catch(callback);
  2577. });
  2578. }
  2579. /**
  2580. * @typedef {object} MakeBucketPublicOptions
  2581. * @property {boolean} [includeFiles=false] Make each file in the bucket
  2582. * private.
  2583. * @property {boolean} [force] Queue errors occurred while making files
  2584. * private until all files have been processed.
  2585. */
  2586. /**
  2587. * @callback MakeBucketPublicCallback
  2588. * @param {?Error} err Request error, if any.
  2589. * @param {File[]} files List of files made public.
  2590. */
  2591. /**
  2592. * @typedef {array} MakeBucketPublicResponse
  2593. * @property {File[]} 0 List of files made public.
  2594. */
  2595. /**
  2596. * Make the bucket publicly readable.
  2597. *
  2598. * You may also choose to make the contents of the bucket publicly readable by
  2599. * specifying `includeFiles: true`. This will automatically run
  2600. * {@link File#makePublic} for every file in the bucket.
  2601. *
  2602. * When specifying `includeFiles: true`, use `force: true` to delay execution
  2603. * of your callback until all files have been processed. By default, the
  2604. * callback is executed after the first error. Use `force` to queue such
  2605. * errors until all files have been processed, after which they will be
  2606. * returned as an array as the first argument to your callback.
  2607. *
  2608. * NOTE: This may cause the process to be long-running and use a high number
  2609. * of requests. Use with caution.
  2610. *
  2611. * See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/patch| Buckets: patch API Documentation}
  2612. *
  2613. * @param {MakeBucketPublicOptions} [options] Configuration options.
  2614. * @param {boolean} [options.includeFiles=false] Make each file in the bucket
  2615. * private.
  2616. * @param {boolean} [options.force] Queue errors occurred while making files
  2617. * private until all files have been processed.
  2618. * @param {MakeBucketPublicCallback} [callback] Callback function.
  2619. * @returns {Promise<MakeBucketPublicResponse>}
  2620. *
  2621. * @example
  2622. * ```
  2623. * const {Storage} = require('@google-cloud/storage');
  2624. * const storage = new Storage();
  2625. * const bucket = storage.bucket('albums');
  2626. *
  2627. * //-
  2628. * // Make the bucket publicly readable.
  2629. * //-
  2630. * bucket.makePublic(function(err) {});
  2631. *
  2632. * //-
  2633. * // Make the bucket and its contents publicly readable.
  2634. * //-
  2635. * const opts = {
  2636. * includeFiles: true
  2637. * };
  2638. *
  2639. * bucket.makePublic(opts, function(err, files) {
  2640. * // `err`:
  2641. * // The first error to occur, otherwise null.
  2642. * //
  2643. * // `files`:
  2644. * // Array of files successfully made public in the bucket.
  2645. * });
  2646. *
  2647. * //-
  2648. * // Make the bucket and its contents publicly readable, using force to
  2649. * // suppress errors until all files have been processed.
  2650. * //-
  2651. * const opts = {
  2652. * includeFiles: true,
  2653. * force: true
  2654. * };
  2655. *
  2656. * bucket.makePublic(opts, function(errors, files) {
  2657. * // `errors`:
  2658. * // Array of errors if any occurred, otherwise null.
  2659. * //
  2660. * // `files`:
  2661. * // Array of files successfully made public in the bucket.
  2662. * });
  2663. *
  2664. * //-
  2665. * // If the callback is omitted, we'll return a Promise.
  2666. * //-
  2667. * bucket.makePublic(opts).then(function(data) {
  2668. * const files = data[0];
  2669. * });
  2670. * ```
  2671. */
  2672. makePublic(optionsOrCallback, callback) {
  2673. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2674. callback =
  2675. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2676. const req = { public: true, ...options };
  2677. this.acl
  2678. .add({
  2679. entity: 'allUsers',
  2680. role: 'READER',
  2681. })
  2682. .then(() => {
  2683. return this.acl.default.add({
  2684. entity: 'allUsers',
  2685. role: 'READER',
  2686. });
  2687. })
  2688. .then(() => {
  2689. if (req.includeFiles) {
  2690. return promisify(this.makeAllFilesPublicPrivate_).call(this, req);
  2691. }
  2692. return [];
  2693. })
  2694. .then(files => callback(null, files), callback);
  2695. }
  2696. /**
  2697. * Get a reference to a Cloud Pub/Sub Notification.
  2698. *
  2699. * @param {string} id ID of notification.
  2700. * @returns {Notification}
  2701. * @see Notification
  2702. *
  2703. * @example
  2704. * ```
  2705. * const {Storage} = require('@google-cloud/storage');
  2706. * const storage = new Storage();
  2707. * const bucket = storage.bucket('my-bucket');
  2708. * const notification = bucket.notification('1');
  2709. * ```
  2710. */
  2711. notification(id) {
  2712. if (!id) {
  2713. throw new Error(BucketExceptionMessages.SUPPLY_NOTIFICATION_ID);
  2714. }
  2715. return new Notification(this, id);
  2716. }
  2717. /**
  2718. * Remove an already-existing retention policy from this bucket, if it is not
  2719. * locked.
  2720. *
  2721. * @param {SetBucketMetadataCallback} [callback] Callback function.
  2722. * @param {SetBucketMetadataOptions} [options] Options, including precondition options
  2723. * @returns {Promise<SetBucketMetadataResponse>}
  2724. *
  2725. * @example
  2726. * ```
  2727. * const storage = require('@google-cloud/storage')();
  2728. * const bucket = storage.bucket('albums');
  2729. *
  2730. * bucket.removeRetentionPeriod(function(err, apiResponse) {});
  2731. *
  2732. * //-
  2733. * // If the callback is omitted, we'll return a Promise.
  2734. * //-
  2735. * bucket.removeRetentionPeriod().then(function(data) {
  2736. * const apiResponse = data[0];
  2737. * });
  2738. * ```
  2739. */
  2740. removeRetentionPeriod(optionsOrCallback, callback) {
  2741. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2742. callback =
  2743. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2744. this.setMetadata({
  2745. retentionPolicy: null,
  2746. }, options, callback);
  2747. }
  2748. /**
  2749. * Makes request and applies userProject query parameter if necessary.
  2750. *
  2751. * @private
  2752. *
  2753. * @param {object} reqOpts - The request options.
  2754. * @param {function} callback - The callback function.
  2755. */
  2756. request(reqOpts, callback) {
  2757. if (this.userProject && (!reqOpts.qs || !reqOpts.qs.userProject)) {
  2758. reqOpts.qs = { ...reqOpts.qs, userProject: this.userProject };
  2759. }
  2760. return super.request(reqOpts, callback);
  2761. }
  2762. /**
  2763. * @deprecated
  2764. * @typedef {array} SetLabelsResponse
  2765. * @property {object} 0 The bucket metadata.
  2766. */
  2767. /**
  2768. * @deprecated
  2769. * @callback SetLabelsCallback
  2770. * @param {?Error} err Request error, if any.
  2771. * @param {object} metadata The bucket metadata.
  2772. */
  2773. /**
  2774. * @deprecated
  2775. * @typedef {object} SetLabelsOptions Configuration options for Bucket#setLabels().
  2776. * @property {string} [userProject] The ID of the project which will be
  2777. * billed for the request.
  2778. */
  2779. /**
  2780. * @deprecated Use setMetadata directly.
  2781. * Set labels on the bucket.
  2782. *
  2783. * This makes an underlying call to {@link Bucket#setMetadata}, which
  2784. * is a PATCH request. This means an individual label can be overwritten, but
  2785. * unmentioned labels will not be touched.
  2786. *
  2787. * @param {object<string, string>} labels Labels to set on the bucket.
  2788. * @param {SetLabelsOptions} [options] Configuration options.
  2789. * @param {string} [options.userProject] The ID of the project which will be
  2790. * billed for the request.
  2791. * @param {SetLabelsCallback} [callback] Callback function.
  2792. * @returns {Promise<SetLabelsResponse>}
  2793. *
  2794. * @example
  2795. * ```
  2796. * const {Storage} = require('@google-cloud/storage');
  2797. * const storage = new Storage();
  2798. * const bucket = storage.bucket('albums');
  2799. *
  2800. * const labels = {
  2801. * labelone: 'labelonevalue',
  2802. * labeltwo: 'labeltwovalue'
  2803. * };
  2804. *
  2805. * bucket.setLabels(labels, function(err, metadata) {
  2806. * if (!err) {
  2807. * // Labels set successfully.
  2808. * }
  2809. * });
  2810. *
  2811. * //-
  2812. * // If the callback is omitted, we'll return a Promise.
  2813. * //-
  2814. * bucket.setLabels(labels).then(function(data) {
  2815. * const metadata = data[0];
  2816. * });
  2817. * ```
  2818. */
  2819. setLabels(labels, optionsOrCallback, callback) {
  2820. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2821. callback =
  2822. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2823. callback = callback || util.noop;
  2824. this.setMetadata({ labels }, options, callback);
  2825. }
  2826. setMetadata(metadata, optionsOrCallback, cb) {
  2827. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2828. cb =
  2829. typeof optionsOrCallback === 'function'
  2830. ? optionsOrCallback
  2831. : cb;
  2832. this.disableAutoRetryConditionallyIdempotent_(this.methods.setMetadata, AvailableServiceObjectMethods.setMetadata, options);
  2833. super
  2834. .setMetadata(metadata, options)
  2835. .then(resp => cb(null, ...resp))
  2836. .catch(cb)
  2837. .finally(() => {
  2838. this.storage.retryOptions.autoRetry = this.instanceRetryValue;
  2839. });
  2840. }
  2841. /**
  2842. * Lock all objects contained in the bucket, based on their creation time. Any
  2843. * attempt to overwrite or delete objects younger than the retention period
  2844. * will result in a `PERMISSION_DENIED` error.
  2845. *
  2846. * An unlocked retention policy can be modified or removed from the bucket via
  2847. * {@link File#removeRetentionPeriod} and {@link File#setRetentionPeriod}. A
  2848. * locked retention policy cannot be removed or shortened in duration for the
  2849. * lifetime of the bucket. Attempting to remove or decrease period of a locked
  2850. * retention policy will result in a `PERMISSION_DENIED` error. You can still
  2851. * increase the policy.
  2852. *
  2853. * @param {*} duration In seconds, the minimum retention time for all objects
  2854. * contained in this bucket.
  2855. * @param {SetBucketMetadataCallback} [callback] Callback function.
  2856. * @param {SetBucketMetadataCallback} [options] Options, including precondition options.
  2857. * @returns {Promise<SetBucketMetadataResponse>}
  2858. *
  2859. * @example
  2860. * ```
  2861. * const storage = require('@google-cloud/storage')();
  2862. * const bucket = storage.bucket('albums');
  2863. *
  2864. * const DURATION_SECONDS = 15780000; // 6 months.
  2865. *
  2866. * //-
  2867. * // Lock the objects in this bucket for 6 months.
  2868. * //-
  2869. * bucket.setRetentionPeriod(DURATION_SECONDS, function(err, apiResponse) {});
  2870. *
  2871. * //-
  2872. * // If the callback is omitted, we'll return a Promise.
  2873. * //-
  2874. * bucket.setRetentionPeriod(DURATION_SECONDS).then(function(data) {
  2875. * const apiResponse = data[0];
  2876. * });
  2877. * ```
  2878. */
  2879. setRetentionPeriod(duration, optionsOrCallback, callback) {
  2880. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2881. callback =
  2882. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2883. this.setMetadata({
  2884. retentionPolicy: {
  2885. retentionPeriod: duration.toString(),
  2886. },
  2887. }, options, callback);
  2888. }
  2889. /**
  2890. *
  2891. * @typedef {object} Cors
  2892. * @property {number} [maxAgeSeconds] The number of seconds the browser is
  2893. * allowed to make requests before it must repeat the preflight request.
  2894. * @property {string[]} [method] HTTP method allowed for cross origin resource
  2895. * sharing with this bucket.
  2896. * @property {string[]} [origin] an origin allowed for cross origin resource
  2897. * sharing with this bucket.
  2898. * @property {string[]} [responseHeader] A header allowed for cross origin
  2899. * resource sharing with this bucket.
  2900. */
  2901. /**
  2902. * This can be used to set the CORS configuration on the bucket.
  2903. *
  2904. * The configuration will be overwritten with the value passed into this.
  2905. *
  2906. * @param {Cors[]} corsConfiguration The new CORS configuration to set
  2907. * @param {number} [corsConfiguration.maxAgeSeconds] The number of seconds the browser is
  2908. * allowed to make requests before it must repeat the preflight request.
  2909. * @param {string[]} [corsConfiguration.method] HTTP method allowed for cross origin resource
  2910. * sharing with this bucket.
  2911. * @param {string[]} [corsConfiguration.origin] an origin allowed for cross origin resource
  2912. * sharing with this bucket.
  2913. * @param {string[]} [corsConfiguration.responseHeader] A header allowed for cross origin
  2914. * resource sharing with this bucket.
  2915. * @param {SetBucketMetadataCallback} [callback] Callback function.
  2916. * @param {SetBucketMetadataOptions} [options] Options, including precondition options.
  2917. * @returns {Promise<SetBucketMetadataResponse>}
  2918. *
  2919. * @example
  2920. * ```
  2921. * const storage = require('@google-cloud/storage')();
  2922. * const bucket = storage.bucket('albums');
  2923. *
  2924. * const corsConfiguration = [{maxAgeSeconds: 3600}]; // 1 hour
  2925. * bucket.setCorsConfiguration(corsConfiguration);
  2926. *
  2927. * //-
  2928. * // If the callback is omitted, we'll return a Promise.
  2929. * //-
  2930. * bucket.setCorsConfiguration(corsConfiguration).then(function(data) {
  2931. * const apiResponse = data[0];
  2932. * });
  2933. * ```
  2934. */
  2935. setCorsConfiguration(corsConfiguration, optionsOrCallback, callback) {
  2936. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2937. callback =
  2938. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2939. this.setMetadata({
  2940. cors: corsConfiguration,
  2941. }, options, callback);
  2942. }
  2943. /**
  2944. * @typedef {object} SetBucketStorageClassOptions
  2945. * @property {string} [userProject] - The ID of the project which will be
  2946. * billed for the request.
  2947. */
  2948. /**
  2949. * @callback SetBucketStorageClassCallback
  2950. * @param {?Error} err Request error, if any.
  2951. */
  2952. /**
  2953. * Set the default storage class for new files in this bucket.
  2954. *
  2955. * See {@link https://cloud.google.com/storage/docs/storage-classes| Storage Classes}
  2956. *
  2957. * @param {string} storageClass The new storage class. (`standard`,
  2958. * `nearline`, `coldline`, or `archive`).
  2959. * **Note:** The storage classes `multi_regional`, `regional`, and
  2960. * `durable_reduced_availability` are now legacy and will be deprecated in
  2961. * the future.
  2962. * @param {object} [options] Configuration options.
  2963. * @param {string} [options.userProject] - The ID of the project which will be
  2964. * billed for the request.
  2965. * @param {SetStorageClassCallback} [callback] Callback function.
  2966. * @returns {Promise}
  2967. *
  2968. * @example
  2969. * ```
  2970. * const {Storage} = require('@google-cloud/storage');
  2971. * const storage = new Storage();
  2972. * const bucket = storage.bucket('albums');
  2973. *
  2974. * bucket.setStorageClass('nearline', function(err, apiResponse) {
  2975. * if (err) {
  2976. * // Error handling omitted.
  2977. * }
  2978. *
  2979. * // The storage class was updated successfully.
  2980. * });
  2981. *
  2982. * //-
  2983. * // If the callback is omitted, we'll return a Promise.
  2984. * //-
  2985. * bucket.setStorageClass('nearline').then(function() {});
  2986. * ```
  2987. */
  2988. setStorageClass(storageClass, optionsOrCallback, callback) {
  2989. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  2990. callback =
  2991. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  2992. // In case we get input like `storageClass`, convert to `storage_class`.
  2993. storageClass = storageClass
  2994. .replace(/-/g, '_')
  2995. .replace(/([a-z])([A-Z])/g, (_, low, up) => {
  2996. return low + '_' + up;
  2997. })
  2998. .toUpperCase();
  2999. this.setMetadata({ storageClass }, options, callback);
  3000. }
  3001. /**
  3002. * Set a user project to be billed for all requests made from this Bucket
  3003. * object and any files referenced from this Bucket object.
  3004. *
  3005. * @param {string} userProject The user project.
  3006. *
  3007. * @example
  3008. * ```
  3009. * const {Storage} = require('@google-cloud/storage');
  3010. * const storage = new Storage();
  3011. * const bucket = storage.bucket('albums');
  3012. *
  3013. * bucket.setUserProject('grape-spaceship-123');
  3014. * ```
  3015. */
  3016. setUserProject(userProject) {
  3017. this.userProject = userProject;
  3018. const methods = [
  3019. 'create',
  3020. 'delete',
  3021. 'exists',
  3022. 'get',
  3023. 'getMetadata',
  3024. 'setMetadata',
  3025. ];
  3026. methods.forEach(method => {
  3027. const methodConfig = this.methods[method];
  3028. if (typeof methodConfig === 'object') {
  3029. if (typeof methodConfig.reqOpts === 'object') {
  3030. Object.assign(methodConfig.reqOpts.qs, { userProject });
  3031. }
  3032. else {
  3033. methodConfig.reqOpts = {
  3034. qs: { userProject },
  3035. };
  3036. }
  3037. }
  3038. });
  3039. }
  3040. /**
  3041. * @typedef {object} UploadOptions Configuration options for Bucket#upload().
  3042. * @property {string|File} [destination] The place to save
  3043. * your file. If given a string, the file will be uploaded to the bucket
  3044. * using the string as a filename. When given a File object, your local
  3045. * file will be uploaded to the File object's bucket and under the File
  3046. * object's name. Lastly, when this argument is omitted, the file is uploaded
  3047. * to your bucket using the name of the local file.
  3048. * @property {string} [encryptionKey] A custom encryption key. See
  3049. * {@link https://cloud.google.com/storage/docs/encryption#customer-supplied| Customer-supplied Encryption Keys}.
  3050. * @property {boolean} [gzip] Automatically gzip the file. This will set
  3051. * `options.metadata.contentEncoding` to `gzip`.
  3052. * @property {string} [kmsKeyName] The name of the Cloud KMS key that will
  3053. * be used to encrypt the object. Must be in the format:
  3054. * `projects/my-project/locations/location/keyRings/my-kr/cryptoKeys/my-key`.
  3055. * @property {object} [metadata] See an
  3056. * {@link https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request_properties_JSON| Objects: insert request body}.
  3057. * @property {string} [offset] The starting byte of the upload stream, for
  3058. * resuming an interrupted upload. Defaults to 0.
  3059. * @property {string} [predefinedAcl] Apply a predefined set of access
  3060. * controls to this object.
  3061. *
  3062. * Acceptable values are:
  3063. * - **`authenticatedRead`** - Object owner gets `OWNER` access, and
  3064. * `allAuthenticatedUsers` get `READER` access.
  3065. *
  3066. * - **`bucketOwnerFullControl`** - Object owner gets `OWNER` access, and
  3067. * project team owners get `OWNER` access.
  3068. *
  3069. * - **`bucketOwnerRead`** - Object owner gets `OWNER` access, and project
  3070. * team owners get `READER` access.
  3071. *
  3072. * - **`private`** - Object owner gets `OWNER` access.
  3073. *
  3074. * - **`projectPrivate`** - Object owner gets `OWNER` access, and project
  3075. * team members get access according to their roles.
  3076. *
  3077. * - **`publicRead`** - Object owner gets `OWNER` access, and `allUsers`
  3078. * get `READER` access.
  3079. * @property {boolean} [private] Make the uploaded file private. (Alias for
  3080. * `options.predefinedAcl = 'private'`)
  3081. * @property {boolean} [public] Make the uploaded file public. (Alias for
  3082. * `options.predefinedAcl = 'publicRead'`)
  3083. * @property {boolean} [resumable=true] Resumable uploads are automatically
  3084. * enabled and must be shut off explicitly by setting to false.
  3085. * @property {number} [timeout=60000] Set the HTTP request timeout in
  3086. * milliseconds. This option is not available for resumable uploads.
  3087. * Default: `60000`
  3088. * @property {string} [uri] The URI for an already-created resumable
  3089. * upload. See {@link File#createResumableUpload}.
  3090. * @property {string} [userProject] The ID of the project which will be
  3091. * billed for the request.
  3092. * @property {string|boolean} [validation] Possible values: `"md5"`,
  3093. * `"crc32c"`, or `false`. By default, data integrity is validated with an
  3094. * MD5 checksum for maximum reliability. CRC32c will provide better
  3095. * performance with less reliability. You may also choose to skip
  3096. * validation completely, however this is **not recommended**.
  3097. */
  3098. /**
  3099. * @typedef {array} UploadResponse
  3100. * @property {object} 0 The uploaded {@link File}.
  3101. * @property {object} 1 The full API response.
  3102. */
  3103. /**
  3104. * @callback UploadCallback
  3105. * @param {?Error} err Request error, if any.
  3106. * @param {object} file The uploaded {@link File}.
  3107. * @param {object} apiResponse The full API response.
  3108. */
  3109. /**
  3110. * Upload a file to the bucket. This is a convenience method that wraps
  3111. * {@link File#createWriteStream}.
  3112. *
  3113. * Resumable uploads are enabled by default
  3114. *
  3115. * See {@link https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload#uploads| Upload Options (Simple or Resumable)}
  3116. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/insert| Objects: insert API Documentation}
  3117. *
  3118. * @param {string} pathString The fully qualified path to the file you
  3119. * wish to upload to your bucket.
  3120. * @param {UploadOptions} [options] Configuration options.
  3121. * @param {string|File} [options.destination] The place to save
  3122. * your file. If given a string, the file will be uploaded to the bucket
  3123. * using the string as a filename. When given a File object, your local
  3124. * file will be uploaded to the File object's bucket and under the File
  3125. * object's name. Lastly, when this argument is omitted, the file is uploaded
  3126. * to your bucket using the name of the local file.
  3127. * @param {string} [options.encryptionKey] A custom encryption key. See
  3128. * {@link https://cloud.google.com/storage/docs/encryption#customer-supplied| Customer-supplied Encryption Keys}.
  3129. * @param {boolean} [options.gzip] Automatically gzip the file. This will set
  3130. * `options.metadata.contentEncoding` to `gzip`.
  3131. * @param {string} [options.kmsKeyName] The name of the Cloud KMS key that will
  3132. * be used to encrypt the object. Must be in the format:
  3133. * `projects/my-project/locations/location/keyRings/my-kr/cryptoKeys/my-key`.
  3134. * @param {object} [options.metadata] See an
  3135. * {@link https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request_properties_JSON| Objects: insert request body}.
  3136. * @param {string} [options.offset] The starting byte of the upload stream, for
  3137. * resuming an interrupted upload. Defaults to 0.
  3138. * @param {string} [options.predefinedAcl] Apply a predefined set of access
  3139. * controls to this object.
  3140. * Acceptable values are:
  3141. * - **`authenticatedRead`** - Object owner gets `OWNER` access, and
  3142. * `allAuthenticatedUsers` get `READER` access.
  3143. *
  3144. * - **`bucketOwnerFullControl`** - Object owner gets `OWNER` access, and
  3145. * project team owners get `OWNER` access.
  3146. *
  3147. * - **`bucketOwnerRead`** - Object owner gets `OWNER` access, and project
  3148. * team owners get `READER` access.
  3149. *
  3150. * - **`private`** - Object owner gets `OWNER` access.
  3151. *
  3152. * - **`projectPrivate`** - Object owner gets `OWNER` access, and project
  3153. * team members get access according to their roles.
  3154. *
  3155. * - **`publicRead`** - Object owner gets `OWNER` access, and `allUsers`
  3156. * get `READER` access.
  3157. * @param {boolean} [options.private] Make the uploaded file private. (Alias for
  3158. * `options.predefinedAcl = 'private'`)
  3159. * @param {boolean} [options.public] Make the uploaded file public. (Alias for
  3160. * `options.predefinedAcl = 'publicRead'`)
  3161. * @param {boolean} [options.resumable=true] Resumable uploads are automatically
  3162. * enabled and must be shut off explicitly by setting to false.
  3163. * @param {number} [options.timeout=60000] Set the HTTP request timeout in
  3164. * milliseconds. This option is not available for resumable uploads.
  3165. * Default: `60000`
  3166. * @param {string} [options.uri] The URI for an already-created resumable
  3167. * upload. See {@link File#createResumableUpload}.
  3168. * @param {string} [options.userProject] The ID of the project which will be
  3169. * billed for the request.
  3170. * @param {string|boolean} [options.validation] Possible values: `"md5"`,
  3171. * `"crc32c"`, or `false`. By default, data integrity is validated with an
  3172. * MD5 checksum for maximum reliability. CRC32c will provide better
  3173. * performance with less reliability. You may also choose to skip
  3174. * validation completely, however this is **not recommended**.
  3175. * @param {UploadCallback} [callback] Callback function.
  3176. * @returns {Promise<UploadResponse>}
  3177. *
  3178. * @example
  3179. * ```
  3180. * const {Storage} = require('@google-cloud/storage');
  3181. * const storage = new Storage();
  3182. * const bucket = storage.bucket('albums');
  3183. *
  3184. * //-
  3185. * // Upload a file from a local path.
  3186. * //-
  3187. * bucket.upload('/local/path/image.png', function(err, file, apiResponse) {
  3188. * // Your bucket now contains:
  3189. * // - "image.png" (with the contents of `/local/path/image.png')
  3190. *
  3191. * // `file` is an instance of a File object that refers to your new file.
  3192. * });
  3193. *
  3194. *
  3195. * //-
  3196. * // It's not always that easy. You will likely want to specify the filename
  3197. * // used when your new file lands in your bucket.
  3198. * //
  3199. * // You may also want to set metadata or customize other options.
  3200. * //-
  3201. * const options = {
  3202. * destination: 'new-image.png',
  3203. * validation: 'crc32c',
  3204. * metadata: {
  3205. * metadata: {
  3206. * event: 'Fall trip to the zoo'
  3207. * }
  3208. * }
  3209. * };
  3210. *
  3211. * bucket.upload('local-image.png', options, function(err, file) {
  3212. * // Your bucket now contains:
  3213. * // - "new-image.png" (with the contents of `local-image.png')
  3214. *
  3215. * // `file` is an instance of a File object that refers to your new file.
  3216. * });
  3217. *
  3218. * //-
  3219. * // You can also have a file gzip'd on the fly.
  3220. * //-
  3221. * bucket.upload('index.html', { gzip: true }, function(err, file) {
  3222. * // Your bucket now contains:
  3223. * // - "index.html" (automatically compressed with gzip)
  3224. *
  3225. * // Downloading the file with `file.download` will automatically decode
  3226. * the
  3227. * // file.
  3228. * });
  3229. *
  3230. * //-
  3231. * // You may also re-use a File object, {File}, that references
  3232. * // the file you wish to create or overwrite.
  3233. * //-
  3234. * const options = {
  3235. * destination: bucket.file('existing-file.png'),
  3236. * resumable: false
  3237. * };
  3238. *
  3239. * bucket.upload('local-img.png', options, function(err, newFile) {
  3240. * // Your bucket now contains:
  3241. * // - "existing-file.png" (with the contents of `local-img.png')
  3242. *
  3243. * // Note:
  3244. * // The `newFile` parameter is equal to `file`.
  3245. * });
  3246. *
  3247. * //-
  3248. * // To use
  3249. * // <a
  3250. * href="https://cloud.google.com/storage/docs/encryption#customer-supplied">
  3251. * // Customer-supplied Encryption Keys</a>, provide the `encryptionKey`
  3252. * option.
  3253. * //-
  3254. * const crypto = require('crypto');
  3255. * const encryptionKey = crypto.randomBytes(32);
  3256. *
  3257. * bucket.upload('img.png', {
  3258. * encryptionKey: encryptionKey
  3259. * }, function(err, newFile) {
  3260. * // `img.png` was uploaded with your custom encryption key.
  3261. *
  3262. * // `newFile` is already configured to use the encryption key when making
  3263. * // operations on the remote object.
  3264. *
  3265. * // However, to use your encryption key later, you must create a `File`
  3266. * // instance with the `key` supplied:
  3267. * const file = bucket.file('img.png', {
  3268. * encryptionKey: encryptionKey
  3269. * });
  3270. *
  3271. * // Or with `file#setEncryptionKey`:
  3272. * const file = bucket.file('img.png');
  3273. * file.setEncryptionKey(encryptionKey);
  3274. * });
  3275. *
  3276. * //-
  3277. * // If the callback is omitted, we'll return a Promise.
  3278. * //-
  3279. * bucket.upload('local-image.png').then(function(data) {
  3280. * const file = data[0];
  3281. * });
  3282. *
  3283. * To upload a file from a URL, use {@link File#createWriteStream}.
  3284. *
  3285. * ```
  3286. * @example <caption>include:samples/files.js</caption>
  3287. * region_tag:storage_upload_file
  3288. * Another example:
  3289. *
  3290. * @example <caption>include:samples/encryption.js</caption>
  3291. * region_tag:storage_upload_encrypted_file
  3292. * Example of uploading an encrypted file:
  3293. */
  3294. upload(pathString, optionsOrCallback, callback) {
  3295. var _a, _b;
  3296. const upload = (numberOfRetries) => {
  3297. const returnValue = AsyncRetry(async (bail) => {
  3298. await new Promise((resolve, reject) => {
  3299. var _a, _b;
  3300. if (numberOfRetries === 0 &&
  3301. ((_b = (_a = newFile === null || newFile === void 0 ? void 0 : newFile.storage) === null || _a === void 0 ? void 0 : _a.retryOptions) === null || _b === void 0 ? void 0 : _b.autoRetry)) {
  3302. newFile.storage.retryOptions.autoRetry = false;
  3303. }
  3304. const writable = newFile.createWriteStream(options);
  3305. if (options.onUploadProgress) {
  3306. writable.on('progress', options.onUploadProgress);
  3307. }
  3308. fs.createReadStream(pathString)
  3309. .on('error', bail)
  3310. .pipe(writable)
  3311. .on('error', err => {
  3312. if (this.storage.retryOptions.autoRetry &&
  3313. this.storage.retryOptions.retryableErrorFn(err)) {
  3314. return reject(err);
  3315. }
  3316. else {
  3317. return bail(err);
  3318. }
  3319. })
  3320. .on('finish', () => {
  3321. return resolve();
  3322. });
  3323. });
  3324. }, {
  3325. retries: numberOfRetries,
  3326. factor: this.storage.retryOptions.retryDelayMultiplier,
  3327. maxTimeout: this.storage.retryOptions.maxRetryDelay * 1000, //convert to milliseconds
  3328. maxRetryTime: this.storage.retryOptions.totalTimeout * 1000, //convert to milliseconds
  3329. });
  3330. if (!callback) {
  3331. return returnValue;
  3332. }
  3333. else {
  3334. return returnValue
  3335. .then(() => {
  3336. if (callback) {
  3337. return callback(null, newFile, newFile.metadata);
  3338. }
  3339. })
  3340. .catch(callback);
  3341. }
  3342. };
  3343. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3344. if (global['GCLOUD_SANDBOX_ENV']) {
  3345. return;
  3346. }
  3347. let options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  3348. callback =
  3349. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  3350. options = Object.assign({
  3351. metadata: {},
  3352. }, options);
  3353. // Do not retry if precondition option ifGenerationMatch is not set
  3354. // because this is a file operation
  3355. let maxRetries = this.storage.retryOptions.maxRetries;
  3356. if ((((_a = options === null || options === void 0 ? void 0 : options.preconditionOpts) === null || _a === void 0 ? void 0 : _a.ifGenerationMatch) === undefined &&
  3357. ((_b = this.instancePreconditionOpts) === null || _b === void 0 ? void 0 : _b.ifGenerationMatch) === undefined &&
  3358. this.storage.retryOptions.idempotencyStrategy ===
  3359. IdempotencyStrategy.RetryConditional) ||
  3360. this.storage.retryOptions.idempotencyStrategy ===
  3361. IdempotencyStrategy.RetryNever) {
  3362. maxRetries = 0;
  3363. }
  3364. let newFile;
  3365. if (options.destination instanceof File) {
  3366. newFile = options.destination;
  3367. }
  3368. else if (options.destination !== null &&
  3369. typeof options.destination === 'string') {
  3370. // Use the string as the name of the file.
  3371. newFile = this.file(options.destination, {
  3372. encryptionKey: options.encryptionKey,
  3373. kmsKeyName: options.kmsKeyName,
  3374. preconditionOpts: this.instancePreconditionOpts,
  3375. });
  3376. }
  3377. else {
  3378. // Resort to using the name of the incoming file.
  3379. const destination = path.basename(pathString);
  3380. newFile = this.file(destination, {
  3381. encryptionKey: options.encryptionKey,
  3382. kmsKeyName: options.kmsKeyName,
  3383. preconditionOpts: this.instancePreconditionOpts,
  3384. });
  3385. }
  3386. upload(maxRetries);
  3387. }
  3388. /**
  3389. * @private
  3390. *
  3391. * @typedef {object} MakeAllFilesPublicPrivateOptions
  3392. * @property {boolean} [force] Suppress errors until all files have been
  3393. * processed.
  3394. * @property {boolean} [private] Make files private.
  3395. * @property {boolean} [public] Make files public.
  3396. * @property {string} [userProject] The ID of the project which will be
  3397. * billed for the request.
  3398. */
  3399. /**
  3400. * @private
  3401. *
  3402. * @callback SetBucketMetadataCallback
  3403. * @param {?Error} err Request error, if any.
  3404. * @param {File[]} files Files that were updated.
  3405. */
  3406. /**
  3407. * @typedef {array} MakeAllFilesPublicPrivateResponse
  3408. * @property {File[]} 0 List of files affected.
  3409. */
  3410. /**
  3411. * Iterate over all of a bucket's files, calling `file.makePublic()` (public)
  3412. * or `file.makePrivate()` (private) on each.
  3413. *
  3414. * Operations are performed in parallel, up to 10 at once. The first error
  3415. * breaks the loop, and will execute the provided callback with it. Specify
  3416. * `{ force: true }` to suppress the errors.
  3417. *
  3418. * @private
  3419. *
  3420. * @param {MakeAllFilesPublicPrivateOptions} [options] Configuration options.
  3421. * @param {boolean} [options.force] Suppress errors until all files have been
  3422. * processed.
  3423. * @param {boolean} [options.private] Make files private.
  3424. * @param {boolean} [options.public] Make files public.
  3425. * @param {string} [options.userProject] The ID of the project which will be
  3426. * billed for the request.
  3427. * @param {MakeAllFilesPublicPrivateCallback} callback Callback function.
  3428. *
  3429. * @return {Promise<MakeAllFilesPublicPrivateResponse>}
  3430. */
  3431. makeAllFilesPublicPrivate_(optionsOrCallback, callback) {
  3432. const MAX_PARALLEL_LIMIT = 10;
  3433. const errors = [];
  3434. const updatedFiles = [];
  3435. const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
  3436. callback =
  3437. typeof optionsOrCallback === 'function' ? optionsOrCallback : callback;
  3438. const processFile = async (file) => {
  3439. try {
  3440. await (options.public ? file.makePublic() : file.makePrivate(options));
  3441. updatedFiles.push(file);
  3442. }
  3443. catch (e) {
  3444. if (!options.force) {
  3445. throw e;
  3446. }
  3447. errors.push(e);
  3448. }
  3449. };
  3450. this.getFiles(options)
  3451. .then(([files]) => {
  3452. const limit = pLimit(MAX_PARALLEL_LIMIT);
  3453. const promises = files.map(file => {
  3454. return limit(() => processFile(file));
  3455. });
  3456. return Promise.all(promises);
  3457. })
  3458. .then(() => callback(errors.length > 0 ? errors : null, updatedFiles), err => callback(err, updatedFiles));
  3459. }
  3460. getId() {
  3461. return this.id;
  3462. }
  3463. disableAutoRetryConditionallyIdempotent_(
  3464. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3465. coreOpts,
  3466. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  3467. methodType, localPreconditionOptions) {
  3468. var _a, _b;
  3469. if (typeof coreOpts === 'object' &&
  3470. ((_b = (_a = coreOpts === null || coreOpts === void 0 ? void 0 : coreOpts.reqOpts) === null || _a === void 0 ? void 0 : _a.qs) === null || _b === void 0 ? void 0 : _b.ifMetagenerationMatch) === undefined &&
  3471. (localPreconditionOptions === null || localPreconditionOptions === void 0 ? void 0 : localPreconditionOptions.ifMetagenerationMatch) === undefined &&
  3472. (methodType === AvailableServiceObjectMethods.setMetadata ||
  3473. methodType === AvailableServiceObjectMethods.delete) &&
  3474. this.storage.retryOptions.idempotencyStrategy ===
  3475. IdempotencyStrategy.RetryConditional) {
  3476. this.storage.retryOptions.autoRetry = false;
  3477. }
  3478. else if (this.storage.retryOptions.idempotencyStrategy ===
  3479. IdempotencyStrategy.RetryNever) {
  3480. this.storage.retryOptions.autoRetry = false;
  3481. }
  3482. }
  3483. }
  3484. /*! Developer Documentation
  3485. *
  3486. * These methods can be auto-paginated.
  3487. */
  3488. paginator.extend(Bucket, 'getFiles');
  3489. /*! Developer Documentation
  3490. *
  3491. * All async methods (except for streams) will return a Promise in the event
  3492. * that a callback is omitted.
  3493. */
  3494. promisifyAll(Bucket, {
  3495. exclude: ['cloudStorageURI', 'request', 'file', 'notification'],
  3496. });
  3497. /**
  3498. * Reference to the {@link Bucket} class.
  3499. * @name module:@google-cloud/storage.Bucket
  3500. * @see Bucket
  3501. */
  3502. export { Bucket };