UNPKG

12.2 kBTypeScriptView Raw
1/*!
2 * Copyright 2022 Google LLC. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Bucket, UploadOptions, UploadResponse } from './bucket.js';
17import { DownloadOptions, DownloadResponse, File } from './file.js';
18import { GaxiosResponse } from 'gaxios';
19export interface UploadManyFilesOptions {
20 concurrencyLimit?: number;
21 customDestinationBuilder?(path: string, options: UploadManyFilesOptions): string;
22 skipIfExists?: boolean;
23 prefix?: string;
24 passthroughOptions?: Omit<UploadOptions, 'destination'>;
25}
26export interface DownloadManyFilesOptions {
27 concurrencyLimit?: number;
28 prefix?: string;
29 stripPrefix?: string;
30 passthroughOptions?: DownloadOptions;
31 skipIfExists?: boolean;
32}
33export interface DownloadFileInChunksOptions {
34 concurrencyLimit?: number;
35 chunkSizeBytes?: number;
36 destination?: string;
37 validation?: 'crc32c' | false;
38 noReturnData?: boolean;
39}
40export interface UploadFileInChunksOptions {
41 concurrencyLimit?: number;
42 chunkSizeBytes?: number;
43 uploadName?: string;
44 maxQueueSize?: number;
45 uploadId?: string;
46 autoAbortFailure?: boolean;
47 partsMap?: Map<number, string>;
48 validation?: 'md5' | false;
49 headers?: {
50 [key: string]: string;
51 };
52}
53export interface MultiPartUploadHelper {
54 bucket: Bucket;
55 fileName: string;
56 uploadId?: string;
57 partsMap?: Map<number, string>;
58 initiateUpload(headers?: {
59 [key: string]: string;
60 }): Promise<void>;
61 uploadPart(partNumber: number, chunk: Buffer, validation?: 'md5' | false): Promise<void>;
62 completeUpload(): Promise<GaxiosResponse | undefined>;
63 abortUpload(): Promise<void>;
64}
65export type MultiPartHelperGenerator = (bucket: Bucket, fileName: string, uploadId?: string, partsMap?: Map<number, string>) => MultiPartUploadHelper;
66export declare class MultiPartUploadError extends Error {
67 private uploadId;
68 private partsMap;
69 constructor(message: string, uploadId: string, partsMap: Map<number, string>);
70}
71/**
72 * Create a TransferManager object to perform parallel transfer operations on a Cloud Storage bucket.
73 *
74 * @class
75 * @hideconstructor
76 *
77 * @param {Bucket} bucket A {@link Bucket} instance
78 *
79 */
80export declare class TransferManager {
81 bucket: Bucket;
82 constructor(bucket: Bucket);
83 /**
84 * @typedef {object} UploadManyFilesOptions
85 * @property {number} [concurrencyLimit] The number of concurrently executing promises
86 * to use when uploading the files.
87 * @property {Function} [customDestinationBuilder] A fuction that will take the current path of a local file
88 * and return a string representing a custom path to be used to upload the file to GCS.
89 * @property {boolean} [skipIfExists] Do not upload the file if it already exists in
90 * the bucket. This will set the precondition ifGenerationMatch = 0.
91 * @property {string} [prefix] A prefix to append to all of the uploaded files.
92 * @property {object} [passthroughOptions] {@link UploadOptions} Options to be passed through
93 * to each individual upload operation.
94 *
95 */
96 /**
97 * Upload multiple files in parallel to the bucket. This is a convenience method
98 * that utilizes {@link Bucket#upload} to perform the upload.
99 *
100 * @param {array | string} [filePathsOrDirectory] An array of fully qualified paths to the files or a directory name.
101 * If a directory name is provided, the directory will be recursively walked and all files will be added to the upload list.
102 * to be uploaded to the bucket
103 * @param {UploadManyFilesOptions} [options] Configuration options.
104 * @returns {Promise<UploadResponse[]>}
105 *
106 * @example
107 * ```
108 * const {Storage} = require('@google-cloud/storage');
109 * const storage = new Storage();
110 * const bucket = storage.bucket('my-bucket');
111 * const transferManager = new TransferManager(bucket);
112 *
113 * //-
114 * // Upload multiple files in parallel.
115 * //-
116 * const response = await transferManager.uploadManyFiles(['/local/path/file1.txt, 'local/path/file2.txt']);
117 * // Your bucket now contains:
118 * // - "local/path/file1.txt" (with the contents of '/local/path/file1.txt')
119 * // - "local/path/file2.txt" (with the contents of '/local/path/file2.txt')
120 * const response = await transferManager.uploadManyFiles('/local/directory');
121 * // Your bucket will now contain all files contained in '/local/directory' maintaining the subdirectory structure.
122 * ```
123 *
124 */
125 uploadManyFiles(filePathsOrDirectory: string[] | string, options?: UploadManyFilesOptions): Promise<UploadResponse[]>;
126 /**
127 * @typedef {object} DownloadManyFilesOptions
128 * @property {number} [concurrencyLimit] The number of concurrently executing promises
129 * to use when downloading the files.
130 * @property {string} [prefix] A prefix to append to all of the downloaded files.
131 * @property {string} [stripPrefix] A prefix to remove from all of the downloaded files.
132 * @property {object} [passthroughOptions] {@link DownloadOptions} Options to be passed through
133 * to each individual download operation.
134 * @property {boolean} [skipIfExists] Do not download the file if it already exists in
135 * the destination.
136 *
137 */
138 /**
139 * Download multiple files in parallel to the local filesystem. This is a convenience method
140 * that utilizes {@link File#download} to perform the download.
141 *
142 * @param {array | string} [filesOrFolder] An array of file name strings or file objects to be downloaded. If
143 * a string is provided this will be treated as a GCS prefix and all files with that prefix will be downloaded.
144 * @param {DownloadManyFilesOptions} [options] Configuration options. Setting options.prefix or options.stripPrefix
145 * or options.passthroughOptions.destination will cause the downloaded files to be written to the file system
146 * instead of being returned as a buffer.
147 * @returns {Promise<DownloadResponse[]>}
148 *
149 * @example
150 * ```
151 * const {Storage} = require('@google-cloud/storage');
152 * const storage = new Storage();
153 * const bucket = storage.bucket('my-bucket');
154 * const transferManager = new TransferManager(bucket);
155 *
156 * //-
157 * // Download multiple files in parallel.
158 * //-
159 * const response = await transferManager.downloadManyFiles(['file1.txt', 'file2.txt']);
160 * // The following files have been downloaded:
161 * // - "file1.txt" (with the contents from my-bucket.file1.txt)
162 * // - "file2.txt" (with the contents from my-bucket.file2.txt)
163 * const response = await transferManager.downloadManyFiles([bucket.File('file1.txt'), bucket.File('file2.txt')]);
164 * // The following files have been downloaded:
165 * // - "file1.txt" (with the contents from my-bucket.file1.txt)
166 * // - "file2.txt" (with the contents from my-bucket.file2.txt)
167 * const response = await transferManager.downloadManyFiles('test-folder');
168 * // All files with GCS prefix of 'test-folder' have been downloaded.
169 * ```
170 *
171 */
172 downloadManyFiles(filesOrFolder: File[] | string[] | string, options?: DownloadManyFilesOptions): Promise<void | DownloadResponse[]>;
173 /**
174 * @typedef {object} DownloadFileInChunksOptions
175 * @property {number} [concurrencyLimit] The number of concurrently executing promises
176 * to use when downloading the file.
177 * @property {number} [chunkSizeBytes] The size in bytes of each chunk to be downloaded.
178 * @property {string | boolean} [validation] Whether or not to perform a CRC32C validation check when download is complete.
179 * @property {boolean} [noReturnData] Whether or not to return the downloaded data. A `true` value here would be useful for files with a size that will not fit into memory.
180 *
181 */
182 /**
183 * Download a large file in chunks utilizing parallel download operations. This is a convenience method
184 * that utilizes {@link File#download} to perform the download.
185 *
186 * @param {File | string} fileOrName {@link File} to download.
187 * @param {DownloadFileInChunksOptions} [options] Configuration options.
188 * @returns {Promise<void | DownloadResponse>}
189 *
190 * @example
191 * ```
192 * const {Storage} = require('@google-cloud/storage');
193 * const storage = new Storage();
194 * const bucket = storage.bucket('my-bucket');
195 * const transferManager = new TransferManager(bucket);
196 *
197 * //-
198 * // Download a large file in chunks utilizing parallel operations.
199 * //-
200 * const response = await transferManager.downloadFileInChunks(bucket.file('large-file.txt');
201 * // Your local directory now contains:
202 * // - "large-file.txt" (with the contents from my-bucket.large-file.txt)
203 * ```
204 *
205 */
206 downloadFileInChunks(fileOrName: File | string, options?: DownloadFileInChunksOptions): Promise<void | DownloadResponse>;
207 /**
208 * @typedef {object} UploadFileInChunksOptions
209 * @property {number} [concurrencyLimit] The number of concurrently executing promises
210 * to use when uploading the file.
211 * @property {number} [chunkSizeBytes] The size in bytes of each chunk to be uploaded.
212 * @property {string} [uploadName] Name of the file when saving to GCS. If ommitted the name is taken from the file path.
213 * @property {number} [maxQueueSize] The number of chunks to be uploaded to hold in memory concurrently. If not specified
214 * defaults to the specified concurrency limit.
215 * @property {string} [uploadId] If specified attempts to resume a previous upload.
216 * @property {Map} [partsMap] If specified alongside uploadId, attempts to resume a previous upload from the last chunk
217 * specified in partsMap
218 * @property {object} [headers] headers to be sent when initiating the multipart upload.
219 * See {@link https://cloud.google.com/storage/docs/xml-api/post-object-multipart#request_headers| Request Headers: Initiate a Multipart Upload}
220 * @property {boolean} [autoAbortFailure] boolean to indicate if an in progress upload session will be automatically aborted upon failure. If not set,
221 * failures will be automatically aborted.
222 *
223 */
224 /**
225 * Upload a large file in chunks utilizing parallel upload opertions. If the upload fails, an uploadId and
226 * map containing all the successfully uploaded parts will be returned to the caller. These arguments can be used to
227 * resume the upload.
228 *
229 * @param {string} [filePath] The path of the file to be uploaded
230 * @param {UploadFileInChunksOptions} [options] Configuration options.
231 * @param {MultiPartHelperGenerator} [generator] A function that will return a type that implements the MPU interface. Most users will not need to use this.
232 * @returns {Promise<void>} If successful a promise resolving to void, otherwise a error containing the message, uploadid, and parts map.
233 *
234 * @example
235 * ```
236 * const {Storage} = require('@google-cloud/storage');
237 * const storage = new Storage();
238 * const bucket = storage.bucket('my-bucket');
239 * const transferManager = new TransferManager(bucket);
240 *
241 * //-
242 * // Upload a large file in chunks utilizing parallel operations.
243 * //-
244 * const response = await transferManager.uploadFileInChunks('large-file.txt');
245 * // Your bucket now contains:
246 * // - "large-file.txt"
247 * ```
248 *
249 *
250 */
251 uploadFileInChunks(filePath: string, options?: UploadFileInChunksOptions, generator?: MultiPartHelperGenerator): Promise<GaxiosResponse | undefined>;
252 private getPathsFromDirectory;
253}