PHP S3 EXAMPLES

PHP S3 EXAMPLES

INSTALLING AWS PHP SDK

This installs AWS PHP SDK using composer (see here how to install composer).

$ composer install aws/aws-sdk-php

CREATING A CONNECTION

This creates a connection so that you can interact with the server.

Note

The client initialization requires a region so we use ''.

<?php

use Aws\S3\S3Client;

define('AWS_KEY', 'place access key here');
define('AWS_SECRET_KEY', 'place secret key here');
$ENDPOINT = 'http://objects.dreamhost.com';

// require the amazon sdk from your composer vendor dir
require __DIR__.'/vendor/autoload.php';

// Instantiate the S3 class and point it at the desired host
$client = new S3Client([
    'region' => '',
    'version' => '2006-03-01',
    'endpoint' => $ENDPOINT,
    'credentials' => [
        'key' => AWS_KEY,
        'secret' => AWS_SECRET_KEY
    ],
    // Set the S3 class to use objects.dreamhost.com/bucket
    // instead of bucket.objects.dreamhost.com
    'use_path_style_endpoint' => true
]);

LISTING OWNED BUCKETS

This gets a AWS\Result instance that is more convenient to visit using array access way. This also prints out the bucket name and creation date of each bucket.

<?php
$listResponse = $client->listBuckets();
$buckets = $listResponse['Buckets'];
foreach ($buckets as $bucket) {
    echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
}

The output will look something like this:

mahbuckat1   2011-04-21T18:05:39.000Z
mahbuckat2   2011-04-21T18:05:48.000Z
mahbuckat3   2011-04-21T18:07:18.000Z

CREATING A BUCKET

This creates a new bucket called my-new-bucket and returns a AWS\Result object.

<?php
$client->createBucket(['Bucket' => 'my-new-bucket']);

LIST A BUCKET’S CONTENT

This gets a AWS\Result instance that is more convenient to visit using array access way. This then prints out each object’s name, the file size, and last modified date.

<?php
$objectsListResponse = $client->listObjects(['Bucket' => $bucketname]);
$objects = $objectsListResponse['Contents'] ?? [];
foreach ($objects as $object) {
    echo $object['Key'] . "\t" . $object['Size'] . "\t" . $object['LastModified'] . "\n";
}

Note

If there are more than 1000 objects in this bucket, you need to check $objectsListResponse[‘isTruncated’] and run again with the name of the last key listed. Keep doing this until isTruncated is not true.

The output will look something like this if the bucket has some files:

myphoto1.jpg 251262  2011-08-08T21:35:48.000Z
myphoto2.jpg 262518  2011-08-08T21:38:01.000Z

DELETING A BUCKET

This deletes the bucket called my-old-bucket and returns a AWS\Result object

Note

The Bucket must be empty! Otherwise it won’t work!

<?php
$client->deleteBucket(['Bucket' => 'my-old-bucket']);

CREATING AN OBJECT

This creates an object hello.txt with the string "Hello World!"

<?php
$client->putObject([
    'Bucket' => 'my-bucket-name',
    'Key' => 'hello.txt',
    'Body' => "Hello World!"
]);

CHANGE AN OBJECT’S ACL

This makes the object hello.txt to be publicly readable and secret_plans.txt to be private.

<?php
$client->putObjectAcl([
    'Bucket' => 'my-bucket-name',
    'Key' => 'hello.txt',
    'ACL' => 'public-read'
]);
$client->putObjectAcl([
    'Bucket' => 'my-bucket-name',
    'Key' => 'secret_plans.txt',
    'ACL' => 'private'
]);

DELETE AN OBJECT

This deletes the object goodbye.txt

<?php
$client->deleteObject(['Bucket' => 'my-bucket-name', 'Key' => 'goodbye.txt']);

DOWNLOAD AN OBJECT (TO A FILE)

This downloads the object poetry.pdf and saves it in /home/larry/documents/

<?php
$object = $client->getObject(['Bucket' => 'my-bucket-name', 'Key' => 'poetry.pdf']);
file_put_contents('/home/larry/documents/poetry.pdf', $object['Body']->getContents());

GENERATE OBJECT DOWNLOAD URLS (SIGNED AND UNSIGNED)

This generates an unsigned download URL for hello.txt. This works because we made hello.txt public by setting the ACL above. This then generates a signed download URL for secret_plans.txt that will work for 1 hour. Signed download URLs will work for the time period even if the object is private (when the time period is up, the URL will stop working).

<?php
$hello_url = $client->getObjectUrl('my-bucket-name', 'hello.txt');
echo $hello_url."\n";

$secret_plans_cmd = $client->getCommand('GetObject', ['Bucket' => 'my-bucket-name', 'Key' => 'secret_plans.txt']);
$request = $client->createPresignedRequest($secret_plans_cmd, '+1 hour');
echo $request->getUri()."\n";

The output of this will look something like:

 

0
0
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

Cloud

번호 제목 글쓴이 날짜 조회수
2 Object Stroage Client Tool - s3fs 관리자 05-14 233
1 PHP S3 EXAMPLES 관리자 05-09 253