Class TransferManager (7.16.0)

Create a TransferManager object to perform parallel transfer operations on a Cloud Storage bucket.

Package

@google-cloud/storage

Constructors

(constructor)(bucket)

constructor(bucket: Bucket);

Constructs a new instance of the TransferManager class

Parameter
Name Description
bucket Bucket

Properties

bucket

bucket: Bucket;

Methods

downloadFileInChunks(fileOrName, options)

downloadFileInChunks(fileOrName: File | string, options?: DownloadFileInChunksOptions): Promise<void | DownloadResponse>;

Download a large file in chunks utilizing parallel download operations. This is a convenience method that utilizes to perform the download.

Parameters
Name Description
fileOrName File | string

to download.

options DownloadFileInChunksOptions

Configuration options.

Returns
Type Description
Promise<void | DownloadResponse>

{Promise<void | DownloadResponse>}

Example

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const transferManager = new TransferManager(bucket);

//-
// Download a large file in chunks utilizing parallel operations.
//-
const response = await transferManager.downloadFileInChunks(bucket.file('large-file.txt');
// Your local directory now contains:
// - "large-file.txt" (with the contents from my-bucket.large-file.txt)

downloadManyFiles(filesOrFolder, options)

downloadManyFiles(filesOrFolder: File[] | string[] | string, options?: DownloadManyFilesOptions): Promise<void | DownloadResponse[]>;

Download multiple files in parallel to the local filesystem. This is a convenience method that utilizes to perform the download.

Parameters
Name Description
filesOrFolder File[] | string[] | string

An array of file name strings or file objects to be downloaded. If a string is provided this will be treated as a GCS prefix and all files with that prefix will be downloaded.

options DownloadManyFilesOptions

Configuration options. Setting options.prefix or options.stripPrefix or options.passthroughOptions.destination will cause the downloaded files to be written to the file system instead of being returned as a buffer.

Returns
Type Description
Promise<void | DownloadResponse[]>

{Promise<DownloadResponse[]>}

Example

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const transferManager = new TransferManager(bucket);

//-
// Download multiple files in parallel.
//-
const response = await transferManager.downloadManyFiles(['file1.txt', 'file2.txt']);
// The following files have been downloaded:
// - "file1.txt" (with the contents from my-bucket.file1.txt)
// - "file2.txt" (with the contents from my-bucket.file2.txt)
const response = await transferManager.downloadManyFiles([bucket.File('file1.txt'), bucket.File('file2.txt')]);
// The following files have been downloaded:
// - "file1.txt" (with the contents from my-bucket.file1.txt)
// - "file2.txt" (with the contents from my-bucket.file2.txt)
const response = await transferManager.downloadManyFiles('test-folder');
// All files with GCS prefix of 'test-folder' have been downloaded.

uploadFileInChunks(filePath, options, generator)

uploadFileInChunks(filePath: string, options?: UploadFileInChunksOptions, generator?: MultiPartHelperGenerator): Promise<GaxiosResponse | undefined>;

Upload a large file in chunks utilizing parallel upload opertions. If the upload fails, an uploadId and map containing all the successfully uploaded parts will be returned to the caller. These arguments can be used to resume the upload.

Parameters
Name Description
filePath string

The path of the file to be uploaded

options UploadFileInChunksOptions

Configuration options.

generator MultiPartHelperGenerator

A function that will return a type that implements the MPU interface. Most users will not need to use this.

Returns
Type Description
Promise<GaxiosResponse | undefined>

{Promise

Example

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const transferManager = new TransferManager(bucket);

//-
// Upload a large file in chunks utilizing parallel operations.
//-
const response = await transferManager.uploadFileInChunks('large-file.txt');
// Your bucket now contains:
// - "large-file.txt"

uploadManyFiles(filePathsOrDirectory, options)

uploadManyFiles(filePathsOrDirectory: string[] | string, options?: UploadManyFilesOptions): Promise<UploadResponse[]>;

Upload multiple files in parallel to the bucket. This is a convenience method that utilizes to perform the upload.

Parameters
Name Description
filePathsOrDirectory string[] | string

An array of fully qualified paths to the files or a directory name. If a directory name is provided, the directory will be recursively walked and all files will be added to the upload list. to be uploaded to the bucket

options UploadManyFilesOptions

Configuration options.

Returns
Type Description
Promise<UploadResponse[]>

{Promise<UploadResponse[]>}

Example

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket('my-bucket');
const transferManager = new TransferManager(bucket);

//-
// Upload multiple files in parallel.
//-
const response = await transferManager.uploadManyFiles(['/local/path/file1.txt, 'local/path/file2.txt']);
// Your bucket now contains:
// - "local/path/file1.txt" (with the contents of '/local/path/file1.txt')
// - "local/path/file2.txt" (with the contents of '/local/path/file2.txt')
const response = await transferManager.uploadManyFiles('/local/directory');
// Your bucket will now contain all files contained in '/local/directory' maintaining the subdirectory structure.