我正在開發一個web應用程序,使用預先簽署的帖子,Id想要使用該應用程序將文件上傳到Amazon S3。按照此鏈接上的說明:http://aws.amazon.com/articles/1434我有以下表單。上傳文件amazon s3預先簽署的文章
<form action=<%= @aws_s3_url %> method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value= <%= @base_path + "${filename}" %> >
<input type="hidden" name="AWSAccessKeyId" value= <%= @aws_access_key_id %> >
<input type="hidden" name="acl" value="private">
<input type="hidden" name="success_action_redirect" value=<%= "https://localhost:3000/projects/" + @project.id.to_s %> >
<input type="hidden" name="policy" value= <%= @aws_policy %>>
<input type="hidden" name="signature" value=<%= @aws_signature %>>
<!-- Include any additional input fields here -->
File to upload to S3:
<input name="file" type="file">
<br>
<input type="submit" value="Upload File to S3">
</form>
其中@aws_s3_url = "https://s3-eu-west-1.s3.amazonaws.com/my_bucket"
的策略與該方法產生的:
def generate_policy_pre_signed_post(key)
policy = "{ \"expiration\": \"#{Time.now.tomorrow.utc.iso8601}\","
policy +="\"conditions\": ["
policy += "{\"acl\": \"private\" },"
policy += "{\"success_action_redirect\": \"https://localhost:3000/projects/#{@project.id.to_s}\"},"
policy += "{\"bucket\": \"my_bucket\" },"
policy += "[\"starts-with\", \"$key\", \"#{key}\"],]}"
policy
end
然後編碼和類似的簽名在上面的鏈接: 政策= generate_policy_pre_signed_post( 「/ users_data /」 + user_creds.aws_identity_id +「/」)
@aws_policy = Base64.encode64(policy).gsub("\n","")
@aws_signature = Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest::Digest.new('sha1'),
server_credentials.secret_access_key, @aws_policy)
).gsub("\n","")
但是,當我嘗試將文件上載到S3時,它會出現以下錯誤:「指定的方法不允許針對此資源。」
這怎麼可能?在IAM我的用戶擁有所有寫入S3的權限,以及香港專業教育學院也嘗試添加一個水桶政策像這樣的:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "statement1",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::my_iam"
},
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::my_bucket/*"
]
}
]
}
什麼可能發生的任何想法?
編輯: 香港專業教育學院還試圖修改CORS權限(和它仍然不工作)
<CORSRule>
<AllowedOrigin>https://0.0.0.0:3000/*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>