@aws-amplify/storage
Version:
Storage category of aws-amplify
110 lines (108 loc) • 5.56 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPresignedGetObjectUrl = exports.getObject = void 0;
const aws_client_utils_1 = require("@aws-amplify/core/internals/aws-client-utils");
const utils_1 = require("@aws-amplify/core/internals/utils");
const composers_1 = require("@aws-amplify/core/internals/aws-client-utils/composers");
const base_1 = require("./base");
const utils_2 = require("./utils");
const USER_AGENT_HEADER = 'x-amz-user-agent';
const getObjectSerializer = async (input, endpoint) => {
const url = new utils_1.AmplifyUrl(endpoint.url.toString());
(0, utils_2.validateS3RequiredParameter)(!!input.Key, 'Key');
url.pathname = (0, utils_2.serializePathnameObjectKey)(url, input.Key);
return {
method: 'GET',
headers: {
...(input.Range && { Range: input.Range }),
},
url,
};
};
const getObjectDeserializer = async (response) => {
if (response.statusCode >= 300) {
const error = (await (0, utils_2.parseXmlError)(response));
throw (0, utils_2.buildStorageServiceError)(error, response.statusCode);
}
else {
return {
...(0, utils_2.map)(response.headers, {
DeleteMarker: ['x-amz-delete-marker', utils_2.deserializeBoolean],
AcceptRanges: 'accept-ranges',
Expiration: 'x-amz-expiration',
Restore: 'x-amz-restore',
LastModified: ['last-modified', utils_2.deserializeTimestamp],
ContentLength: ['content-length', utils_2.deserializeNumber],
ETag: 'etag',
ChecksumCRC32: 'x-amz-checksum-crc32',
ChecksumCRC32C: 'x-amz-checksum-crc32c',
ChecksumSHA1: 'x-amz-checksum-sha1',
ChecksumSHA256: 'x-amz-checksum-sha256',
MissingMeta: ['x-amz-missing-meta', utils_2.deserializeNumber],
VersionId: 'x-amz-version-id',
CacheControl: 'cache-control',
ContentDisposition: 'content-disposition',
ContentEncoding: 'content-encoding',
ContentLanguage: 'content-language',
ContentRange: 'content-range',
ContentType: 'content-type',
Expires: ['expires', utils_2.deserializeTimestamp],
WebsiteRedirectLocation: 'x-amz-website-redirect-location',
ServerSideEncryption: 'x-amz-server-side-encryption',
SSECustomerAlgorithm: 'x-amz-server-side-encryption-customer-algorithm',
SSECustomerKeyMD5: 'x-amz-server-side-encryption-customer-key-md5',
SSEKMSKeyId: 'x-amz-server-side-encryption-aws-kms-key-id',
BucketKeyEnabled: [
'x-amz-server-side-encryption-bucket-key-enabled',
utils_2.deserializeBoolean,
],
StorageClass: 'x-amz-storage-class',
RequestCharged: 'x-amz-request-charged',
ReplicationStatus: 'x-amz-replication-status',
PartsCount: ['x-amz-mp-parts-count', utils_2.deserializeNumber],
TagCount: ['x-amz-tagging-count', utils_2.deserializeNumber],
ObjectLockMode: 'x-amz-object-lock-mode',
ObjectLockRetainUntilDate: [
'x-amz-object-lock-retain-until-date',
utils_2.deserializeTimestamp,
],
ObjectLockLegalHoldStatus: 'x-amz-object-lock-legal-hold',
}),
Metadata: (0, utils_2.deserializeMetadata)(response.headers),
$metadata: (0, aws_client_utils_1.parseMetadata)(response),
// @ts-expect-error The body is a CompatibleHttpResponse type because the lower-level handler is XHR instead of
// fetch, which represents payload in Blob instread of ReadableStream.
Body: response.body,
};
}
};
exports.getObject = (0, composers_1.composeServiceApi)(utils_2.s3TransferHandler, getObjectSerializer, getObjectDeserializer, { ...base_1.defaultConfig, responseType: 'blob' });
/**
* Get a presigned URL for the `getObject` API.
*
* @internal
*/
const getPresignedGetObjectUrl = async (config, input) => {
const endpoint = base_1.defaultConfig.endpointResolver(config, input);
const { url, headers, method } = await getObjectSerializer(input, endpoint);
// TODO: set content sha256 query parameter with value of UNSIGNED-PAYLOAD instead of empty hash.
// It requires changes in presignUrl. Without this change, the generated url still works,
// but not the same as other tools like AWS SDK and CLI.
url.searchParams.append(utils_2.CONTENT_SHA256_HEADER, aws_client_utils_1.EMPTY_SHA256_HASH);
if (config.userAgentValue) {
url.searchParams.append(config.userAgentHeader ?? USER_AGENT_HEADER, config.userAgentValue);
}
for (const [headerName, value] of Object.entries(headers).sort(([key1], [key2]) => key1.localeCompare(key2))) {
url.searchParams.append(headerName, value);
}
return (0, aws_client_utils_1.presignUrl)({ method, url, body: undefined }, {
signingService: base_1.defaultConfig.service,
signingRegion: config.region,
...base_1.defaultConfig,
...config,
});
};
exports.getPresignedGetObjectUrl = getPresignedGetObjectUrl;
//# sourceMappingURL=getObject.js.map
;