오브젝트스토리지

송용근 에반젤리스트

iwinv Object Storage - PHP SDK

 

해당 메뉴얼은 AWS S3에서 제공하는 PHP SDK를 이용하여 iwinv Object Storage를 사용하는 방법을 설명합니다.

 

참고문서

  https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide

  https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/php/example_code/s3

 

 

PHP SDK 사용하기 


 
테스트 OS : Ubuntu 18.04 LTS

 

[인증키 확인]

Endpoint -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> API {IDC} Endpoint   

Access_Key  : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Access Key ID

Secret_Key   : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Secret Key ID

 

 

composer 설치 

Windows:  https://getcomposer.org/Composer-Setup.exe 다운로드 

Linux: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/

 

AWS PHP SDK 설치하기  

composer.phar require aws/aws-sdk-php

 

 

# 예제코드


 

버킷 생성 

 

 PHP 7.X 

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

function createBucket($s3Client, $bucketName)
{
try {
$result = $s3Client->createBucket([
'Bucket' => $bucketName,
]);
return 'The bucket\'s location is: ' .
$result['Location'] . '. ' .
'The bucket\'s effective URI is: ' .
$result['@metadata']['effectiveUri'];
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}

function createTheBucket()
{
$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);


echo createBucket($s3Client, 'Bucket_Name');
}

createTheBucket();

?>

 

 

 PHP 5.X 

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

function createBucket($s3Client, $bucketName)
{
try {
$result = $s3Client->createBucket([
'Bucket' => $bucketName,
]);
return 'The bucket\'s location is: ' .
$result['Location'] . '. ' .
'The bucket\'s effective URI is: ' .
$result['@metadata']['effectiveUri'];
} catch (AwsException $e) {
return 'Error: ' . $e->getAwsErrorMessage();
}
}

function createTheBucket()
{
$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));


echo createBucket($s3Client, 'Bucket_Name');
}

createTheBucket();

?>

 

 

 

버킷 삭제

 

 PHP 7.X 

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$BUCKET_NAME = 'Bucket_Name';

$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);

try {
$objects = $s3Client->getIterator('ListObjects', ([
'Bucket' => $BUCKET_NAME
]));
echo "Keys retrieved!\n";
foreach ($objects as $object) {
echo $object['Key'] . "\n";
$result = $s3Client->deleteObject([
'Bucket' => $BUCKET_NAME,
'Key' => $object['Key'],
]);
}
$result = $s3Client->deleteBucket([
'Bucket' => $BUCKET_NAME,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?> 

 

 PHP 5.X 

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;


$BUCKET_NAME = 'Bucket_Name';

$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));

try {
$objects = $s3Client->getIterator('ListObjects', ([
'Bucket' => $BUCKET_NAME
]));
echo "Keys retrieved!\n";
foreach ($objects as $object) {
echo $object['Key'] . "\n";
$result = $s3Client->deleteObject([
'Bucket' => $BUCKET_NAME,
'Key' => $object['Key'],
]);
}
$result = $s3Client->deleteBucket([
'Bucket' => $BUCKET_NAME,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?> 

 

 

버킷 리스트 확인

 

PHP 7.X

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);


$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";

echo "
";

}


?>

 

PHP 5.X  

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_key,

'secret' => Secret_Key,

'endpoint' => 'EndPoint',));


$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
echo $bucket['Name'] . "\n";

echo "
";

}


?>

 

 

오브젝트 업로드

 

PHP 7.X

 
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$USAGE = "\n" .
"To run this example, supply the name of an S3 bucket and a file to\n" .
"upload to it.\n" .
"\n" .
"Ex: php PutObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$file_Path = $argv[2];
$key = basename($argv[2]);


try {

$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);

$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file_Path,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Path/File_Name]

ex)  php object_upload_aws.php iwinv-bucket /tmp/test.txt

  

PHP 5.X  

 
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException; 

$USAGE = "\n" .
"To run this example, supply the name of an S3 bucket and a file to\n" .
"upload to it.\n" .
"\n" .
"Ex: php PutObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$file_Path = $argv[2];
$key = basename($argv[2]);


try {

$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));

$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $file_Path,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Path/File_Name]

ex)  php object_upload_aws.php iwinv-bucket /tmp/test.txt

 

 

오브젝트 다운로드

 

PHP 7.X

 
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$USAGE = "\n" .
"To run this example, supply the name of an S3 bucket and object to\n" .
"download from it.\n" .
"\n" .
"Ex: php GetObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$key = $argv[2];

try {
$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);


$result = $s3Client->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'SaveAs' => $key
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Object_Name] 

ex)  php object_upload_aws.php iwinv-bucket test.txt

 

PHP 5.X  

 
<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
$USAGE = "\n" .

"To run this example, supply the name of an S3 bucket and object to\n" .
"download from it.\n" .
"\n" .
"Ex: php GetObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$key = $argv[2];

try {
$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));


$result = $s3Client->getObject(array(
'Bucket' => $bucket,
'Key' => $key,
'SaveAs' => $key
));
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Object_Name] 

ex)  php object_upload_aws.php iwinv-bucket test.txt

 

 

오브젝트 리스트

 

PHP 7.X

<?php

require 'vendor/autoload.php';

$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);


$list = $s3Client->listObjects(Array('Bucket'=>'Bucket_Name'));

$listArray = $list->toArray();

foreach($listArray['Contents'] as $item){
print($item['Key']."\n");

echo "
";

}


?>

 

PHP 5.X  

 
 
<?php

require ‘vendor/autoload.php’;


$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));

$list = $s3Client->listObjects(Array('Bucket'=>'Bucket_Name'));

$listArray = $list->toArray();

foreach($listArray['Contents'] as $item){
print($item['Key']."\n");

echo "
";

}


?>

 

 

 

오브젝트 삭제

 

PHP 7.X

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$USAGE = "\n" .
"To run this example, supply the name of an S3 bucket and object\n" .
"name (key) to delete.\n" .
"\n" .
"Ex: php DeleteObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$key = $argv[2];

try {
$s3Client = new S3Client([
'region' => 'default',
'version' => 'latest',
'credentials' => [
'key' => 'Access_Key',
'secret' => 'Secret_Key',
],
'endpoint' => 'Endpoint'
]);

$result = $s3Client->deleteObject([
'Bucket' => $bucket,
'Key' => $key,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Object_Name] 

ex)  php object_upload_aws.php iwinv-bucket test.txt

 

PHP 5.X  

<?php
require 'vendor/autoload.php';
use Aws\S3\S3Client;
use Aws\Exception\AwsException;

$USAGE = "\n" .
"To run this example, supply the name of an S3 bucket and object\n" .
"name (key) to delete.\n" .
"\n" .
"Ex: php DeleteObject.php \n";

if (count($argv) <= 2) {
echo $USAGE;
exit();
}

$bucket = $argv[1];
$key = $argv[2];

try {
$s3Client = Aws\S3\S3Client::factory(array(

'key' => Access_Key,

'secret' => Secret_Key,

'endpoint' => 'Endpoint',));

$result = $s3Client->deleteObject([
'Bucket' => $bucket,
'Key' => $key,
]);
} catch (S3Exception $e) {
echo $e->getMessage() . "\n";
}

?>

 

# 실행 명령어 

php 파일명.php [Bucket_Name] [Object_Name] 

ex)  php object_upload_aws.php iwinv-bucket test.txt