+86 13541016684Mon. - Fri. 10:00-22:00

安装AWS SDK for PHP

安装AWS SDK for PHP

安装AWS SDK for PHP

AWS_SDK_for_PHP

AWS SDK for PHP

安装AWS SDK for PHP以后通过调用API可操作Amazon S3的桶(Bucket)和对象(Object)。

安装AWS SDK for PHP

Composer是PHP包管理工具,以项目单位管理包及库。在PHP版本5.3.2以上可使用。

首先安装Composer。

# curl -sS https://getcomposer.org/installer | php

创建composer.json文件。

# vi composer.json
{
    "require": {
        "aws/aws-sdk-php": "2.*"
    }
}

利用刚才创建的json文件安装AWS-SDK-PHP。

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing symfony/event-dispatcher (v2.6.6)
    Downloading: 100%

  - Installing guzzle/guzzle (v3.9.3)
    Downloading: 100%

  - Installing aws/aws-sdk-php (2.8.2)
    Downloading: 100%

symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
guzzle/guzzle suggests installing guzzlehttp/guzzle (Guzzle 5 has moved to a new package name. The package you have installed, Guzzle 3, is deprecated.)
aws/aws-sdk-php suggests installing doctrine/cache (Adds support for caching of credentials and responses)
aws/aws-sdk-php suggests installing ext-apc (Allows service description opcode caching, request and response caching, and credentials caching)
aws/aws-sdk-php suggests installing monolog/monolog (Adds support for logging HTTP requests and responses)
aws/aws-sdk-php suggests installing symfony/yaml (Eases the ability to write manifests for creating jobs in AWS Import/Export)
Writing lock file
Generating autoload files

确认安装结果

创建简单test.php文件,获取S3的桶一览。

[Access Key Id],[Secret Access Key]部分填写root账户或者IAM账户的信息。

# vi test.php
<?php
require_once("vendor/autoload.php");
use Aws\S3\S3Client;

$config = array(
        'key'   => 'Access Key Id',
        'secret'=> 'Secret Access Key',
);

$s3 = S3Client::factory($config);

$buckets = $s3->listBuckets();
echo print_r($buckets,true);

执行test.php文件查看输出结果。

# php ./test.php
Guzzle\Service\Resource\Model Object
(
    [structure:protected] =>
    [data:protected] => Array
        (
            [Owner] => Array
                (
                    [ID] => ddeef914fd5b41b96d5243b0a595f9e906737e20f6fc592be511d9628c0dc63a
                    [DisplayName] => awsgood
                )

            [Buckets] => Array
                (
                    [0] => Array
                        (
                            [Name] => awsgood
                            [CreationDate] => 2015-04-17T08:21:47.000Z
                        )

                    [1] => Array
                        (
                            [Name] => awsgood
                            [CreationDate] => 2015-04-27T05:38:53.000Z
                        )

                )

            [RequestId] => D07361245E11E2A7
        )
)

成功获取了Amazon S3桶(Bucket)一览。