오브젝트스토리지

iwinv Object Storage - PHP SDK

 

iwinv에서 제공하는 서비스인 오브젝트 스토리지는 인터넷을 통해 대용량 데이터를 제한없이 저장하거나 안전하게 저장할 수 있는 객체형 스토리지 서비스로,

REST API Secure Token 인증을 통한 AWS S3와 높은 호환성을 가진 서비스입니다. 

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

## 2021.07.07 추가 ##  해당 메뉴얼이 자동으로 누락되는 에러가 발생하고있습니다.  해당 메뉴얼의 코드나 , 내용이 누락된경우 아래 링크를 참고해주시면 감사하겠습니다. 

iwinv Object Storage – PHP SDK -  https://idchowto.com/?p=54679

 

 

참고문서

  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   

## Endpoint 입력시에는  http://kr.object.iwinv.kr  혹은 https://kr.object.iwinv.kr  과 같은 형식으로 입력해주어야 코드가 정상 실행됩니다. 

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 

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 


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 

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 

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

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  

 
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

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 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(array(
'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

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_download_aws.php iwinv-bucket test.txt

 

PHP 5.X  

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_download_aws.php iwinv-bucket test.txt

 

 

오브젝트 리스트

 

PHP 7.X

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'
]);

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

$listArray = $list->toArray();

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

echo “
“;

}

?>

 

PHP 5.X  

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

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_delete_aws.php iwinv-bucket test.txt

 

PHP 5.X  

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_delete_aws.php iwinv-bucket test.txt