首先,我使用的是ruby的aws-sdk,但我所面對的似乎是對S3的預定帖子的誤解。S3 Presigned Post需要特定的內容類型
我想要做的就是確保上傳到S3的任何內容都是我的預先登記帖子是視頻。
我試過的是在presigned_post哈希值中設置content_type_starts_with: "video/"
。這會導致所有上傳被拒絕Policy Condition failed: ["starts-with", "$Content-Type", "video/"]
。
然後我嘗試了content_type: "video/mp4"
,但是這會導致s3允許上傳任何文件,然後在元數據中使用Content-Type: "video/mp4"
進行標記。
然後我注意到,當我上傳沒有Content-Type約束的文件時,S3會將文件標記爲binary/octet-stream
。所以我再次嘗試了第一種方法,但是這次是content_type_starts_with: "binary/"
。與以前相同的結果,Policy Condition Failed
。
我該如何使用Content-Type字段?它似乎從未做過我想要的事情。
下面是我使用的代碼片段:
# 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>