Skip to content

Commit dc1e488

Browse files
authored
feat: add function to allow user to set destination in transfer manager (#2497)
* feat: add function to allow user to set destination in transfer manager * lint
1 parent ded96e3 commit dc1e488

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/transfer-manager.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ const GCCL_GCS_CMD_FEATURE = {
9494

9595
export interface UploadManyFilesOptions {
9696
concurrencyLimit?: number;
97+
customDestinationBuilder?(
98+
path: string,
99+
options: UploadManyFilesOptions
100+
): string;
97101
skipIfExists?: boolean;
98102
prefix?: string;
99103
passthroughOptions?: Omit<UploadOptions, 'destination'>;
@@ -411,6 +415,8 @@ export class TransferManager {
411415
* @typedef {object} UploadManyFilesOptions
412416
* @property {number} [concurrencyLimit] The number of concurrently executing promises
413417
* to use when uploading the files.
418+
* @property {Function} [customDestinationBuilder] A fuction that will take the current path of a local file
419+
* and return a string representing a custom path to be used to upload the file to GCS.
414420
* @property {boolean} [skipIfExists] Do not upload the file if it already exists in
415421
* the bucket. This will set the precondition ifGenerationMatch = 0.
416422
* @property {string} [prefix] A prefix to append to all of the uploaded files.
@@ -490,9 +496,9 @@ export class TransferManager {
490496
[GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.UPLOAD_MANY,
491497
};
492498

493-
passThroughOptionsCopy.destination = filePath
494-
.split(path.sep)
495-
.join(path.posix.sep);
499+
passThroughOptionsCopy.destination = options.customDestinationBuilder
500+
? options.customDestinationBuilder(filePath, options)
501+
: filePath.split(path.sep).join(path.posix.sep);
496502
if (options.prefix) {
497503
passThroughOptionsCopy.destination = path.posix.join(
498504
...options.prefix.split(path.sep),

test/transfer-manager.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
MultiPartUploadError,
2727
MultiPartUploadHelper,
2828
UploadOptions,
29+
UploadManyFilesOptions,
2930
TransferManager,
3031
Storage,
3132
DownloadResponse,
@@ -173,6 +174,24 @@ describe('Transfer Manager', () => {
173174

174175
await transferManager.uploadManyFiles([filePath]);
175176
});
177+
178+
it('allows the user to apply a custom destination transformation when supplied a custom function', async () => {
179+
const paths = ['a', 'b', 'foo/bar', 'bar.txt'];
180+
const expected = ['foo/a', 'b/bar', 'foo/foo/bar', 'bar.txt/bar'];
181+
sandbox.stub(bucket, 'upload').callsFake((path, options) => {
182+
const uploadOpts = options as UploadOptions;
183+
assert(expected.includes(uploadOpts.destination as string));
184+
});
185+
186+
let callCount = 0;
187+
const transformationFunc = (path: string) => {
188+
assert.strictEqual(path, paths[callCount]);
189+
return expected[callCount++];
190+
};
191+
await transferManager.uploadManyFiles(paths, {
192+
customDestinationBuilder: transformationFunc,
193+
});
194+
});
176195
});
177196

178197
describe('downloadManyFiles', () => {

0 commit comments

Comments
 (0)