简体   繁体   中英

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

I am trying to use the 3rd party Amazon S3 PHP Class to make every file in a bucket public, but can't seem to suss out the acl control policy - I have tried the following code without success:

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

        }

    }

Can someone please help?

To allow public access to your files you should send them by this way :

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

If you want to change to public access after upload :

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

If you want to make a file public then In the permission array set uri to " http://acs.amazonaws.com/groups/global/AllUsers " and permission to "READ"

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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