file.d.ts 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. import { BodyResponseCallback, DecorateRequestOptions, GetConfig, MetadataCallback, ServiceObject, SetMetadataResponse } from './nodejs-common/index.js';
  2. import * as resumableUpload from './resumable-upload.js';
  3. import { Writable, Readable, PipelineSource } from 'stream';
  4. import * as http from 'http';
  5. import { PreconditionOptions, Storage } from './storage.js';
  6. import { AvailableServiceObjectMethods, Bucket } from './bucket.js';
  7. import { Acl, AclMetadata } from './acl.js';
  8. import { GetSignedUrlResponse, GetSignedUrlCallback, URLSigner, SignerGetSignedUrlConfig, Query } from './signer.js';
  9. import { Duplexify, GCCL_GCS_CMD_KEY } from './nodejs-common/util.js';
  10. import { CRC32C, CRC32CValidatorGenerator } from './crc32c.js';
  11. import { URL } from 'url';
  12. import { BaseMetadata, DeleteCallback, DeleteOptions, GetResponse, InstanceResponseCallback, RequestResponse, SetMetadataOptions } from './nodejs-common/service-object.js';
  13. import * as r from 'teeny-request';
  14. export type GetExpirationDateResponse = [Date];
  15. export interface GetExpirationDateCallback {
  16. (err: Error | null, expirationDate?: Date | null, apiResponse?: unknown): void;
  17. }
  18. export interface PolicyDocument {
  19. string: string;
  20. base64: string;
  21. signature: string;
  22. }
  23. export type SaveData = string | Buffer | Uint8Array | PipelineSource<string | Buffer | Uint8Array>;
  24. export type GenerateSignedPostPolicyV2Response = [PolicyDocument];
  25. export interface GenerateSignedPostPolicyV2Callback {
  26. (err: Error | null, policy?: PolicyDocument): void;
  27. }
  28. export interface GenerateSignedPostPolicyV2Options {
  29. equals?: string[] | string[][];
  30. expires: string | number | Date;
  31. startsWith?: string[] | string[][];
  32. acl?: string;
  33. successRedirect?: string;
  34. successStatus?: string;
  35. contentLengthRange?: {
  36. min?: number;
  37. max?: number;
  38. };
  39. /**
  40. * @example
  41. * 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/'
  42. */
  43. signingEndpoint?: string;
  44. }
  45. export interface PolicyFields {
  46. [key: string]: string;
  47. }
  48. export interface GenerateSignedPostPolicyV4Options {
  49. expires: string | number | Date;
  50. bucketBoundHostname?: string;
  51. virtualHostedStyle?: boolean;
  52. conditions?: object[];
  53. fields?: PolicyFields;
  54. /**
  55. * @example
  56. * 'https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/'
  57. */
  58. signingEndpoint?: string;
  59. }
  60. export interface GenerateSignedPostPolicyV4Callback {
  61. (err: Error | null, output?: SignedPostPolicyV4Output): void;
  62. }
  63. export type GenerateSignedPostPolicyV4Response = [SignedPostPolicyV4Output];
  64. export interface SignedPostPolicyV4Output {
  65. url: string;
  66. fields: PolicyFields;
  67. }
  68. export interface GetSignedUrlConfig extends Pick<SignerGetSignedUrlConfig, 'host' | 'signingEndpoint'> {
  69. action: 'read' | 'write' | 'delete' | 'resumable';
  70. version?: 'v2' | 'v4';
  71. virtualHostedStyle?: boolean;
  72. cname?: string;
  73. contentMd5?: string;
  74. contentType?: string;
  75. expires: string | number | Date;
  76. accessibleAt?: string | number | Date;
  77. extensionHeaders?: http.OutgoingHttpHeaders;
  78. promptSaveAs?: string;
  79. responseDisposition?: string;
  80. responseType?: string;
  81. queryParams?: Query;
  82. }
  83. export interface GetFileMetadataOptions {
  84. userProject?: string;
  85. }
  86. export type GetFileMetadataResponse = [FileMetadata, unknown];
  87. export interface GetFileMetadataCallback {
  88. (err: Error | null, metadata?: FileMetadata, apiResponse?: unknown): void;
  89. }
  90. export interface GetFileOptions extends GetConfig {
  91. userProject?: string;
  92. generation?: number;
  93. restoreToken?: string;
  94. softDeleted?: boolean;
  95. }
  96. export type GetFileResponse = [File, unknown];
  97. export interface GetFileCallback {
  98. (err: Error | null, file?: File, apiResponse?: unknown): void;
  99. }
  100. export interface FileExistsOptions {
  101. userProject?: string;
  102. }
  103. export type FileExistsResponse = [boolean];
  104. export interface FileExistsCallback {
  105. (err: Error | null, exists?: boolean): void;
  106. }
  107. export interface DeleteFileOptions {
  108. ignoreNotFound?: boolean;
  109. userProject?: string;
  110. }
  111. export type DeleteFileResponse = [unknown];
  112. export interface DeleteFileCallback {
  113. (err: Error | null, apiResponse?: unknown): void;
  114. }
  115. export type PredefinedAcl = 'authenticatedRead' | 'bucketOwnerFullControl' | 'bucketOwnerRead' | 'private' | 'projectPrivate' | 'publicRead';
  116. type PublicResumableUploadOptions = 'chunkSize' | 'highWaterMark' | 'isPartialUpload' | 'metadata' | 'origin' | 'offset' | 'predefinedAcl' | 'private' | 'public' | 'uri' | 'userProject';
  117. export interface CreateResumableUploadOptions extends Pick<resumableUpload.UploadConfig, PublicResumableUploadOptions> {
  118. /**
  119. * A CRC32C to resume from when continuing a previous upload. It is recommended
  120. * to capture the `crc32c` event from previous upload sessions to provide in
  121. * subsequent requests in order to accurately track the upload. This is **required**
  122. * when validating a final portion of the uploaded object.
  123. *
  124. * @see {@link CRC32C.from} for possible values.
  125. */
  126. resumeCRC32C?: Parameters<(typeof CRC32C)['from']>[0];
  127. preconditionOpts?: PreconditionOptions;
  128. [GCCL_GCS_CMD_KEY]?: resumableUpload.UploadConfig[typeof GCCL_GCS_CMD_KEY];
  129. }
  130. export type CreateResumableUploadResponse = [string];
  131. export interface CreateResumableUploadCallback {
  132. (err: Error | null, uri?: string): void;
  133. }
  134. export interface CreateWriteStreamOptions extends CreateResumableUploadOptions {
  135. contentType?: string;
  136. gzip?: string | boolean;
  137. resumable?: boolean;
  138. timeout?: number;
  139. validation?: string | boolean;
  140. }
  141. export interface MakeFilePrivateOptions {
  142. metadata?: FileMetadata;
  143. strict?: boolean;
  144. userProject?: string;
  145. preconditionOpts?: PreconditionOptions;
  146. }
  147. export type MakeFilePrivateResponse = [unknown];
  148. export type MakeFilePrivateCallback = SetFileMetadataCallback;
  149. export interface IsPublicCallback {
  150. (err: Error | null, resp?: boolean): void;
  151. }
  152. export type IsPublicResponse = [boolean];
  153. export type MakeFilePublicResponse = [unknown];
  154. export interface MakeFilePublicCallback {
  155. (err?: Error | null, apiResponse?: unknown): void;
  156. }
  157. export type MoveResponse = [unknown];
  158. export interface MoveCallback {
  159. (err: Error | null, destinationFile?: File | null, apiResponse?: unknown): void;
  160. }
  161. export interface MoveOptions {
  162. userProject?: string;
  163. preconditionOpts?: PreconditionOptions;
  164. }
  165. export type RenameOptions = MoveOptions;
  166. export type RenameResponse = MoveResponse;
  167. export type RenameCallback = MoveCallback;
  168. export type RotateEncryptionKeyOptions = string | Buffer | EncryptionKeyOptions;
  169. export interface EncryptionKeyOptions {
  170. encryptionKey?: string | Buffer;
  171. kmsKeyName?: string;
  172. preconditionOpts?: PreconditionOptions;
  173. }
  174. export type RotateEncryptionKeyCallback = CopyCallback;
  175. export type RotateEncryptionKeyResponse = CopyResponse;
  176. export declare enum ActionToHTTPMethod {
  177. read = "GET",
  178. write = "PUT",
  179. delete = "DELETE",
  180. resumable = "POST"
  181. }
  182. /**
  183. * @deprecated - no longer used
  184. */
  185. export declare const STORAGE_POST_POLICY_BASE_URL = "https://storage.googleapis.com";
  186. export interface FileOptions {
  187. crc32cGenerator?: CRC32CValidatorGenerator;
  188. encryptionKey?: string | Buffer;
  189. generation?: number | string;
  190. restoreToken?: string;
  191. kmsKeyName?: string;
  192. preconditionOpts?: PreconditionOptions;
  193. userProject?: string;
  194. }
  195. export interface CopyOptions {
  196. cacheControl?: string;
  197. contentEncoding?: string;
  198. contentType?: string;
  199. contentDisposition?: string;
  200. destinationKmsKeyName?: string;
  201. metadata?: {
  202. [key: string]: string | boolean | number | null;
  203. };
  204. predefinedAcl?: string;
  205. token?: string;
  206. userProject?: string;
  207. preconditionOpts?: PreconditionOptions;
  208. }
  209. export type CopyResponse = [File, unknown];
  210. export interface CopyCallback {
  211. (err: Error | null, file?: File | null, apiResponse?: unknown): void;
  212. }
  213. export type DownloadResponse = [Buffer];
  214. export type DownloadCallback = (err: RequestError | null, contents: Buffer) => void;
  215. export interface DownloadOptions extends CreateReadStreamOptions {
  216. destination?: string;
  217. }
  218. export interface CreateReadStreamOptions {
  219. userProject?: string;
  220. validation?: 'md5' | 'crc32c' | false | true;
  221. start?: number;
  222. end?: number;
  223. decompress?: boolean;
  224. [GCCL_GCS_CMD_KEY]?: string;
  225. }
  226. export interface SaveOptions extends CreateWriteStreamOptions {
  227. onUploadProgress?: (progressEvent: any) => void;
  228. }
  229. export interface SaveCallback {
  230. (err?: Error | null): void;
  231. }
  232. export interface SetFileMetadataOptions {
  233. userProject?: string;
  234. }
  235. export interface SetFileMetadataCallback {
  236. (err?: Error | null, apiResponse?: unknown): void;
  237. }
  238. export type SetFileMetadataResponse = [unknown];
  239. export type SetStorageClassResponse = [unknown];
  240. export interface SetStorageClassOptions {
  241. userProject?: string;
  242. preconditionOpts?: PreconditionOptions;
  243. }
  244. export interface SetStorageClassCallback {
  245. (err?: Error | null, apiResponse?: unknown): void;
  246. }
  247. export interface RestoreOptions extends PreconditionOptions {
  248. generation: number;
  249. restoreToken?: string;
  250. projection?: 'full' | 'noAcl';
  251. }
  252. export interface FileMetadata extends BaseMetadata {
  253. acl?: AclMetadata[] | null;
  254. bucket?: string;
  255. cacheControl?: string;
  256. componentCount?: number;
  257. contentDisposition?: string;
  258. contentEncoding?: string;
  259. contentLanguage?: string;
  260. contentType?: string;
  261. crc32c?: string;
  262. customerEncryption?: {
  263. encryptionAlgorithm?: string;
  264. keySha256?: string;
  265. };
  266. customTime?: string;
  267. eventBasedHold?: boolean | null;
  268. readonly eventBasedHoldReleaseTime?: string;
  269. generation?: string | number;
  270. restoreToken?: string;
  271. hardDeleteTime?: string;
  272. kmsKeyName?: string;
  273. md5Hash?: string;
  274. mediaLink?: string;
  275. metadata?: {
  276. [key: string]: string | boolean | number | null;
  277. };
  278. metageneration?: string | number;
  279. name?: string;
  280. owner?: {
  281. entity?: string;
  282. entityId?: string;
  283. };
  284. retention?: {
  285. retainUntilTime?: string;
  286. mode?: string;
  287. } | null;
  288. retentionExpirationTime?: string;
  289. size?: string | number;
  290. softDeleteTime?: string;
  291. storageClass?: string;
  292. temporaryHold?: boolean | null;
  293. timeCreated?: string;
  294. timeDeleted?: string;
  295. timeStorageClassUpdated?: string;
  296. updated?: string;
  297. }
  298. export declare class RequestError extends Error {
  299. code?: string;
  300. errors?: Error[];
  301. }
  302. export declare enum FileExceptionMessages {
  303. EXPIRATION_TIME_NA = "An expiration time is not available.",
  304. DESTINATION_NO_NAME = "Destination file should have a name.",
  305. INVALID_VALIDATION_FILE_RANGE = "Cannot use validation with file ranges (start/end).",
  306. MD5_NOT_AVAILABLE = "MD5 verification was specified, but is not available for the requested object. MD5 is not available for composite objects.",
  307. EQUALS_CONDITION_TWO_ELEMENTS = "Equals condition must be an array of 2 elements.",
  308. STARTS_WITH_TWO_ELEMENTS = "StartsWith condition must be an array of 2 elements.",
  309. CONTENT_LENGTH_RANGE_MIN_MAX = "ContentLengthRange must have numeric min & max fields.",
  310. DOWNLOAD_MISMATCH = "The downloaded data did not match the data from the server. To be sure the content is the same, you should download the file again.",
  311. UPLOAD_MISMATCH_DELETE_FAIL = "The uploaded data did not match the data from the server.\n As a precaution, we attempted to delete the file, but it was not successful.\n To be sure the content is the same, you should try removing the file manually,\n then uploading the file again.\n \n\nThe delete attempt failed with this message:\n\n ",
  312. UPLOAD_MISMATCH = "The uploaded data did not match the data from the server.\n As a precaution, the file has been deleted.\n To be sure the content is the same, you should try uploading the file again.",
  313. MD5_RESUMED_UPLOAD = "MD5 cannot be used with a continued resumable upload as MD5 cannot be extended from an existing value",
  314. MISSING_RESUME_CRC32C_FINAL_UPLOAD = "The CRC32C is missing for the final portion of a resumed upload, which is required for validation. Please provide `resumeCRC32C` if validation is required, or disable `validation`."
  315. }
  316. /**
  317. * A File object is created from your {@link Bucket} object using
  318. * {@link Bucket#file}.
  319. *
  320. * @class
  321. */
  322. declare class File extends ServiceObject<File, FileMetadata> {
  323. #private;
  324. acl: Acl;
  325. crc32cGenerator: CRC32CValidatorGenerator;
  326. bucket: Bucket;
  327. storage: Storage;
  328. kmsKeyName?: string;
  329. userProject?: string;
  330. signer?: URLSigner;
  331. name: string;
  332. generation?: number;
  333. restoreToken?: string;
  334. parent: Bucket;
  335. private encryptionKey?;
  336. private encryptionKeyBase64?;
  337. private encryptionKeyHash?;
  338. private encryptionKeyInterceptor?;
  339. private instanceRetryValue?;
  340. instancePreconditionOpts?: PreconditionOptions;
  341. /**
  342. * Cloud Storage uses access control lists (ACLs) to manage object and
  343. * bucket access. ACLs are the mechanism you use to share objects with other
  344. * users and allow other users to access your buckets and objects.
  345. *
  346. * An ACL consists of one or more entries, where each entry grants permissions
  347. * to an entity. Permissions define the actions that can be performed against
  348. * an object or bucket (for example, `READ` or `WRITE`); the entity defines
  349. * who the permission applies to (for example, a specific user or group of
  350. * users).
  351. *
  352. * The `acl` object on a File instance provides methods to get you a list of
  353. * the ACLs defined on your bucket, as well as set, update, and delete them.
  354. *
  355. * See {@link http://goo.gl/6qBBPO| About Access Control lists}
  356. *
  357. * @name File#acl
  358. * @mixes Acl
  359. *
  360. * @example
  361. * ```
  362. * const {Storage} = require('@google-cloud/storage');
  363. * const storage = new Storage();
  364. * const myBucket = storage.bucket('my-bucket');
  365. *
  366. * const file = myBucket.file('my-file');
  367. * //-
  368. * // Make a file publicly readable.
  369. * //-
  370. * const options = {
  371. * entity: 'allUsers',
  372. * role: storage.acl.READER_ROLE
  373. * };
  374. *
  375. * file.acl.add(options, function(err, aclObject) {});
  376. *
  377. * //-
  378. * // If the callback is omitted, we'll return a Promise.
  379. * //-
  380. * file.acl.add(options).then(function(data) {
  381. * const aclObject = data[0];
  382. * const apiResponse = data[1];
  383. * });
  384. * ```
  385. */
  386. /**
  387. * The API-formatted resource description of the file.
  388. *
  389. * Note: This is not guaranteed to be up-to-date when accessed. To get the
  390. * latest record, call the `getMetadata()` method.
  391. *
  392. * @name File#metadata
  393. * @type {object}
  394. */
  395. /**
  396. * The file's name.
  397. * @name File#name
  398. * @type {string}
  399. */
  400. /**
  401. * @callback Crc32cGeneratorToStringCallback
  402. * A method returning the CRC32C as a base64-encoded string.
  403. *
  404. * @returns {string}
  405. *
  406. * @example
  407. * Hashing the string 'data' should return 'rth90Q=='
  408. *
  409. * ```js
  410. * const buffer = Buffer.from('data');
  411. * crc32c.update(buffer);
  412. * crc32c.toString(); // 'rth90Q=='
  413. * ```
  414. **/
  415. /**
  416. * @callback Crc32cGeneratorValidateCallback
  417. * A method validating a base64-encoded CRC32C string.
  418. *
  419. * @param {string} [value] base64-encoded CRC32C string to validate
  420. * @returns {boolean}
  421. *
  422. * @example
  423. * Should return `true` if the value matches, `false` otherwise
  424. *
  425. * ```js
  426. * const buffer = Buffer.from('data');
  427. * crc32c.update(buffer);
  428. * crc32c.validate('DkjKuA=='); // false
  429. * crc32c.validate('rth90Q=='); // true
  430. * ```
  431. **/
  432. /**
  433. * @callback Crc32cGeneratorUpdateCallback
  434. * A method for passing `Buffer`s for CRC32C generation.
  435. *
  436. * @param {Buffer} [data] data to update CRC32C value with
  437. * @returns {undefined}
  438. *
  439. * @example
  440. * Hashing buffers from 'some ' and 'text\n'
  441. *
  442. * ```js
  443. * const buffer1 = Buffer.from('some ');
  444. * crc32c.update(buffer1);
  445. *
  446. * const buffer2 = Buffer.from('text\n');
  447. * crc32c.update(buffer2);
  448. *
  449. * crc32c.toString(); // 'DkjKuA=='
  450. * ```
  451. **/
  452. /**
  453. * @typedef {object} CRC32CValidator
  454. * @property {Crc32cGeneratorToStringCallback}
  455. * @property {Crc32cGeneratorValidateCallback}
  456. * @property {Crc32cGeneratorUpdateCallback}
  457. */
  458. /**
  459. * @callback Crc32cGeneratorCallback
  460. * @returns {CRC32CValidator}
  461. */
  462. /**
  463. * @typedef {object} FileOptions Options passed to the File constructor.
  464. * @property {string} [encryptionKey] A custom encryption key.
  465. * @property {number} [generation] Generation to scope the file to.
  466. * @property {string} [kmsKeyName] Cloud KMS Key used to encrypt this
  467. * object, if the object is encrypted by such a key. Limited availability;
  468. * usable only by enabled projects.
  469. * @property {string} [userProject] The ID of the project which will be
  470. * billed for all requests made from File object.
  471. * @property {Crc32cGeneratorCallback} [callback] A function that generates a CRC32C Validator. Defaults to {@link CRC32C}
  472. */
  473. /**
  474. * Constructs a file object.
  475. *
  476. * @param {Bucket} bucket The Bucket instance this file is
  477. * attached to.
  478. * @param {string} name The name of the remote file.
  479. * @param {FileOptions} [options] Configuration options.
  480. * @example
  481. * ```
  482. * const {Storage} = require('@google-cloud/storage');
  483. * const storage = new Storage();
  484. * const myBucket = storage.bucket('my-bucket');
  485. *
  486. * const file = myBucket.file('my-file');
  487. * ```
  488. */
  489. constructor(bucket: Bucket, name: string, options?: FileOptions);
  490. /**
  491. * The object's Cloud Storage URI (`gs://`)
  492. *
  493. * @example
  494. * ```ts
  495. * const {Storage} = require('@google-cloud/storage');
  496. * const storage = new Storage();
  497. * const bucket = storage.bucket('my-bucket');
  498. * const file = bucket.file('image.png');
  499. *
  500. * // `gs://my-bucket/image.png`
  501. * const href = file.cloudStorageURI.href;
  502. * ```
  503. */
  504. get cloudStorageURI(): URL;
  505. /**
  506. * A helper method for determining if a request should be retried based on preconditions.
  507. * This should only be used for methods where the idempotency is determined by
  508. * `ifGenerationMatch`
  509. * @private
  510. *
  511. * A request should not be retried under the following conditions:
  512. * - if precondition option `ifGenerationMatch` is not set OR
  513. * - if `idempotencyStrategy` is set to `RetryNever`
  514. */
  515. private shouldRetryBasedOnPreconditionAndIdempotencyStrat;
  516. copy(destination: string | Bucket | File, options?: CopyOptions): Promise<CopyResponse>;
  517. copy(destination: string | Bucket | File, callback: CopyCallback): void;
  518. copy(destination: string | Bucket | File, options: CopyOptions, callback: CopyCallback): void;
  519. /**
  520. * @typedef {object} CreateReadStreamOptions Configuration options for File#createReadStream.
  521. * @property {string} [userProject] The ID of the project which will be
  522. * billed for the request.
  523. * @property {string|boolean} [validation] Possible values: `"md5"`,
  524. * `"crc32c"`, or `false`. By default, data integrity is validated with a
  525. * CRC32c checksum. You may use MD5 if preferred, but that hash is not
  526. * supported for composite objects. An error will be raised if MD5 is
  527. * specified but is not available. You may also choose to skip validation
  528. * completely, however this is **not recommended**.
  529. * @property {number} [start] A byte offset to begin the file's download
  530. * from. Default is 0. NOTE: Byte ranges are inclusive; that is,
  531. * `options.start = 0` and `options.end = 999` represent the first 1000
  532. * bytes in a file or object. NOTE: when specifying a byte range, data
  533. * integrity is not available.
  534. * @property {number} [end] A byte offset to stop reading the file at.
  535. * NOTE: Byte ranges are inclusive; that is, `options.start = 0` and
  536. * `options.end = 999` represent the first 1000 bytes in a file or object.
  537. * NOTE: when specifying a byte range, data integrity is not available.
  538. * @property {boolean} [decompress=true] Disable auto decompression of the
  539. * received data. By default this option is set to `true`.
  540. * Applicable in cases where the data was uploaded with
  541. * `gzip: true` option. See {@link File#createWriteStream}.
  542. */
  543. /**
  544. * Create a readable stream to read the contents of the remote file. It can be
  545. * piped to a writable stream or listened to for 'data' events to read a
  546. * file's contents.
  547. *
  548. * In the unlikely event there is a mismatch between what you downloaded and
  549. * the version in your Bucket, your error handler will receive an error with
  550. * code "CONTENT_DOWNLOAD_MISMATCH". If you receive this error, the best
  551. * recourse is to try downloading the file again.
  552. *
  553. * NOTE: Readable streams will emit the `end` event when the file is fully
  554. * downloaded.
  555. *
  556. * @param {CreateReadStreamOptions} [options] Configuration options.
  557. * @returns {ReadableStream}
  558. *
  559. * @example
  560. * ```
  561. * //-
  562. * // <h4>Downloading a File</h4>
  563. * //
  564. * // The example below demonstrates how we can reference a remote file, then
  565. * // pipe its contents to a local file. This is effectively creating a local
  566. * // backup of your remote data.
  567. * //-
  568. * const {Storage} = require('@google-cloud/storage');
  569. * const storage = new Storage();
  570. * const bucket = storage.bucket('my-bucket');
  571. *
  572. * const fs = require('fs');
  573. * const remoteFile = bucket.file('image.png');
  574. * const localFilename = '/Users/stephen/Photos/image.png';
  575. *
  576. * remoteFile.createReadStream()
  577. * .on('error', function(err) {})
  578. * .on('response', function(response) {
  579. * // Server connected and responded with the specified status and headers.
  580. * })
  581. * .on('end', function() {
  582. * // The file is fully downloaded.
  583. * })
  584. * .pipe(fs.createWriteStream(localFilename));
  585. *
  586. * //-
  587. * // To limit the downloaded data to only a byte range, pass an options
  588. * // object.
  589. * //-
  590. * const logFile = myBucket.file('access_log');
  591. * logFile.createReadStream({
  592. * start: 10000,
  593. * end: 20000
  594. * })
  595. * .on('error', function(err) {})
  596. * .pipe(fs.createWriteStream('/Users/stephen/logfile.txt'));
  597. *
  598. * //-
  599. * // To read a tail byte range, specify only `options.end` as a negative
  600. * // number.
  601. * //-
  602. * const logFile = myBucket.file('access_log');
  603. * logFile.createReadStream({
  604. * end: -100
  605. * })
  606. * .on('error', function(err) {})
  607. * .pipe(fs.createWriteStream('/Users/stephen/logfile.txt'));
  608. * ```
  609. */
  610. createReadStream(options?: CreateReadStreamOptions): Readable;
  611. createResumableUpload(options?: CreateResumableUploadOptions): Promise<CreateResumableUploadResponse>;
  612. createResumableUpload(options: CreateResumableUploadOptions, callback: CreateResumableUploadCallback): void;
  613. createResumableUpload(callback: CreateResumableUploadCallback): void;
  614. /**
  615. * @typedef {object} CreateWriteStreamOptions Configuration options for File#createWriteStream().
  616. * @property {string} [contentType] Alias for
  617. * `options.metadata.contentType`. If set to `auto`, the file name is used
  618. * to determine the contentType.
  619. * @property {string|boolean} [gzip] If true, automatically gzip the file.
  620. * If set to `auto`, the contentType is used to determine if the file
  621. * should be gzipped. This will set `options.metadata.contentEncoding` to
  622. * `gzip` if necessary.
  623. * @property {object} [metadata] See the examples below or
  624. * {@link https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request_properties_JSON| Objects: insert request body}
  625. * for more details.
  626. * @property {number} [offset] The starting byte of the upload stream, for
  627. * resuming an interrupted upload. Defaults to 0.
  628. * @property {string} [predefinedAcl] Apply a predefined set of access
  629. * controls to this object.
  630. *
  631. * Acceptable values are:
  632. * - **`authenticatedRead`** - Object owner gets `OWNER` access, and
  633. * `allAuthenticatedUsers` get `READER` access.
  634. *
  635. * - **`bucketOwnerFullControl`** - Object owner gets `OWNER` access, and
  636. * project team owners get `OWNER` access.
  637. *
  638. * - **`bucketOwnerRead`** - Object owner gets `OWNER` access, and project
  639. * team owners get `READER` access.
  640. *
  641. * - **`private`** - Object owner gets `OWNER` access.
  642. *
  643. * - **`projectPrivate`** - Object owner gets `OWNER` access, and project
  644. * team members get access according to their roles.
  645. *
  646. * - **`publicRead`** - Object owner gets `OWNER` access, and `allUsers`
  647. * get `READER` access.
  648. * @property {boolean} [private] Make the uploaded file private. (Alias for
  649. * `options.predefinedAcl = 'private'`)
  650. * @property {boolean} [public] Make the uploaded file public. (Alias for
  651. * `options.predefinedAcl = 'publicRead'`)
  652. * @property {boolean} [resumable] Force a resumable upload. NOTE: When
  653. * working with streams, the file format and size is unknown until it's
  654. * completely consumed. Because of this, it's best for you to be explicit
  655. * for what makes sense given your input.
  656. * @property {number} [timeout=60000] Set the HTTP request timeout in
  657. * milliseconds. This option is not available for resumable uploads.
  658. * Default: `60000`
  659. * @property {string} [uri] The URI for an already-created resumable
  660. * upload. See {@link File#createResumableUpload}.
  661. * @property {string} [userProject] The ID of the project which will be
  662. * billed for the request.
  663. * @property {string|boolean} [validation] Possible values: `"md5"`,
  664. * `"crc32c"`, or `false`. By default, data integrity is validated with a
  665. * CRC32c checksum. You may use MD5 if preferred, but that hash is not
  666. * supported for composite objects. An error will be raised if MD5 is
  667. * specified but is not available. You may also choose to skip validation
  668. * completely, however this is **not recommended**. In addition to specifying
  669. * validation type, providing `metadata.crc32c` or `metadata.md5Hash` will
  670. * cause the server to perform validation in addition to client validation.
  671. * NOTE: Validation is automatically skipped for objects that were
  672. * uploaded using the `gzip` option and have already compressed content.
  673. */
  674. /**
  675. * Create a writable stream to overwrite the contents of the file in your
  676. * bucket.
  677. *
  678. * A File object can also be used to create files for the first time.
  679. *
  680. * Resumable uploads are automatically enabled and must be shut off explicitly
  681. * by setting `options.resumable` to `false`.
  682. *
  683. *
  684. * <p class="notice">
  685. * There is some overhead when using a resumable upload that can cause
  686. * noticeable performance degradation while uploading a series of small
  687. * files. When uploading files less than 10MB, it is recommended that the
  688. * resumable feature is disabled.
  689. * </p>
  690. *
  691. * NOTE: Writable streams will emit the `finish` event when the file is fully
  692. * uploaded.
  693. *
  694. * See {@link https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload Upload Options (Simple or Resumable)}
  695. * See {@link https://cloud.google.com/storage/docs/json_api/v1/objects/insert Objects: insert API Documentation}
  696. *
  697. * @param {CreateWriteStreamOptions} [options] Configuration options.
  698. * @returns {WritableStream}
  699. *
  700. * @example
  701. * ```
  702. * const fs = require('fs');
  703. * const {Storage} = require('@google-cloud/storage');
  704. * const storage = new Storage();
  705. * const myBucket = storage.bucket('my-bucket');
  706. *
  707. * const file = myBucket.file('my-file');
  708. *
  709. * //-
  710. * // <h4>Uploading a File</h4>
  711. * //
  712. * // Now, consider a case where we want to upload a file to your bucket. You
  713. * // have the option of using {@link Bucket#upload}, but that is just
  714. * // a convenience method which will do the following.
  715. * //-
  716. * fs.createReadStream('/Users/stephen/Photos/birthday-at-the-zoo/panda.jpg')
  717. * .pipe(file.createWriteStream())
  718. * .on('error', function(err) {})
  719. * .on('finish', function() {
  720. * // The file upload is complete.
  721. * });
  722. *
  723. * //-
  724. * // <h4>Uploading a File with gzip compression</h4>
  725. * //-
  726. * fs.createReadStream('/Users/stephen/site/index.html')
  727. * .pipe(file.createWriteStream({ gzip: true }))
  728. * .on('error', function(err) {})
  729. * .on('finish', function() {
  730. * // The file upload is complete.
  731. * });
  732. *
  733. * //-
  734. * // Downloading the file with `createReadStream` will automatically decode
  735. * // the file.
  736. * //-
  737. *
  738. * //-
  739. * // <h4>Uploading a File with Metadata</h4>
  740. * //
  741. * // One last case you may run into is when you want to upload a file to your
  742. * // bucket and set its metadata at the same time. Like above, you can use
  743. * // {@link Bucket#upload} to do this, which is just a wrapper around
  744. * // the following.
  745. * //-
  746. * fs.createReadStream('/Users/stephen/Photos/birthday-at-the-zoo/panda.jpg')
  747. * .pipe(file.createWriteStream({
  748. * metadata: {
  749. * contentType: 'image/jpeg',
  750. * metadata: {
  751. * custom: 'metadata'
  752. * }
  753. * }
  754. * }))
  755. * .on('error', function(err) {})
  756. * .on('finish', function() {
  757. * // The file upload is complete.
  758. * });
  759. * ```
  760. *
  761. * //-
  762. * // <h4>Continuing a Resumable Upload</h4>
  763. * //
  764. * // One can capture a `uri` from a resumable upload to reuse later.
  765. * // Additionally, for validation, one can also capture and pass `crc32c`.
  766. * //-
  767. * let uri: string | undefined = undefined;
  768. * let resumeCRC32C: string | undefined = undefined;
  769. *
  770. * fs.createWriteStream()
  771. * .on('uri', link => {uri = link})
  772. * .on('crc32', crc32c => {resumeCRC32C = crc32c});
  773. *
  774. * // later...
  775. * fs.createWriteStream({uri, resumeCRC32C});
  776. */
  777. createWriteStream(options?: CreateWriteStreamOptions): Writable;
  778. /**
  779. * Delete the object.
  780. *
  781. * @param {function=} callback - The callback function.
  782. * @param {?error} callback.err - An error returned while making this request.
  783. * @param {object} callback.apiResponse - The full API response.
  784. */
  785. delete(options?: DeleteOptions): Promise<[r.Response]>;
  786. delete(options: DeleteOptions, callback: DeleteCallback): void;
  787. delete(callback: DeleteCallback): void;
  788. download(options?: DownloadOptions): Promise<DownloadResponse>;
  789. download(options: DownloadOptions, callback: DownloadCallback): void;
  790. download(callback: DownloadCallback): void;
  791. /**
  792. * The Storage API allows you to use a custom key for server-side encryption.
  793. *
  794. * See {@link https://cloud.google.com/storage/docs/encryption#customer-supplied| Customer-supplied Encryption Keys}
  795. *
  796. * @param {string|buffer} encryptionKey An AES-256 encryption key.
  797. * @returns {File}
  798. *
  799. * @example
  800. * ```
  801. * const crypto = require('crypto');
  802. * const {Storage} = require('@google-cloud/storage');
  803. * const storage = new Storage();
  804. * const myBucket = storage.bucket('my-bucket');
  805. *
  806. * const encryptionKey = crypto.randomBytes(32);
  807. *
  808. * const fileWithCustomEncryption = myBucket.file('my-file');
  809. * fileWithCustomEncryption.setEncryptionKey(encryptionKey);
  810. *
  811. * const fileWithoutCustomEncryption = myBucket.file('my-file');
  812. *
  813. * fileWithCustomEncryption.save('data', function(err) {
  814. * // Try to download with the File object that hasn't had
  815. * // `setEncryptionKey()` called:
  816. * fileWithoutCustomEncryption.download(function(err) {
  817. * // We will receive an error:
  818. * // err.message === 'Bad Request'
  819. *
  820. * // Try again with the File object we called `setEncryptionKey()` on:
  821. * fileWithCustomEncryption.download(function(err, contents) {
  822. * // contents.toString() === 'data'
  823. * });
  824. * });
  825. * });
  826. *
  827. * ```
  828. * @example <caption>include:samples/encryption.js</caption>
  829. * region_tag:storage_upload_encrypted_file
  830. * Example of uploading an encrypted file:
  831. *
  832. * @example <caption>include:samples/encryption.js</caption>
  833. * region_tag:storage_download_encrypted_file
  834. * Example of downloading an encrypted file:
  835. */
  836. setEncryptionKey(encryptionKey: string | Buffer): this;
  837. /**
  838. * Gets a reference to a Cloud Storage {@link File} file from the provided URL in string format.
  839. * @param {string} publicUrlOrGsUrl the URL as a string. Must be of the format gs://bucket/file
  840. * or https://storage.googleapis.com/bucket/file.
  841. * @param {Storage} storageInstance an instance of a Storage object.
  842. * @param {FileOptions} [options] Configuration options
  843. * @returns {File}
  844. */
  845. static from(publicUrlOrGsUrl: string, storageInstance: Storage, options?: FileOptions): File;
  846. get(options?: GetFileOptions): Promise<GetResponse<File>>;
  847. get(callback: InstanceResponseCallback<File>): void;
  848. get(options: GetFileOptions, callback: InstanceResponseCallback<File>): void;
  849. getExpirationDate(): Promise<GetExpirationDateResponse>;
  850. getExpirationDate(callback: GetExpirationDateCallback): void;
  851. generateSignedPostPolicyV2(options: GenerateSignedPostPolicyV2Options): Promise<GenerateSignedPostPolicyV2Response>;
  852. generateSignedPostPolicyV2(options: GenerateSignedPostPolicyV2Options, callback: GenerateSignedPostPolicyV2Callback): void;
  853. generateSignedPostPolicyV2(callback: GenerateSignedPostPolicyV2Callback): void;
  854. generateSignedPostPolicyV4(options: GenerateSignedPostPolicyV4Options): Promise<GenerateSignedPostPolicyV4Response>;
  855. generateSignedPostPolicyV4(options: GenerateSignedPostPolicyV4Options, callback: GenerateSignedPostPolicyV4Callback): void;
  856. generateSignedPostPolicyV4(callback: GenerateSignedPostPolicyV4Callback): void;
  857. getSignedUrl(cfg: GetSignedUrlConfig): Promise<GetSignedUrlResponse>;
  858. getSignedUrl(cfg: GetSignedUrlConfig, callback: GetSignedUrlCallback): void;
  859. isPublic(): Promise<IsPublicResponse>;
  860. isPublic(callback: IsPublicCallback): void;
  861. makePrivate(options?: MakeFilePrivateOptions): Promise<MakeFilePrivateResponse>;
  862. makePrivate(callback: MakeFilePrivateCallback): void;
  863. makePrivate(options: MakeFilePrivateOptions, callback: MakeFilePrivateCallback): void;
  864. makePublic(): Promise<MakeFilePublicResponse>;
  865. makePublic(callback: MakeFilePublicCallback): void;
  866. /**
  867. * The public URL of this File
  868. * Use {@link File#makePublic} to enable anonymous access via the returned URL.
  869. *
  870. * @returns {string}
  871. *
  872. * @example
  873. * ```
  874. * const {Storage} = require('@google-cloud/storage');
  875. * const storage = new Storage();
  876. * const bucket = storage.bucket('albums');
  877. * const file = bucket.file('my-file');
  878. *
  879. * // publicUrl will be "https://storage.googleapis.com/albums/my-file"
  880. * const publicUrl = file.publicUrl();
  881. * ```
  882. */
  883. publicUrl(): string;
  884. move(destination: string | Bucket | File, options?: MoveOptions): Promise<MoveResponse>;
  885. move(destination: string | Bucket | File, callback: MoveCallback): void;
  886. move(destination: string | Bucket | File, options: MoveOptions, callback: MoveCallback): void;
  887. rename(destinationFile: string | File, options?: RenameOptions): Promise<RenameResponse>;
  888. rename(destinationFile: string | File, callback: RenameCallback): void;
  889. rename(destinationFile: string | File, options: RenameOptions, callback: RenameCallback): void;
  890. /**
  891. * @typedef {object} RestoreOptions Options for File#restore(). See an
  892. * {@link https://cloud.google.com/storage/docs/json_api/v1/objects#resource| Object resource}.
  893. * @param {string} [userProject] The ID of the project which will be
  894. * billed for the request.
  895. * @param {number} [generation] If present, selects a specific revision of this object.
  896. * @param {string} [restoreToken] Returns an option that must be specified when getting a soft-deleted object from an HNS-enabled
  897. * bucket that has a naming and generation conflict with another object in the same bucket.
  898. * @param {string} [projection] Specifies the set of properties to return. If used, must be 'full' or 'noAcl'.
  899. * @param {string | number} [ifGenerationMatch] Request proceeds if the generation of the target resource
  900. * matches the value used in the precondition.
  901. * If the values don't match, the request fails with a 412 Precondition Failed response.
  902. * @param {string | number} [ifGenerationNotMatch] Request proceeds if the generation of the target resource does
  903. * not match the value used in the precondition. If the values match, the request fails with a 304 Not Modified response.
  904. * @param {string | number} [ifMetagenerationMatch] Request proceeds if the meta-generation of the target resource
  905. * matches the value used in the precondition.
  906. * If the values don't match, the request fails with a 412 Precondition Failed response.
  907. * @param {string | number} [ifMetagenerationNotMatch] Request proceeds if the meta-generation of the target resource does
  908. * not match the value used in the precondition. If the values match, the request fails with a 304 Not Modified response.
  909. */
  910. /**
  911. * Restores a soft-deleted file
  912. * @param {RestoreOptions} options Restore options.
  913. * @returns {Promise<File>}
  914. */
  915. restore(options: RestoreOptions): Promise<File>;
  916. request(reqOpts: DecorateRequestOptions): Promise<RequestResponse>;
  917. request(reqOpts: DecorateRequestOptions, callback: BodyResponseCallback): void;
  918. rotateEncryptionKey(options?: RotateEncryptionKeyOptions): Promise<RotateEncryptionKeyResponse>;
  919. rotateEncryptionKey(callback: RotateEncryptionKeyCallback): void;
  920. rotateEncryptionKey(options: RotateEncryptionKeyOptions, callback: RotateEncryptionKeyCallback): void;
  921. save(data: SaveData, options?: SaveOptions): Promise<void>;
  922. save(data: SaveData, callback: SaveCallback): void;
  923. save(data: SaveData, options: SaveOptions, callback: SaveCallback): void;
  924. setMetadata(metadata: FileMetadata, options?: SetMetadataOptions): Promise<SetMetadataResponse<FileMetadata>>;
  925. setMetadata(metadata: FileMetadata, callback: MetadataCallback<FileMetadata>): void;
  926. setMetadata(metadata: FileMetadata, options: SetMetadataOptions, callback: MetadataCallback<FileMetadata>): void;
  927. setStorageClass(storageClass: string, options?: SetStorageClassOptions): Promise<SetStorageClassResponse>;
  928. setStorageClass(storageClass: string, options: SetStorageClassOptions, callback: SetStorageClassCallback): void;
  929. setStorageClass(storageClass: string, callback?: SetStorageClassCallback): void;
  930. /**
  931. * Set a user project to be billed for all requests made from this File
  932. * object.
  933. *
  934. * @param {string} userProject The user project.
  935. *
  936. * @example
  937. * ```
  938. * const {Storage} = require('@google-cloud/storage');
  939. * const storage = new Storage();
  940. * const bucket = storage.bucket('albums');
  941. * const file = bucket.file('my-file');
  942. *
  943. * file.setUserProject('grape-spaceship-123');
  944. * ```
  945. */
  946. setUserProject(userProject: string): void;
  947. /**
  948. * This creates a resumable-upload upload stream.
  949. *
  950. * @param {Duplexify} stream - Duplexify stream of data to pipe to the file.
  951. * @param {object=} options - Configuration object.
  952. *
  953. * @private
  954. */
  955. startResumableUpload_(dup: Duplexify, options?: CreateResumableUploadOptions): void;
  956. /**
  957. * Takes a readable stream and pipes it to a remote file. Unlike
  958. * `startResumableUpload_`, which uses the resumable upload technique, this
  959. * method uses a simple upload (all or nothing).
  960. *
  961. * @param {Duplexify} dup - Duplexify stream of data to pipe to the file.
  962. * @param {object=} options - Configuration object.
  963. *
  964. * @private
  965. */
  966. startSimpleUpload_(dup: Duplexify, options?: CreateWriteStreamOptions): void;
  967. disableAutoRetryConditionallyIdempotent_(coreOpts: any, methodType: AvailableServiceObjectMethods, localPreconditionOptions?: PreconditionOptions): void;
  968. private getBufferFromReadable;
  969. }
  970. /**
  971. * Reference to the {@link File} class.
  972. * @name module:@google-cloud/storage.File
  973. * @see File
  974. */
  975. export { File };