UNPKG

7.34 kBTypeScriptView Raw
1/*!
2 * Copyright 2018 Google LLC
3 *
4 * Use of this source code is governed by an MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT.
7 */
8/// <reference types="node" />
9import * as ConfigStore from 'configstore';
10import { GaxiosOptions, GaxiosPromise, GaxiosResponse } from 'gaxios';
11import { GoogleAuthOptions } from 'google-auth-library';
12import * as Pumpify from 'pumpify';
13export declare const PROTOCOL_REGEX: RegExp;
14export interface ErrorWithCode extends Error {
15 code: number;
16}
17export declare type CreateUriCallback = (err: Error | null, uri?: string) => void;
18export interface Encryption {
19 key: {};
20 hash: {};
21}
22export declare type PredefinedAcl = 'authenticatedRead' | 'bucketOwnerFullControl' | 'bucketOwnerRead' | 'private' | 'projectPrivate' | 'publicRead';
23export interface QueryParameters {
24 contentEncoding?: string;
25 ifGenerationMatch?: number;
26 ifGenerationNotMatch?: number;
27 ifMetagenerationMatch?: number;
28 ifMetagenerationNotMatch?: number;
29 kmsKeyName?: string;
30 predefinedAcl?: PredefinedAcl;
31 projection?: 'full' | 'noAcl';
32 userProject?: string;
33}
34export interface UploadConfig {
35 /**
36 * The API endpoint used for the request.
37 * Defaults to `storage.googleapis.com`.
38 * **Warning**:
39 * If this value does not match the pattern *.googleapis.com,
40 * an emulator context will be assumed and authentication will be bypassed.
41 */
42 apiEndpoint?: string;
43 /**
44 * The name of the destination bucket.
45 */
46 bucket: string;
47 /**
48 * The name of the destination file.
49 */
50 file: string;
51 /**
52 * The GoogleAuthOptions passed to google-auth-library
53 */
54 authConfig?: GoogleAuthOptions;
55 /**
56 * If you want to re-use an auth client from google-auto-auth, pass an
57 * instance here.
58 * Defaults to GoogleAuth and gets automatically overridden if an
59 * emulator context is detected.
60 */
61 authClient?: {
62 request: <T = any>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
63 };
64 /**
65 * Where the gcs-resumable-upload configuration file should be stored on your
66 * system. This maps to the configstore option by the same name.
67 */
68 configPath?: string;
69 /**
70 * For each API request we send, you may specify custom request options that
71 * we'll add onto the request. The request options follow the gaxios API:
72 * https://github.com/googleapis/gaxios#request-options.
73 */
74 customRequestOptions?: GaxiosOptions;
75 /**
76 * This will cause the upload to fail if the current generation of the remote
77 * object does not match the one provided here.
78 */
79 generation?: number;
80 /**
81 * A customer-supplied encryption key. See
82 * https://cloud.google.com/storage/docs/encryption#customer-supplied.
83 */
84 key?: string | Buffer;
85 /**
86 * Resource name of the Cloud KMS key, of the form
87 * `projects/my-project/locations/global/keyRings/my-kr/cryptoKeys/my-key`,
88 * that will be used to encrypt the object. Overrides the object metadata's
89 * `kms_key_name` value, if any.
90 */
91 kmsKeyName?: string;
92 /**
93 * Any metadata you wish to set on the object.
94 */
95 metadata?: ConfigMetadata;
96 /**
97 * The starting byte of the upload stream, for resuming an interrupted upload.
98 * See
99 * https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#resume-upload.
100 */
101 offset?: number;
102 /**
103 * Set an Origin header when creating the resumable upload URI.
104 */
105 origin?: string;
106 /**
107 * Specify query parameters that go along with the initial upload request. See
108 * https://cloud.google.com/storage/docs/json_api/v1/objects/insert#parameters
109 */
110 params?: QueryParameters;
111 /**
112 * Apply a predefined set of access controls to the created file.
113 */
114 predefinedAcl?: PredefinedAcl;
115 /**
116 * Make the uploaded file private. (Alias for config.predefinedAcl =
117 * 'private')
118 */
119 private?: boolean;
120 /**
121 * Make the uploaded file public. (Alias for config.predefinedAcl =
122 * 'publicRead')
123 */
124 public?: boolean;
125 /**
126 * If you already have a resumable URI from a previously-created resumable
127 * upload, just pass it in here and we'll use that.
128 */
129 uri?: string;
130 /**
131 * If the bucket being accessed has requesterPays functionality enabled, this
132 * can be set to control which project is billed for the access of this file.
133 */
134 userProject?: string;
135 /**
136 * Configuration options for retrying retriable errors.
137 */
138 retryOptions?: RetryOptions;
139}
140export interface ConfigMetadata {
141 [key: string]: any;
142 /**
143 * Set the length of the file being uploaded.
144 */
145 contentLength?: number;
146 /**
147 * Set the content type of the incoming data.
148 */
149 contentType?: string;
150}
151export interface RetryOptions {
152 retryDelayMultiplier?: number;
153 totalTimeout?: number;
154 maxRetryDelay?: number;
155 autoRetry?: boolean;
156 maxRetries?: number;
157 retryableErrorFn?: (err: ApiError) => boolean;
158}
159export interface ApiError extends Error {
160 code?: number;
161}
162export declare class Upload extends Pumpify {
163 bucket: string;
164 file: string;
165 apiEndpoint: string;
166 baseURI: string;
167 authConfig?: {
168 scopes?: string[];
169 };
170 authClient: {
171 request: <T = any>(opts: GaxiosOptions) => Promise<GaxiosResponse<T>> | GaxiosPromise<T>;
172 };
173 cacheKey: string;
174 customRequestOptions: GaxiosOptions;
175 generation?: number;
176 key?: string | Buffer;
177 kmsKeyName?: string;
178 metadata: ConfigMetadata;
179 offset?: number;
180 origin?: string;
181 params: QueryParameters;
182 predefinedAcl?: PredefinedAcl;
183 private?: boolean;
184 public?: boolean;
185 uri?: string;
186 userProject?: string;
187 encryption?: Encryption;
188 configStore: ConfigStore;
189 uriProvidedManually: boolean;
190 numBytesWritten: number;
191 numRetries: number;
192 contentLength: number | '*';
193 retryLimit: number;
194 maxRetryDelay: number;
195 retryDelayMultiplier: number;
196 maxRetryTotalTimeout: number;
197 timeOfFirstRequest: number;
198 retryableErrorFn?: (err: ApiError) => boolean;
199 private bufferStream?;
200 private offsetStream?;
201 constructor(cfg: UploadConfig);
202 createURI(): Promise<string>;
203 createURI(callback: CreateUriCallback): void;
204 protected createURIAsync(): Promise<string>;
205 private continueUploading;
206 private startUploading;
207 private onChunk;
208 private getAndSetOffset;
209 private makeRequest;
210 private makeRequestStream;
211 private restart;
212 private get;
213 private set;
214 deleteConfig(): void;
215 /**
216 * @return {bool} is the request good?
217 */
218 private onResponse;
219 /**
220 * @param resp GaxiosResponse object from previous attempt
221 */
222 private attemptDelayedRetry;
223 /**
224 * @returns {number} the amount of time to wait before retrying the request
225 */
226 private getRetryDelay;
227 private sanitizeEndpoint;
228}
229export declare function upload(cfg: UploadConfig): Upload;
230export declare function createURI(cfg: UploadConfig): Promise<string>;
231export declare function createURI(cfg: UploadConfig, callback: CreateUriCallback): void;