我如何使用第三方 Amazon S3 PHP Class'将桶中的每个文件公开?

时间:2012-03-09 14:15:13

标签: php amazon-s3 amazon acl public

我正在尝试使用第三方Amazon S3 PHP Class将存储桶中的每个文件公开,但似乎无法排除acl控制策略 - 我尝试了以下代码但没有成功:

if (!class_exists('S3')) require 's3/S3.php';

    // AWS access info
    if (!defined('awsAccessKey')) define('awsAccessKey', $as3key);
    if (!defined('awsSecretKey')) define('awsSecretKey', $assecretkey);

    $s3 = new S3(awsAccessKey, awsSecretKey);

    $bucket = ltrim($_POST['bucket']);
    $policy = ltrim($_POST['policy']);

    if (($contents = $s3->getBucket($bucket)) !== false) {

        foreach ($contents as $object) {

            $fname = $object['name'];

            $furl = "https://". $bucket . ".s3.amazonaws.com/".rawurlencode($fname);

            if (($acp = S3::getAccessControlPolicy($bucket,$fname)) !== false) {
            // Here you would modify the $acp array...
            // For example, grant access to the S3 LogDelivery system:
            $acp["acl"][] = array( 
                "type" => "Group", "uri" => $fname, "permission" => "FULL_CONTROL"
            );
            // Then update the policy using the modified $acp array:
            if (S3::setAccessControlPolicy($bucket, $fname, $acp)) {
                echo $fname . "Policy updated";
            }
            }

        }

    }

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

要允许公开访问您的文件,您应该通过这种方式发送它们:

$file_upload_response = $s3->create_object($bucket, $file_on_amazon_s3, array (
                        'fileUpload' => $file_attach,
                        'acl' => AmazonS3::ACL_PUBLIC
                    ));

如果您想在上传后更改为公开访问权限:

$s3_response = $s3->set_object_acl($bucket,
                                    $file_on_amazon_s3,
                                    AmazonS3::ACL_PUBLIC);

答案 1 :(得分:0)

如果要公开文件,请在权限数组中将uri设置为“http://acs.amazonaws.com/groups/global/AllUsers”并允许“READ”

if(!class_exists('S3'))需要's3 / S3.php';

// AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', $as3key);
if (!defined('awsSecretKey')) define('awsSecretKey', $assecretkey);

$s3 = new S3(awsAccessKey, awsSecretKey);

$bucket = ltrim($_POST['bucket']);
$policy = ltrim($_POST['policy']);

if (($contents = $s3->getBucket($bucket)) !== false) {

    foreach ($contents as $object) {

        $fname = $object['name'];

        $furl = "https://". $bucket . ".s3.amazonaws.com/".rawurlencode($fname);

        if (($acp = S3::getAccessControlPolicy($bucket,$fname)) !== false) {
        // Here you would modify the $acp array...
        // For example, grant access to the S3 LogDelivery system:
        $acp["acl"][] = array( 
            "type" => "Group", "uri" => "http://acs.amazonaws.com/groups/global/AllUsers", "permission" => "READ"
        );
        // Then update the policy using the modified $acp array:
        if (S3::setAccessControlPolicy($bucket, $fname, $acp)) {
            echo $fname . "Policy updated";
        }
        }

    }

}