Yap Yee Qiang
02/22/2022, 10:51 AMRoss Coundon
02/22/2022, 11:02 AMYap Yee Qiang
02/22/2022, 11:22 AMRoss Coundon
02/22/2022, 11:26 AMimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';
const config: S3ClientConfig = {
region: process.env.REGION,
};
function getSignedUrlForFile(bucket: string, filename: string, operation: 'get' | 'put'): Promise<string> {
const getObjectParams: GetObjectCommandInput = {
Bucket: bucket,
Key: filename,
};
const putObjectParams: PutObjectCommandInput = {
Bucket: bucket,
Key: filename,
};
const s3Client = new S3Client(config);
const command = operation === 'get' ? new GetObjectCommand(getObjectParams) : new PutObjectCommand(putObjectParams);
return getSignedUrl(s3Client, command, { expiresIn: 3600 });
}
Ross Coundon
02/22/2022, 11:27 AMYap Yee Qiang
02/22/2022, 11:48 AM@aws-sdk
as well?Ross Coundon
02/22/2022, 12:13 PMRoss Coundon
02/22/2022, 12:13 PMimport {
S3Client,
_Object,
S3ClientConfig,
PutObjectCommand,
GetObjectCommand,
GetObjectCommandInput,
PutObjectCommandOutput,
PutObjectCommandInput,
DeleteObjectCommandInput,
DeleteObjectCommand,
GetObjectOutput,
ListObjectsV2CommandInput,
ListObjectsV2Command,
} from '@aws-sdk/client-s3';
Yap Yee Qiang
02/22/2022, 12:23 PMRoss Coundon
02/22/2022, 12:34 PMYap Yee Qiang
02/22/2022, 12:42 PMRoss Coundon
02/22/2022, 12:44 PMFrank
<input
type="file"
onChange={async (e) => {
const [file] = e.target.files!;
const url = await callApiToGetPresignedUrl(...);
await fetch(url.data.presignedUrl, {
method: "PUT",
body: file,
});
}}
/>
Yap Yee Qiang
02/22/2022, 3:57 PM