Created
October 22, 2013 13:35
-
-
Save fridolin-koch/7100906 to your computer and use it in GitHub Desktop.
PHP AWS SDK - S3 StreamWrapper with ACL and ContentType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//autoloader | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Aws\S3\S3Client; | |
use Aws\S3\Enum\CannedAcl; | |
//init client | |
$s3Client = S3Client::factory(array( | |
'key' => '<key>', | |
'secret' => '<secret>', | |
'region' => 'eu-west-1' | |
)); | |
//register wrapper | |
$s3Client->registerStreamWrapper(); | |
define('S3_PATH', 's3://<bucket>/aws-acl-test/'); | |
function createRandomFileWithPermission($acl, $filename) | |
{ | |
//create content | |
$string = ''; | |
for ($i=0; $i < 4096; $i++) { | |
$char = ''; | |
switch (rand(0, 2)) { | |
//A-Z | |
case 0: | |
$char = chr(rand(65, 90)); | |
break; | |
//a-z | |
case 1: | |
$char = chr(rand(97, 122)); | |
break; | |
//0-9 | |
case 2: | |
$char = chr(rand(97, 122)); | |
break; | |
} | |
$string .= $char; | |
} | |
//create stream context | |
$context = stream_context_create(array( | |
's3' => array( | |
'ACL' => $acl, | |
'ContentType' => 'text/plain' | |
) | |
)); | |
//create file with file_put_contents | |
file_put_contents(S3_PATH . 'file_put_contents_'.$filename.'.txt', $string, null, $context); | |
//create file with fopen | |
$stream = fopen(S3_PATH . 'fopen_'.$filename.'.txt', 'w', null, $context); | |
fwrite($stream, $string); | |
fclose($stream); | |
} | |
createRandomFileWithPermission(CannedAcl::PRIVATE_ACCESS, 'private_access'); | |
createRandomFileWithPermission(CannedAcl::PUBLIC_READ, 'public_read'); | |
createRandomFileWithPermission(CannedAcl::PUBLIC_READ_WRITE, 'public_read_write'); | |
createRandomFileWithPermission(CannedAcl::AUTHENTICATED_READ, 'authenticated_read'); | |
createRandomFileWithPermission(CannedAcl::BUCKET_OWNER_FULL_CONTROL, 'bucket_owner_full_control'); | |
createRandomFileWithPermission(CannedAcl::BUCKET_OWNER_READ, 'bucket_owner_read'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "fridokoch/aws-acl-test", | |
"require": { | |
"aws/aws-sdk-php": "dev-master" | |
}, | |
"license": "MIT", | |
"authors": [ | |
{ | |
"name": "Frido Koch", | |
"email": "[email protected]" | |
} | |
], | |
"minimum-stability": "dev" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment