con*_*234 137 amazon-s3 amazon-web-services
我正在使用PHP库将文件上传到我的存储桶.我已将ACL设置为public-read-write,它工作正常,但该文件仍然是私有的.
我发现,如果我将受赠人更改为Everyone,则会将该文件公开.我想知道的是如何将我桶中所有对象的默认Grantee设置为"Everyone".或者是否有其他解决方案默认公开文件?
我正在使用的代码如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
jax*_*xbo 282
转到http://awspolicygen.s3.amazonaws.com/policygen.html
填写详细信息,例如:
在Action中选择"GetObject"选择"Add Statement"然后选择"Generate Policy"
复制文本示例:
{
"Id": "Policy1397632521960",
"Statement": [
{
"Sid": "Stmt1397633323327",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::bucketnm/*",
"Principal": {
"AWS": [
"*"
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在转到AWS S3控制台,在存储桶级别,单击"属性","展开权限",然后选择"添加存储桶策略".将上面生成的代码粘贴到编辑器中并点击保存.
默认情况下,存储桶中的所有项目都将公开.
dcr*_*cro 125
如果您希望默认情况下公开所有对象,最简单的方法是通过Bucket Policy而不是在每个单独对象上定义的访问控制列表(ACL)来实现.
您可以使用AWS Policy Generator为您的存储桶生成存储桶策略.
例如,以下策略将允许任何人读取S3存储桶中的每个对象(只需替换<bucket-name>为存储桶的名称):
{
"Id": "Policy1380877762691",
"Statement": [
{
"Sid": "Stmt1380877761162",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::<bucket-name>/*",
"Principal": {
"AWS": [
"*"
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
Bucket Policy包含一个列表,Statements每个语句都有Effect(Allow或者Deny)一个列表,Actions由Principal(用户)在指定的Resource(由Amazon Resource Name或标识ARN)执行.
该Id仅仅是一个可选的政策id和Sid是一个可选的唯一语句ID.
对于S3存储桶策略,资源ARN采用以下形式:
arn:aws:s3:::<bucket_name>/<key_name>
Run Code Online (Sandbox Code Playgroud)
上面的例子允许(Effect: Allow)任何人(Principal: *)访问(Action: s3:GetObject)桶中的任何对象(Resource: arn:aws:s3:::<bucket-name>/*).
Ser*_*pov 23
我的问题略有不同,但由于这个问题在谷歌搜索的顶部,我会留下我的解决方案,也许它会对某人有所帮助。
我之前已经拥有对 S3 存储桶的完全访问权限,但有一天它刚刚开始返回Access Denied到我的所有文件。解决方案很简单。
Services-S3Permissions选项卡,然后转到Bucket Policy选项卡Save按钮。它应该重新分配对所有文件的权限。
无论如何,这里是 fullbucket 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/*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
小智 5
上面的 JSON 对我不起作用,但我发现它可以工作。我已经在多个桶上进行了测试。我可以以编程方式写入存储桶并同时读取它们
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddCannedAcl",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<bucket_name>/*"
}
]
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
76277 次 |
| 最近记录: |