Hướng dẫn aws sdk-php

Bắt đầu sử dụng nhanh chóng AWS với AWS SDK cho PHP. SDK là thư viện PHP nguồn mở hiện đại giúp bạn dễ dàng tích hợp ứng dụng PHP với các dịch vụ AWS như Amazon S3, Amazon Glacier và Amazon DynamoDB.

Các API tài nguyên của AWS cung cấp một phần rút gọn tập trung vào đối tượng của giao diện "cấp thấp" tức giao diện kiểu RPC trong AWS SDK cho PHP, để đem lại trải nghiệm viết mã trực quan hơn. Đối tượng tài nguyên là tham chiếu đến tài nguyên AWS (ví dụ như phiên bản Amazon EC2 hoặc đối tượng Amazon S3) tiết lộ các thuộc tính của tài nguyên và thao tác theo thuộc tính và phương pháp của đối tượng tài nguyên. Thông tin chi tiết của yêu cầu API HTTP ngầm trở nên rõ ràng và bạn sẽ được làm việc với tài nguyên AWS như thể đó là các đối tượng PHP cục bộ. Đoạn mã mẫu bên dưới sẽ minh họa cách thức hoạt động của quá trình này. Các dịch vụ được hỗ trợ gồm có Amazon EC2, Amazon S3, Amazon SNS, Amazon SQS, AWS IAM, Amazon Glacier và AWS CloudFormation, với thêm nhiều dịch vụ khác sẽ được bổ sung trong tương lai.

// Đoạn mã mẫu bên dưới sẽ minh họa cách thức hoạt động của các API tài nguyên

$aws = new Aws($config);

// Lấy tham chiếu đến đối tượng tài nguyên

$bucket = $aws->s3->bucket('my-bucket');

$object = $bucket->object('image/bird.jpg');

// Truy cập thuộc tính của tài nguyên

echo $object['LastModified'];

// Ra lệnh cho phương pháp của tài nguyên thực hiện thao tác

$object->delete();

$bucket->delete();

Bạn muốn tìm các phiên bản cũ của AWS SDK cho PHP?

AWS sẽ ngừng hỗ trợ cho Internet Explorer vào 07/31/2022. Các trình duyệt được hỗ trợ là Chrome, Firefox, Edge và Safari. Tìm hiểu thêm »

You can install the AWS SDK for PHP Version 3:

  • As a dependency via Composer

  • As a prepackaged phar of the SDK

  • As a ZIP file of the SDK

Before you install AWS SDK for PHP Version 3 ensure your environment is using PHP version 5.5 or later. Learn more about environment requirements and recommendations.

Install AWS SDK for PHP as a dependency via Composer

Composer is the recommended way to install the AWS SDK for PHP. Composer is a tool for PHP that manages and installs the dependencies of your project.

For more information on how to install Composer, configure autoloading, and follow other best practices for defining dependencies, see getcomposer.org.

Install Composer

If Composer is not already in your project, download and install Composer.

For Windows, download and run the Composer-Setup.exe.

For Linux, follow the Command-line installation on the Download Composer page.

Add AWS SDK for PHP as a dependency via Composer

If Composer is already installed globally on your system, run the following in the base directory of your project to install AWS SDK for PHP as a dependency:

composer require aws/aws-sdk-php

Otherwise type this Composer command to install the latest version of the AWS SDK for PHP as a dependency.

php -d memory_limit=-1 composer.phar require aws/aws-sdk-php

Add autoloader to your php scripts

To utilize the AWS SDK for PHP in your scripts, include the autoloader in your scripts, as follows.

<?php
   require '/path/to/vendor/autoload.php';
?>

Installing by using the packaged phar

Each release of the AWS SDK for PHP includes a prepackaged phar (PHP archive) that contains all the classes and dependencies you need to run the SDK. Additionally, the phar automatically registers a class autoloader for the AWS SDK for PHP and all its dependencies.

You can download the packaged phar and include it in your scripts.

<?php
   require '/path/to/aws.phar';
?>

Using PHP with the Suhosin patch is not recommended, but is common on Ubuntu and Debian distributions. In this case, you might need to enable the use of phars in the suhosin.ini. If you don’t do this, including a phar file in your code will cause a silent failure. To modify suhosin.ini, add the following line.

suhosin.executor.include.whitelist = phar

Installing by using the ZIP file

The AWS SDK for PHP includes a ZIP file containing all the classes and dependencies you need to run the SDK. Additionally, the ZIP file includes a class autoloader for the AWS SDK for PHP and its dependencies.

To install the SDK, download the .zip file, and then extract it into your project at a location you choose. Then include the autoloader in your scripts, as follows.

<?php
   require '/path/to/aws-autoloader.php';
?>