繁体   English   中英

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

[英]How can I use the 3rd party 'Amazon S3 PHP Class' to make every file in a bucket public?

我正在尝试使用第三方Amazon S3 PHP类将存储桶中的每个文件公开,但似乎无法使用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";
            }
            }

        }

    }

有人可以帮忙吗?

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

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

如果要公开文件,则在权限数组中将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";
        }
        }

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM