I am trying out a new tool called SST which handles configuring AWS services and full stack web apps via code. In the API tutorial we create the following function to upload a file:
import { Resource } from "sst";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import {
S3Client,
GetObjectCommand,
PutObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";
const s3 = new S3Client({});
export async function upload() {
const command = new PutObjectCommand({
Key: crypto.randomUUID(),
Bucket: Resource.MyBucket.name,
});
const body = await getSignedUrl(s3, command);
return {
statusCode: 200,
body
};
}
That is the entire upload function. As you can see there is no explicit handling of any files – I had initially thought this would create an empty file in the specified S3 bucket.
Next, once our server is live we send a curl request with a file to upload:
curl --upload-file somefile.txt "$(curl https://sometestapi.execute-api.eu-west-1.amazonaws.com)"
However when I check the S3 bucket via the AWS console I can see somefile.txt
and all its content is there. I’m really not understanding a) how this file is being picked up by the API – there is nothing explicit going on here, and b) how this file is then being sent to S3.
Logging command
and body
I see the following:
| +26ms {
| +26ms command: PutObjectCommand {
| +26ms middlewareStack: {
| +26ms add: [Function: add],
| +26ms addRelativeTo: [Function: addRelativeTo],
| +26ms clone: [Function: clone],
| +26ms use: [Function: use],
| +26ms remove: [Function: remove],
| +26ms removeByTag: [Function: removeByTag],
| +26ms concat: [Function: concat],
| +26ms applyToStack: [Function: cloneTo],
| +26ms identify: [Function: identify],
| +26ms identifyOnResolve: [Function: identifyOnResolve],
| +26ms resolve: [Function: resolve]
| +26ms },
| +26ms serialize: [AsyncFunction: se_PutObjectCommand],
| +26ms deserialize: [AsyncFunction: de_PutObjectCommand],
| +26ms input: {
| +26ms Key: '860d1393-4232-48fc-4d4b-b42c0c8a6473',
| +26ms Bucket: 'mytestprojectname-testbucket-tokfzzao'
| +26ms }
| +26ms }
| +26ms }
| +26ms {
| +26ms body: 'https://mytestprojectname-testbucket-tokfzzao.s3.eu-west-1.amazonaws.com/860d1393-4232-48fc-9d4a-b42c0c8a6473?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=<removed-credentials>&X-Amz-Date=20240607T170451Z&X-Amz-Expires=900&X-Amz-Security-Token=<removed-security-token>&X-Amz-Signature=<removed-signature>&X-Amz-SignedHeaders=host&x-id=PutObject'
| +26ms }
I have tried debugging all the available variables in the function to see if any contain the file being uploaded, but neither do. I had expected since no file is being explicitly handled it would not be uploaded to S3.
Josh Jahans is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.