如何使用第三方“Amazon S3 PHP类”将存储桶中的每个文件都公开?

uqdfh47h  于 2023-03-07  发布在  PHP
关注(0)|答案(2)|浏览(210)

我尝试使用第三方Amazon S3 PHP Class来使bucket中的每个文件都是公共的,但似乎无法弄清楚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";
            }
            }

        }

    }

有人能帮帮忙吗?

0mkxixxg

0mkxixxg1#

要允许公众访问您的文件,您应该通过以下方式发送它们:

$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);
qij5mzcb

qij5mzcb2#

如果你想公开一个文件,那么在权限数组中设置uri为“http://acs.amazonaws.com/groups/global/AllUsers“,权限为“READ”
如果(!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";
        }
        }

    }

}

相关问题