Skip to content

Commit eec906e

Browse files
committed
feat(storage): provide getUploadUrl and getDownloadUrl methods on files
1 parent 85f2715 commit eec906e

File tree

2 files changed

+105
-21
lines changed

2 files changed

+105
-21
lines changed

src/api/storage/v0/storage.test.ts

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ describe('Storage Client Tests', () => {
228228
jest.resetAllMocks();
229229
});
230230

231-
test('Then StorageClient.delete should reject', async () => {
231+
test('Then file.signUrl should reject', async () => {
232232
const client = new Storage();
233233
await expect(client.bucket('test').file('test').signUrl(FileMode.Read)).rejects.toEqual(
234234
new UnimplementedError("UNIMPLEMENTED")
235235
);
236236
});
237237

238-
test('The Grpc client for Storage.delete should have been called exactly once', () => {
238+
test('The Grpc client for file.signUrl should have been called exactly once', () => {
239239
expect(signUrlMock).toBeCalledTimes(1);
240240
});
241241
});
@@ -244,38 +244,101 @@ describe('Storage Client Tests', () => {
244244
const MOCK_REPLY = new StoragePreSignUrlResponse();
245245
MOCK_REPLY.setUrl("testingUrl");
246246

247-
let preSignUrlMock;
247+
let preSignUrlMock: jest.SpyInstance;
248248

249249
beforeAll(() => {
250250
preSignUrlMock = jest
251251
.spyOn(GrpcStorageClient.prototype, 'preSignUrl')
252252
.mockImplementation((_, callback: any) => {
253253
callback(null, MOCK_REPLY);
254-
255254
return null as any;
256255
});
257256
});
258257

259258
afterAll(() => {
260259
jest.resetAllMocks();
261260
});
262-
263-
test('Then StorageClient.delete should delete the bytes from the bucket', async () => {
264-
const client = new Storage().bucket('test_bucket').file('test/item');
265-
await expect(client.signUrl(FileMode.Read)).resolves.toEqual("testingUrl");
266-
});
267-
268-
test('The Grpc client for Storage.delete should have been called exactly once', () => {
269-
expect(preSignUrlMock).toBeCalledTimes(1);
270-
});
271-
272-
test('The Grpc client for Storage.delete should have been called with provided options', () => {
273-
const MOCK_REQUEST = new StoragePreSignUrlRequest();
274-
MOCK_REQUEST.setBucketName("test_bucket");
275-
MOCK_REQUEST.setKey("test/item");
276-
MOCK_REQUEST.setOperation(FileMode.Read);
277-
MOCK_REQUEST.setExpiry(600);
278-
expect(preSignUrlMock).toBeCalledWith(MOCK_REQUEST, expect.anything());
261+
262+
263+
describe('When calling file.signUrl', () => {
264+
let signUrl;
265+
266+
beforeAll(async () => {
267+
preSignUrlMock.mockClear();
268+
const client = new Storage().bucket('test_bucket').file('test/item');
269+
signUrl = await client.signUrl(FileMode.Read)
270+
})
271+
272+
test('Then file.signUrl should delete the bytes from the bucket', () => {
273+
expect(signUrl).toEqual("testingUrl");
274+
});
275+
276+
test('The Grpc client for file.signUrl should have been called exactly once', () => {
277+
expect(preSignUrlMock).toBeCalledTimes(1);
278+
});
279+
280+
test('The Grpc client for file.signUrl should have been called with provided options', () => {
281+
const MOCK_REQUEST = new StoragePreSignUrlRequest();
282+
MOCK_REQUEST.setBucketName("test_bucket");
283+
MOCK_REQUEST.setKey("test/item");
284+
MOCK_REQUEST.setOperation(FileMode.Read);
285+
MOCK_REQUEST.setExpiry(600);
286+
expect(preSignUrlMock).toBeCalledWith(MOCK_REQUEST, expect.anything());
287+
});
288+
});
289+
290+
describe('When calling file.getUploadUrl', () => {
291+
let signUrl;
292+
293+
beforeAll(async () => {
294+
preSignUrlMock.mockClear();
295+
const client = new Storage().bucket('test_bucket').file('test/item');
296+
signUrl = await client.getUploadUrl()
297+
})
298+
299+
test('Then file.signUrl should delete the bytes from the bucket', () => {
300+
expect(signUrl).toEqual("testingUrl");
301+
});
302+
303+
test('The Grpc client for file.signUrl should have been called exactly once', () => {
304+
expect(preSignUrlMock).toBeCalledTimes(1);
305+
});
306+
307+
test('The Grpc client for file.signUrl should have been called with provided options', () => {
308+
const MOCK_REQUEST = new StoragePreSignUrlRequest();
309+
MOCK_REQUEST.setBucketName("test_bucket");
310+
MOCK_REQUEST.setKey("test/item");
311+
MOCK_REQUEST.setOperation(FileMode.Write);
312+
MOCK_REQUEST.setExpiry(600);
313+
expect(preSignUrlMock).toBeCalledWith(MOCK_REQUEST, expect.anything());
314+
});
315+
});
316+
317+
describe('When calling file.getUploadUrl', () => {
318+
let signUrl;
319+
320+
beforeAll(async () => {
321+
preSignUrlMock.mockClear();
322+
const client = new Storage().bucket('test_bucket').file('test/item');
323+
signUrl = await client.getDownloadUrl()
324+
})
325+
326+
test('Then file.signUrl should delete the bytes from the bucket', () => {
327+
expect(signUrl).toEqual("testingUrl");
328+
});
329+
330+
test('The Grpc client for file.signUrl should have been called exactly once', () => {
331+
expect(preSignUrlMock).toBeCalledTimes(1);
332+
});
333+
334+
test('The Grpc client for file.signUrl should have been called with provided options', () => {
335+
const MOCK_REQUEST = new StoragePreSignUrlRequest();
336+
MOCK_REQUEST.setBucketName("test_bucket");
337+
MOCK_REQUEST.setKey("test/item");
338+
MOCK_REQUEST.setOperation(FileMode.Read);
339+
MOCK_REQUEST.setExpiry(600);
340+
expect(preSignUrlMock).toBeCalledWith(MOCK_REQUEST, expect.anything());
341+
});
279342
});
280343
});
281344
});

src/api/storage/v0/storage.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,29 @@ export class File {
104104
this.name = name;
105105
}
106106

107+
/**
108+
* Get a pre-signed download URL for the file
109+
* @param opts the option passed to the signUrl function.
110+
* @returns a download URL string
111+
*/
112+
public getDownloadUrl(opts?: SignUrlOptions): Promise<string> {
113+
return this.signUrl(FileMode.Read, opts)
114+
}
115+
116+
/**
117+
* Get a pre-signed upload URL for the file.
118+
* @param opts the option passed to the signUrl function.
119+
* @returns a upload URL string
120+
*/
121+
public getUploadUrl(opts?: SignUrlOptions): Promise<string> {
122+
return this.signUrl(FileMode.Write, opts)
123+
}
124+
107125
/**
108126
* Create a presigned url for reading or writing for the given file reference
127+
* @param mode the mode the url will access the file with. E.g. reading or writing.
128+
* @param opts.expiry how long the URL should be valid for in seconds.
129+
* @deprecated for simplicity we suggest using getUploadUrl or getDownloadUrl
109130
*/
110131
public async signUrl(
111132
mode: FileMode,

0 commit comments

Comments
 (0)