Skip to content

Commit

Permalink
add a quick solution to enable getUrl put
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Aug 30, 2024
1 parent 2bbdb67 commit 6fdc67e
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { AmplifyUrl } from '../../../../../utils/amplifyUrl';
import { HttpRequest } from '../../../../types';

import { PresignUrlOptions, Presignable } from './types';
import {
Expand Down Expand Up @@ -45,13 +46,16 @@ export const presignUrl = (
}).forEach(([key, value]) => {
presignedUrl.searchParams.append(key, value);
});
const requestToSign = {
body,
const requestToSign: HttpRequest = {
headers: { [HOST_HEADER]: url.host },
method,
url: presignedUrl,
};

if (body) {
requestToSign.body = body;
}

// calculate and add the signature to the url
const signature = getSignature(requestToSign, signingValues);
presignedUrl.searchParams.append(SIGNATURE_QUERY_PARAM, signature);
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@
"s3"
],
"dependencies": {
"@aws-sdk/s3-request-presigner": "3.637.0",
"@aws-sdk/types": "3.398.0",
"@aws-sdk/util-format-url": "3.609.0",
"@smithy/hash-node": "3.0.3",
"@smithy/md5-js": "2.0.7",
"@smithy/url-parser": "3.0.3",
"buffer": "4.9.2",
"fast-xml-parser": "^4.4.1",
"tslib": "^2.5.0"
Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/providers/s3/apis/internal/getUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const getUrl = async (
...(getUrlOptions?.contentType && {
ResponseContentType: getUrlOptions.contentType,
}),
method: input.options?.method,
},
),
expiresAt: new Date(Date.now() + urlExpirationInSec * 1000),
Expand Down
22 changes: 20 additions & 2 deletions packages/storage/src/providers/s3/utils/client/getObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

import {
EMPTY_SHA256_HASH,
Endpoint,
HttpRequest,
HttpResponse,
Expand All @@ -13,6 +12,10 @@ import {
} from '@aws-amplify/core/internals/aws-client-utils';
import { AmplifyUrl } from '@aws-amplify/core/internals/utils';
import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/composers';
import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner';
import { Hash } from '@smithy/hash-node';
import { formatUrl } from '@aws-sdk/util-format-url';
import { parseUrl } from '@smithy/url-parser';

import { S3EndpointResolverOptions, defaultConfig } from './base';
import type {
Expand Down Expand Up @@ -150,10 +153,25 @@ export const getPresignedGetObjectUrl = async (
const endpoint = defaultConfig.endpointResolver(config, input);
const { url, headers, method } = await getObjectSerializer(input, endpoint);

if (input.method === 'PUT') {
const presigner = new S3RequestPresigner({
credentials: config.credentials,
region: config.region,
sha256: Hash.bind(null, 'sha256'),
});
const signedUrlObject = await presigner.presign({
...parseUrl(url.toString()),
headers,
method,
});

return new AmplifyUrl(formatUrl(signedUrlObject));
}

// 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(CONTENT_SHA256_HEADER, EMPTY_SHA256_HASH);
url.searchParams.append(CONTENT_SHA256_HEADER, 'UNSIGNED-PAYLOAD');
if (config.userAgentValue) {
url.searchParams.append(
config.userAgentHeader ?? USER_AGENT_HEADER,
Expand Down
116 changes: 110 additions & 6 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6fdc67e

Please sign in to comment.