建立專案
建立專案。
1 2
| mkdir aws-s3-php-example cd aws-s3-php-example
|
安裝依賴套件。
1
| composer require aws/aws-sdk-php
|
實作
建立 index.php
檔,初始化一個客戶端實體。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php
require './vendor/autoload.php';
use Aws\Sdk;
$sharedConfig = [ 'region' => 'ap-northeast-1', ];
$sdk = new Sdk($sharedConfig);
$s3 = $sdk->createS3();
|
列出所有儲存貯體
1 2 3 4 5
| $result = $s3->listBuckets();
foreach ($result['Buckets'] as $bucket) { echo $bucket['Name'] . "\n"; }
|
上傳檔案
1 2 3 4 5 6 7 8 9
| $bucketName = 'your-bucket'; $filePath = './test.txt'; $objectName = 'test.txt';
$s3->putObject([ 'Bucket' => $bucketName, 'Key' => $objectName, 'Body' => fopen($filePath, 'r'), ]);
|
上傳檔案
1 2 3 4 5 6 7
| $bucketName = 'your-bucket';
$result = $s3->listObjects(['Bucket' => $bucketName]);
foreach ($result['Contents'] as $object) { echo $object['Key'] . "\n"; }
|
執行程式。
1
| aws-vault exec your-profile -- php index.php
|
程式碼