Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
etc-tiago opened this issue Jun 2, 2021 · 4 comments
Open

Comments

@etc-tiago
Copy link

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.

@kherock
Copy link
Collaborator

kherock commented Jun 3, 2021

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.

@etc-tiago
Copy link
Author

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:

exports.uploadPart = async function uploadPart(ctx) {

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:

exports.getObjectTagging = async function getObjectTagging(ctx) {
const key = ctx.params.key;
const exists = await ctx.store.existsObject(ctx.params.bucket, key);
if (!exists) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: key,
});
}

and

exports.putObjectTagging = async function putObject(ctx) {
const key = ctx.params.key;
const bucket = ctx.params.bucket;
const exists = await ctx.store.existsObject(bucket, key);
if (!exists) {
throw new S3Error('NoSuchKey', 'The specified key does not exist.', {
Key: key,
});
}

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
Copy link
Author

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

@kherock
Copy link
Collaborator

kherock commented Oct 3, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants