我们一直在寻找将文件上传到 S3 的选项。我们正在考虑使用 S3 静态 Web 托管的一种选择。在这个选项中,我们将有一个页面,我正在尝试编写一个带有上传选项的简单 Index.html 页面。我设置了以下存储桶策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Allow Public Access to All Objects",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::<bucketname>/*"
}
]
}
CORS设置如下:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
下面是Index.html中的代码
<html>
<header><title>Hello World</title></header>
<body>
Hello world
<form action="/action_page.php">
<input type="file" name="pic" accept="image/*">
<input type="submit">
</form>
</body>
</html>
当我尝试使用网站托管 URI 访问我的 s3 存储桶时,我收到了页面,当我尝试上传文档时,我收到以下错误:
404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: action_page.php
RequestId: <requestid>
HostId: <hostid>
An Error Occurred While Attempting to Retrieve a Custom Error Document
Code: NoSuchKey
Message: The specified key does not exist.
Key: error.html
如果我添加 error.html 页面,它会显示我的错误页面。
现在我认为使用 S3 虚拟主机将文件上传到存储桶而无需任何编码是正确的方法吗?我在这里缺少什么?
最佳答案
您不能在 S3 静态网站上运行任何 PHP 文件 - 这需要后端服务器,并且您的表单正在尝试发布到“action_page.php”。
那根本行不通。
这只是您可以如何进行的一个示例:
In this example, a simple HTML page provides a browser-based application for creating photo albums in an Amazon S3 bucket into which you can upload photos. The application lets you delete photos and albums that you add.
http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html
关于amazon-web-services - 使用 AWS S3 静态网络托管将文件上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43784139/