

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用第 3 適用於 PHP 的 AWS SDK 版建立簡單的應用程式
<a name="hello"></a>

使用 向 Amazon S3 打招呼 適用於 PHP 的 AWS SDK。下列範例顯示 Amazon S3 儲存貯體的清單。

## 先決條件
<a name="hello-prerequisites"></a>
+  [下載並安裝 SDK](getting-started_installation.md) 
+ 使用 之前 適用於 PHP 的 AWS SDK，您必須使用 設定身分驗證 AWS。如需設定身分驗證的資訊，請參閱 [AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)

## 在您的程式碼中包含 SDK
<a name="including-the-sdk-in-your-code"></a>

無論您使用何種技術來安裝開發套件，您只需使用單一 `require` 陳述式在程式碼中加入開發套件。請參閱下表中來找到最適用於您的安裝技術之 PHP 程式碼。以系統上的實際路徑來替換任何 `/path/to/` 的執行個體。


****  

| 安裝技術 | 需要陳述式 | 
| --- | --- | 
|  使用 Composer  |   `require '/path/to/vendor/autoload.php';`   | 
|  使用 phar  |   `require '/path/to/aws.phar';`   | 
|  使用 ZIP  |   `require '/path/to/aws-autoloader.php';`   | 

在本主題中，我們採用 Composer 安裝方法。若您正在使用不同的安裝方法，您可以重新參考本節來尋找要使用的正確 `require` 程式碼。

## 撰寫程式碼
<a name="sdk-hello-world-code"></a>

確定您可以進行身分驗證。

將下列程式碼複製並貼到新的來源檔案中。儲存並命名檔案 `hello-s3.php`。

```
<?php

require 'vendor/autoload.php';

use Aws\S3\S3Client;

/**
 * List your Amazon S3 buckets.
 */

//Create a S3Client
// snippet-start:[s3.php.list_buckets.main]
$s3Client = new S3Client([
    'profile' => 'default',
    'region' => 'us-west-2',
    'version' => '2006-03-01'
]);

//Listing all S3 Bucket
$buckets = $s3Client->listBuckets();
foreach ($buckets['Buckets'] as $bucket) {
    echo $bucket['Name'] . "\n";
}
```

## 執行程式
<a name="sdk-hello-world-running"></a>

開啟命令提示以執行 PHP 程式。執行 PHP 程式的典型命令語法為：

```
php [source filename] [arguments...]
```

此範例程式碼不使用引數。若要執行此程式碼，請在命令提示中輸入以下內容：

```
$ php hello-s3.php
```

## 後續步驟
<a name="sdk-hello-world-next-steps"></a>

若要測試許多其他 Amazon S3 操作，請查看 GitHub 上的[AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/php/example_code/s3)。