如何在默认情况下,使在AWS S3存储桶全部公开的对象? [英] How to make all Objects in AWS S3 bucket public by default?

查看:969
本文介绍了如何在默认情况下,使在AWS S3存储桶全部公开的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是PHP库文件上传到我的水桶。我已经设置了访问控制列表,公共读写,它工作正常,但文件仍然是私有的。

我发现,如果我受让人改变每一个人,它使文件公开。我想知道的是如何让我的所有对象的默认承授人在我的水桶被设置为所有人。还是有另一种解决方案,在默认情况下使文件公开?

code我现在用的就是如下:

 公共静态函数putObject($输入,$桶,$ URI,$ ACL =自:: ACL_PRIVATE,$ metaHeaders =阵列(),$ requestHeaders =阵列()){
    如果($输入===假)返回false;
    $其余=新S3Request('PUT',$桶,$ URI);

    如果(IS_STRING($输入))$输入=阵列(
        '数据'=> $输入,'大小'=> strlen的($输入),
        '的md5sum'=> base64_en code(MD5($输入,真))
    );

    // 数据
    如果(使用isset($输入['计划生育']))
        $ REST-> FP =安培; $输入['计划生育'];
    ELSEIF(使用isset($输入['文件']))
        $ REST-> FP = @fopen($输入['文件'],RB);
    ELSEIF(使用isset($输入['数据']))
        $ REST->数据= $输入['数据'];

    //内容长度(需要)
    如果(使用isset($输入['大小'])及和放大器; $输入['大小']> = 0)
        $ REST->大小= $输入['大小'];
    其他 {
        如果(使用isset($输入['文件']))
            $ REST->大小=档案大小($输入['文件']);
        ELSEIF(使用isset($输入['数据']))
            $ REST->大小=的strlen($输入['数据']);
    }

    //自定义请求头(内容类型,内容处置,内容编码)
    如果(is_array($ requestHeaders))
        的foreach($ requestHeaders为$ H => $ V)$ REST-> SetHeader可以($小时,$ V);
    ELSEIF(IS_STRING($ requestHeaders))//支持传统的contentType参数
        $输入['类型'] = $ requestHeaders;

    //内容类型
    如果(!使用isset($输入['型'])){
        如果(使用isset($ requestHeaders ['Content-Type的']))
            $输入['类型'] =&放大器; $ requestHeaders ['内容类型'];
        ELSEIF(使用isset($输入['文件']))
            $输入['类型'] =自我:: __ getMimeType($输入['文件']);
        其他
            $输入['类型'] ='应用程序/八位字节流;
    }

    //我们需要发布与内容长度和内容类型,MD5是可选
    如果($ REST->大小> = 0&功放;及($ REST-> FP ==虚假|| $ REST->!数据== FALSE)){
        $ REST-> SetHeader可以(内容类型,$输入['型']);
        如果(使用isset($输入['的md5sum']))$ REST-> SetHeader可以(内容-MD5,$输入['的md5sum']);

        $ REST-> setAmzHeader(X-AMZ-ACL',$ ACL);
        的foreach($ metaHeaders为$ H => $ V)$ REST-> setAmzHeader(X-AMZ-元 - '$ H,$ V);
        $ REST-> GETRESPONSE();
    } 其他
        $ REST->响应 - >错误=阵列('code'=大于0,'信息'=>'缺少输入参数');

    如果($ REST->响应 - >错误===假放;&安培; $ REST->响应 - >!code == 200)
        $ REST->响应 - >错误=阵列('code'=> $ REST->响应 - > code,'消息'=>意外的HTTP状态');
    如果($ REST->响应 - >!错误== FALSE){
        trigger_error(sprintf的(S3 :: putObject():[%S]%S,$ REST->响应 - >错误['code'],$ REST->响应 - >错误[ '消息']),E_USER_WARNING);
        返回false;
    }
    返回true;
}
 

解决方案

如果你想在默认情况下所有对象公开,最简单的方法就是做一个槽的bucket政策代替的ACL。

您可以使用 AWS策略生成器,为您的斗一斗的政策。

例如这桶策略将允许任何人阅读每一个对象在你的S3存储桶(只需更换< bucket_name> 与你斗的名字):

  {
  ID:Policy1380877762691
  声明: [
    {
      锡德:Stmt1380877761162
      行动: [
        S3:GetObject的
      ]
      效果:允许,
      资源:ARN:AWS:S3 :::< bucket_name> / *,
      主要: {
        AWS:[
          *
        ]
      }
    }
  ]
}
 

I am using a PHP library to upload a file to my bucket. I have set the ACL to public-read-write and it works fine but the file is still private.

I found that if I change the Grantee to Everyone it makes the file public. What I want to know is how do I make the default Grantee on all objects in my bucket to be set to "Everyone". Or is there another solution to make files public by default?

Code I am using is below:

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;
}

解决方案

If you want to make all objects public by default, the simplest way is to do it trough a bucket policy instead of ACLs.

You can use the AWS Policy Generator to generate a bucket policy for your bucket.

For example this bucket policy will allow anyone to read every object in your S3 bucket (just replace <bucket_name> with the name of your bucket):

{
  "Id": "Policy1380877762691",
  "Statement": [
    {
      "Sid": "Stmt1380877761162",
      "Action": [
        "s3:GetObject"
      ],
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::<bucket_name>/*",
      "Principal": {
        "AWS": [
          "*"
        ]
      }
    }
  ]
}

这篇关于如何在默认情况下,使在AWS S3存储桶全部公开的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆