Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 Upload Whats does Key do

I am really confused as to what the key value should be when using amazon s3 here is my code.

   <form action="http://bucket.s3.amazonaws.com" method="post" enctype="multipart/form-data">
<input type="text" name="key" value="{filename}" />
<input type="text" name="acl" value="public-read" />
<input type="text" name="content-type" value="text/plain" />
<input type="hidden" name="AWSAccessKeyId" value="Amazon Key" />
<input type="hidden" name="policy" value="ewogICJleHBpcmF0aW9uIjogIjIwMTItMDEtMDFUMTI6MDA6MDAuMDAwWiIsCiAgImNvbmRpdGlvbnMiOiBbCiAgICB7ImJ1Y2tldCI6ICJpcIHsiYWNsIjogInB1YmxpYy1yZWFkIiB9LAogICAgWyJlcSIsICIka2V5IiwgIntmaWxlbmFtZX0iXSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJ0ZXh0LyJdLAogIF0KfQo=" />
<input type="hidden" name="signature" value="fGWi1jKU+hKZKbCIL1eD0=" />
<input name="file" type="file" />
<input name="submit" value="Upload" type="submit" />
</form>

Ok so i am using this service to generate my policy etc because i havent got a clue how to do this manually.

http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html

This works and gives me everything to upload. But when i upload my files they are always call {filename} and not the actually filename says picture.jpg. I know this is down to this line.

<input type="text" name="key" value="{filename}" />

I want it to take that value off the actually filename i am uploading.

What am i doing wrong very confused.

I have tried leaving it blank but i get this error.

InvalidArgumentUser key must have a length greater than 0

I want it to work this out for me????

Any Help Please

like image 337
iSimpleDesign Avatar asked Dec 13 '22 16:12

iSimpleDesign


2 Answers

I know you posted this question a long time ago, but someone might have the same question.

Key is the name and path of the file you are uploading.

You can set conditions in you policy regarding the key, like what it should start with. You can keep original file name by using the value: ${filename} (You are missing the $)

Example: Keep original file name, but put in folder /docs/

The form:

<form action="http://yourbucketname.s3.amazonaws.com" method="post" enctype="multipart/form-data">
<input type="text" name="key" value="docs/${filename}" />
<input type="text" name="acl" value="public-read" />
<input type="text" name="content-type" value="text/plain" />
<input type="hidden" name="AWSAccessKeyId" value="<YourPublicKey>" />
<input type="hidden" name="policy" value="<Base64_encoded_your_policy>" />
<input type="hidden" name="signature" value="<HMAC SHA-1 of the policy>" />
<input name="file" type="file" />
<input name="submit" value="Upload" type="submit" />
</form>

The policy as JSON

{"expiration": "2013-12-01T12:00:00.000Z",
"conditions": [
{"acl": "public-read-write" },
{"bucket": "yourbucketname" },
["starts-with", "$key", "docs/"],
["starts-with", "$Content-Type", "text/plain"],
]
}

What you need to do:

  • Replace Yourbucketname in the for action and Policy-Json
  • Base64 encode the policy
  • Set policy in form policy value
  • Replace your AWSAccessKeyId in the form
  • Sign the encoded policy with your Secret Access Key using HMAC SHA-1
  • Set signature in the form signature value
like image 60
Eystein Bye Avatar answered Jan 24 '23 12:01

Eystein Bye


The parameter "key" is the name of the uploaded file in the bucket. You can put different name of browsed file.

like image 37
ExtremeBt Avatar answered Jan 24 '23 13:01

ExtremeBt