我正在使用 PHP 库将文件上传到我的存储桶。我已将 ACL 设置为 public-read-write 并且工作正常,但该文件仍然是私有的。
我发现如果我将受让人更改为所有人,它会使文件公开。我想知道的是如何将存储桶中所有对象的默认受让人设置为“所有人”。或者是否有另一种解决方案可以默认公开文件?
我正在使用的代码如下:
public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $requestHeaders = array()) {
if ($input === false) return false;
$rest = new S3Request('PUT', $bucket, $uri);
if (is_string($input)) $input = array(
'data' => $input, 'size' => strlen($input),
'md5sum' => base64_encode(md5($input, true))
);
// Data
if (isset($input['fp']))
$rest->fp =& $input['fp'];
elseif (isset($input['file']))
$rest->fp = @fopen($input['file'], 'rb');
elseif (isset($input['data']))
$rest->data = $input['data'];
// Content-Length (required)
if (isset($input['size']) && $input['size'] >= 0)
$rest->size = $input['size'];
else {
if (isset($input['file']))
$rest->size = filesize($input['file']);
elseif (isset($input['data']))
$rest->size = strlen($input['data']);
}
// Custom request headers (Content-Type, Content-Disposition, Content-Encoding)
if (is_array($requestHeaders))
foreach ($requestHeaders as $h => $v) $rest->setHeader($h, $v);
elseif (is_string($requestHeaders)) // Support for legacy contentType parameter
$input['type'] = $requestHeaders;
// Content-Type
if (!isset($input['type'])) {
if (isset($requestHeaders['Content-Type']))
$input['type'] =& $requestHeaders['Content-Type'];
elseif (isset($input['file']))
$input['type'] = self::__getMimeType($input['file']);
else
$input['type'] = 'application/octet-stream';
}
// We need to post with Content-Length and Content-Type, MD5 is optional
if ($rest->size >= 0 && ($rest->fp !== false || $rest->data !== false)) {
$rest->setHeader('Content-Type', $input['type']);
if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']);
$rest->setAmzHeader('x-amz-acl', $acl);
foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v);
$rest->getResponse();
} else
$rest->response->error = array('code' => 0, 'message' => 'Missing input parameters');
if ($rest->response->error === false && $rest->response->code !== 200)
$rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
if ($rest->response->error !== false) {
trigger_error(sprintf("S3::putObject(): [%s] %s", $rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING);
return false;
}
return true;
}
https://i.stack.imgur.com/G2SMP.png
复制文本示例:
{
"Id": "Policy1397632521960",
"Statement": [
{
"Sid": "Stmt1397633323327",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::bucketnm/*",
"Principal": {
"AWS": [
"*"
]
}
}
]
}
现在转到您的 AWS S3 控制台,在存储桶级别,单击属性,展开权限,然后选择添加存储桶策略。将上面生成的代码粘贴到编辑器中并点击保存。
默认情况下,存储桶中的所有项目都是公开的。
如果您希望默认公开所有对象,最简单的方法是通过 Bucket Policy 而不是在每个单独对象上定义的访问控制列表 (ACL) 来实现。
https://i.stack.imgur.com/zoeOE.png
您可以使用 AWS Policy Generator 为您的存储桶生成存储桶策略。
例如,以下策略将允许任何人读取您的 S3 存储桶中的每个对象(只需将 <bucket-name>
替换为您的存储桶名称):
{
"Id": "Policy1380877762691",
"Statement": [
{
"Sid": "Stmt1380877761162",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<bucket-name>/*",
"Principal": {
"AWS": [
"*"
]
}
}
]
}
存储桶策略包含 Statements
列表,每个语句都有一个 Effect
(Allow
或 Deny
),用于 Principal
(用户)在指定的 Actions
上执行的列表7}(由 Amazon Resource Name
或 ARN
标识)。
Id
只是一个可选的策略 ID,而 Sid
是一个可选的唯一语句 ID。
对于 S3 存储桶策略,资源 ARN 采用以下形式:
arn:aws:s3:::<bucket_name>/<key_name>
上面的示例允许 (Effect: Allow
) 任何人 (Principal: *
) 访问 (Action: s3:GetObject
) 存储桶 (Resource: arn:aws:s3:::<bucket-name>/*
) 中的任何对象。
Principal: *
对我帮助很大。谢谢!
我的问题略有不同,但由于这个问题在谷歌搜索的顶部,我会留下我的解决方案,也许它会对某人有所帮助。
我之前已经拥有对 S3 存储桶的完全访问权限,但有一天它刚刚开始将 Access Denied
返回到我的所有文件。解决方案很简单。
转到服务 - S3 单击您的 S3 存储桶切换到权限选项卡,然后转到存储桶策略选项卡并单击保存按钮。
它应该重新分配对所有文件的权限。
https://i.stack.imgur.com/FMQiF.png
无论如何,这里是完整的 bucket policy
,它允许公开所有对象
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::enter-here-your-media-bucket-name/*"
}
]
}
s3:GetObject
操作进行读取访问。
上面的 JSON 对我不起作用,但我发现它起作用了。我已经在多个存储桶上对其进行了测试。我可以以编程方式写入存储桶并同时读取它们
{“版本”:“2012-10-17”,“声明”:[{“Sid”:“AddCannedAcl”,“效果”:“允许”,“主体”:“*”,“行动”:“s3: GetObject", "Resource": "arn:aws:s3:::
它很简单......只需将其添加到您的存储桶策略中......这将使您的存储桶可公开访问。我这样做是因为我的存储桶只保存图像和徽标,因此公开它是有意义的,因为没有敏感或用户特定的数据/资产。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1380877761162",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket-assets/*"
}
]
}
/*
附加到资源名称以避免错误“存储桶操作不适用于任何资源”:stackoverflow.com/questions/44228422/…