S3 Presigned Post requires a certain type of content

For starters, I use ruby ​​aws-sdk, but what I encounter seems to be a misunderstanding of the S3 posts assigned.


What I want to make sure that everything that is uploaded to S3 with my assigned post is a video.

What I tried is to content_type_starts_with: "video/"set presigned_post in the hash setting. This will cause all downloads to be rejected using Policy Condition failed: ["starts-with", "$Content-Type", "video/"].

Then I tried simply content_type: "video/mp4", but this causes s3 to allow the download of any file, and then just puts it in the metadata Content-Type: "video/mp4".

Then I noticed that when I upload a file without Content-Type restriction, S3 marks the file as binary/octet-stream. So I tried the first method again, but this time with content_type_starts_with: "binary/". The same results as before Policy Condition Failed.

How can I use the Content-Type field? It seems he never does what I want.


Here is the code snippet I'm using:

# In the controller

client = Aws::S3::Client.new(region: 'us-east-1')
resource = Aws::S3::Resource.new(client: client)
bucket = resource.bucket("my-bucket")

object_id = SecureRandom.uuid
object = bucket.object(object_id)

@presigned_post = object.presigned_post({
  acl: "private",
  content_length_range: 0..104857600, # 100 MB
  content_type_starts_with: "video/",
})

# In the presigned post erb

<form action="<%= presigned_post.url %>" method="post" enctype="multipart/form-data">
<% presigned_post.fields.each do |name, value| %>
    <input type="hidden" name="<%= name %>" value="<%= value %>"/>
<% end %>
    <input type="file" name="file" accept="*.mp4,*.ogg,video/*" />
    <input type="submit">
</form>
+4
source share
1 answer

I had a very similar problem and I decided that I had solved. Hope this helps.

, content_type_starts_with , , content-type , , .

JavaScript, : formData.set('content-type', file.type) formData - formData, XMLHttpRequest.

PDF , S3, MIME.

, , : http://blog.teamtreehouse.com/uploading-files-ajax

+1

All Articles