UNPKG

3.58 kBJavaScriptView Raw
1import { Amplify } from '@aws-amplify/core';
2import { StorageAction } from '@aws-amplify/core/internals/utils';
3import { resolveS3ConfigAndInput } from '../utils/resolveS3ConfigAndInput.mjs';
4import '@smithy/md5-js';
5import '@aws-amplify/core/internals/aws-client-utils';
6import '../utils/client/runtime/s3TransferHandler/fetch.mjs';
7import 'fast-xml-parser';
8import '../utils/client/runtime/s3TransferHandler/xhr.mjs';
9import 'buffer';
10import { createDownloadTask } from '../utils/transferTask.mjs';
11import '../utils/client/base.mjs';
12import { getObject } from '../utils/client/getObject.mjs';
13import '../utils/client/listObjectsV2.mjs';
14import '../utils/client/putObject.mjs';
15import '../utils/client/createMultipartUpload.mjs';
16import '../utils/client/uploadPart.mjs';
17import '../utils/client/completeMultipartUpload.mjs';
18import '../utils/client/listParts.mjs';
19import '../utils/client/abortMultipartUpload.mjs';
20import '../utils/client/copyObject.mjs';
21import '../utils/client/headObject.mjs';
22import '../utils/client/deleteObject.mjs';
23import { getStorageUserAgentValue } from '../utils/userAgent.mjs';
24import '../../../errors/types/validation.mjs';
25import { logger } from '../../../utils/logger.mjs';
26
27// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
28// SPDX-License-Identifier: Apache-2.0
29/**
30 * Download S3 object data to memory
31 *
32 * @param input - The DownloadDataInput object.
33 * @returns A cancelable task exposing result promise from `result` property.
34 * @throws service: {@link S3Exception} - thrown when checking for existence of the object
35 * @throws validation: {@link StorageValidationErrorCode } - Validation errors
36 *
37 * @example
38 * ```ts
39 * // Download a file from s3 bucket
40 * const { body, eTag } = await downloadData({ key, data: file, options: {
41 * onProgress, // Optional progress callback.
42 * } }).result;
43 * ```
44 * @example
45 * ```ts
46 * // Cancel a task
47 * const downloadTask = downloadData({ key, data: file });
48 * //...
49 * downloadTask.cancel();
50 * try {
51 * await downloadTask.result;
52 * } catch (error) {
53 * if(isCancelError(error)) {
54 * // Handle error thrown by task cancelation.
55 * }
56 * }
57 *```
58 */
59const downloadData = (input) => {
60 const abortController = new AbortController();
61 const downloadTask = createDownloadTask({
62 job: downloadDataJob(input, abortController.signal),
63 onCancel: (message) => {
64 abortController.abort(message);
65 },
66 });
67 return downloadTask;
68};
69const downloadDataJob = ({ options: downloadDataOptions, key }, abortSignal) => async () => {
70 const { bucket, keyPrefix, s3Config } = await resolveS3ConfigAndInput(Amplify, downloadDataOptions);
71 const finalKey = keyPrefix + key;
72 logger.debug(`download ${key} from ${finalKey}.`);
73 const { Body: body, LastModified: lastModified, ContentLength: size, ETag: eTag, Metadata: metadata, VersionId: versionId, ContentType: contentType, } = await getObject({
74 ...s3Config,
75 abortSignal,
76 onDownloadProgress: downloadDataOptions?.onProgress,
77 userAgentValue: getStorageUserAgentValue(StorageAction.DownloadData),
78 }, {
79 Bucket: bucket,
80 Key: finalKey,
81 ...(downloadDataOptions?.bytesRange && {
82 Range: `bytes=${downloadDataOptions.bytesRange.start}-${downloadDataOptions.bytesRange.end}`,
83 }),
84 });
85 return {
86 key,
87 body,
88 lastModified,
89 size,
90 contentType,
91 eTag,
92 metadata,
93 versionId,
94 };
95};
96
97export { downloadData };
98//# sourceMappingURL=downloadData.mjs.map