1 | import { Amplify } from '@aws-amplify/core';
|
2 | import { StorageAction } from '@aws-amplify/core/internals/utils';
|
3 | import { resolveS3ConfigAndInput } from '../utils/resolveS3ConfigAndInput.mjs';
|
4 | import '@smithy/md5-js';
|
5 | import '@aws-amplify/core/internals/aws-client-utils';
|
6 | import '../utils/client/runtime/s3TransferHandler/fetch.mjs';
|
7 | import 'fast-xml-parser';
|
8 | import '../utils/client/runtime/s3TransferHandler/xhr.mjs';
|
9 | import 'buffer';
|
10 | import { createDownloadTask } from '../utils/transferTask.mjs';
|
11 | import { validateStorageOperationInput } from '../utils/validateStorageOperationInput.mjs';
|
12 | import '../../../errors/types/validation.mjs';
|
13 | import { STORAGE_INPUT_KEY } from '../utils/constants.mjs';
|
14 | import '../utils/client/base.mjs';
|
15 | import { getObject } from '../utils/client/getObject.mjs';
|
16 | import '../utils/client/listObjectsV2.mjs';
|
17 | import '../utils/client/putObject.mjs';
|
18 | import '../utils/client/createMultipartUpload.mjs';
|
19 | import '../utils/client/uploadPart.mjs';
|
20 | import '../utils/client/completeMultipartUpload.mjs';
|
21 | import '../utils/client/listParts.mjs';
|
22 | import '../utils/client/abortMultipartUpload.mjs';
|
23 | import '../utils/client/copyObject.mjs';
|
24 | import '../utils/client/headObject.mjs';
|
25 | import '../utils/client/deleteObject.mjs';
|
26 | import { getStorageUserAgentValue } from '../utils/userAgent.mjs';
|
27 | import { logger } from '../../../utils/logger.mjs';
|
28 |
|
29 |
|
30 |
|
31 | function downloadData(input) {
|
32 | const abortController = new AbortController();
|
33 | const downloadTask = createDownloadTask({
|
34 | job: downloadDataJob(input, abortController.signal),
|
35 | onCancel: (message) => {
|
36 | abortController.abort(message);
|
37 | },
|
38 | });
|
39 | return downloadTask;
|
40 | }
|
41 | const downloadDataJob = (downloadDataInput, abortSignal) => async () => {
|
42 | const { options: downloadDataOptions } = downloadDataInput;
|
43 | const { bucket, keyPrefix, s3Config, identityId } = await resolveS3ConfigAndInput(Amplify, downloadDataOptions);
|
44 | const { inputType, objectKey } = validateStorageOperationInput(downloadDataInput, identityId);
|
45 | const finalKey = inputType === STORAGE_INPUT_KEY ? keyPrefix + objectKey : objectKey;
|
46 | logger.debug(`download ${objectKey} from ${finalKey}.`);
|
47 | const { Body: body, LastModified: lastModified, ContentLength: size, ETag: eTag, Metadata: metadata, VersionId: versionId, ContentType: contentType, } = await getObject({
|
48 | ...s3Config,
|
49 | abortSignal,
|
50 | onDownloadProgress: downloadDataOptions?.onProgress,
|
51 | userAgentValue: getStorageUserAgentValue(StorageAction.DownloadData),
|
52 | }, {
|
53 | Bucket: bucket,
|
54 | Key: finalKey,
|
55 | ...(downloadDataOptions?.bytesRange && {
|
56 | Range: `bytes=${downloadDataOptions.bytesRange.start}-${downloadDataOptions.bytesRange.end}`,
|
57 | }),
|
58 | });
|
59 | const result = {
|
60 | body,
|
61 | lastModified,
|
62 | size,
|
63 | contentType,
|
64 | eTag,
|
65 | metadata,
|
66 | versionId,
|
67 | };
|
68 | return inputType === STORAGE_INPUT_KEY
|
69 | ? { key: objectKey, ...result }
|
70 | : { path: objectKey, ...result };
|
71 | };
|
72 |
|
73 | export { downloadData };
|
74 |
|