使用AWS S3静态网站托管上传文件

3

我们一直在寻找上传文件到 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>

当我尝试使用Web托管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 webhosting上传文件到存储桶中,而不需要任何编码。这是正确的方式吗?我错过了什么吗?

1个回答

4
您无法在S3静态网站上运行任何PHP文件 - 这需要一个后端服务器,而您的表单正在尝试发布到'action_page.php'。
那根本行不通。
以下是一个示例,您可以按照此示例继续操作:
在这个示例中,一个简单的HTML页面提供了一个基于浏览器的应用程序,用于在Amazon S3存储桶中创建相册,您可以将照片上传到其中。该应用程序允许您删除添加的照片和相册。

http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接