s3rver icon indicating copy to clipboard operation
s3rver copied to clipboard

key should be Key, capitalized, following the AWS SDK documentation

Open etc-tiago opened this issue 3 years ago • 4 comments

Following docs.aws.amazon.com, the key parameter should be Key. I did some tests with putObject and it works both ways. However when using s3.createPresignedPost using Key, it fails with S3rver and works on AWS and with key works in S3rver and fail on AWS.

For internal testing, I decided to change all code references of S3rver to Key and my tests passed and the code worked on AWS.

I thought about sending a pull request, but the repository tests didn't pass yet.

I would like to know the opinion of the maintainer (@kherock) about the change. If it is of interest, I can make myself available to make the whole process work - with Key instead of key - in a future pull request.

etc-tiago avatar Jun 02 '21 17:06 etc-tiago

Could you provide a more detailed example resembling your internal tests? I'm not too familiar with how s3.createPresignedPostinteracts with the S3 API. My guess is that the POST endpoint just needs to be made case-insensitive when reading request body parameters.

kherock avatar Jun 03 '21 20:06 kherock

Of course!

The idea of s3.createPresignedPost is to upload directly to S3 on the frontend. And also for uploading files larger than API Gateway (10mb) and EC2 can support or have a higher price for this function.

Then you can create a link (server) with parameters for submission and add it to a form (client).

So let's say you add the filename that will be inside s3, control the expiry time and set the upload condition, something like:

{
  Bucket: "example",
  Key: "path/of/file.pdf",
  Conditions: [["content-length-range", 0, 10240000]], // optional
  Expires: 3600,
}

To use in form:

// URL_VALUE url to upload
// VALUE params to use on upload
<form action="URL_VALUE" method="post" enctype="multipart/form-data">
  <input type="hidden" name="key" value="VALUE" />
  <input type="hidden" name="AWSAccessKeyId" value="VALUE" />
  <input type="hidden" name="policy" value="VALUE" />
  <input type="hidden" name="signature" value="VALUE" />
  File:
  <input type="file" name="file" /> <br />
  <input type="submit" name="submit" value="Upload to Amazon S3" />
</form>

For some reason, in the documentation it displays as key and in the code as Key: Docs: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/modules/_aws_sdk_s3_presigned_post.html Code: https://github.com/aws/aws-sdk-js-v3/blob/72611ca19b/packages/s3-presigned-post/src/createPresignedPost.ts#L19

In order for the code to work on AWS, I needed to change from <input type="hidden" name="key" value="VALUE" /> to <input type="hidden" name="Key" value="VALUE" />, but the process fails in place.

In order for it to work with S3rver, I changed all references from key to Key. The rest of the S3 sending and receiving processes, such as profile photo, were not affected and work in both cases in s3rver and aws.

I think the process used refers to the uploadPart function: https://github.com/jamhall/s3rver/blob/005feae2e63020f93e0d3452df4e19ac427be4d1/lib/controllers/object.js#L693

I think the simplest solution would be to check if the Key exists when key does not exist, because has 2 references when use uploadPart: https://github.com/jamhall/s3rver/blob/005feae2e63020f93e0d3452df4e19ac427be4d1/lib/controllers/object.js#L221-L228

and

https://github.com/jamhall/s3rver/blob/005feae2e63020f93e0d3452df4e19ac427be4d1/lib/controllers/object.js#L571-L579

Maybe can use like:

const key = ctx.params.key || ctx.params.Key;

But for now it's all guesswork, I don't have the opportunity and time to analyze each process in depth.

etc-tiago avatar Jun 04 '21 17:06 etc-tiago

I submitted a pull request that allows this change without braking changes. #750

etc-tiago avatar Jun 10 '21 11:06 etc-tiago

It still looks like the parameter name sent over the wire is lowercase. From the same file you linked in the JS SDK it takes the capitalized parameter name and sends it as lowercase: https://github.com/aws/aws-sdk-js-v3/blob/72611ca19bfe43ae6cee0e65dbb0a83689f76c5d/packages/s3-presigned-post/src/createPresignedPost.ts#L92

This aligns with the POST Object docs https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html#RESTObjectPOST-requests-form-fields

kherock avatar Oct 03 '21 16:10 kherock