devtoolboxuk / aws
PHP 库,用于访问 AWS SDK
1.0.7
2019-11-01 12:04 UTC
Requires
- aws/aws-sdk-php: 2.*
Requires (Dev)
README
版本:1.0.0
目录
- 摘要
- 安装
- 使用
- [调用 AWS 服务](#调用 AWS 服务)
- S3
- [调用 S3](#调用 S3)
- [设置 S3 桶](#设置 S3 桶)
- [S3 列对象](#S3 列对象)
- [S3 流对象](#S3 流对象)
- 维护者
摘要
PHP 库,用于连接到 AWS 服务
安装
安装 Composer
$ php -r "readfile('https://composer.php.ac.cn/installer');" | php
安装依赖
$ php composer.phar install
添加到 PHP
require 'vendor/autoload.php';
AWS 凭据
以下代码不会允许将凭据添加到代码中。我们将使用环境变量,如果在 AWS 中,则使用 IAMS。AWS SDK 将默认使用这些凭据,您无需进行配置
Bash
$ export AWS_ACCESS_KEY_ID= $ export AWS_SECRET_ACCESS_KEY= $ export AWS_DEFAULT_REGION=
PowerShell
PS C:\> $Env:AWS_ACCESS_KEY_ID="" PS C:\> $Env:AWS_SECRET_ACCESS_KEY="" PS C:\> $Env:AWS_DEFAULT_REGION=""
调用 AWS 服务
$this->aws = new aws();
S3
调用 S3
$this->s3 = $this->aws->s3();
设置 S3 桶
$this->s3->setBucket('test-bucket');
S3 命令
列对象
S3 列 "文件夹" 中的对象
$list = $this->s3->listObjects('folder'); foreach ($list as $object) { print_r($object['Key']); }
获取对象
从 S3 获取对象 ($s3filename) 并将其保存到本地 $localFilename
$this->s3->getObject($localFilename, $s3filename)
S3 流
打开流包装器
您可以选择
$awsS3 = $this->aws->s3(); $awsS3->setBucket('test-bucket'); $streamWrapper = $awsS3->getStreamWrapper()
####= 通过流下载对象
$awsS3 = $this->aws->s3(); $awsS3->setBucket('test-bucket'); $objectName = 'folder/folder/file.xml'; $chunkCount = $awsS3->countDownloadStreamChunks($objectName); echo sprintf( "\nUsing a stream, there were %d chunks\n", $chunkCount ); // Opens the file for Streaming $awsS3->openDownloadStream($objectName); //outputs the streamed data for($i=0;$i<$chunkCount;$i++) { echo $awsS3->getDownloadStream($i); } // Closes the streamed file $awsS3->closeDownloadStream();
通过流上传对象
即将推出
感谢
@halfer 添加流包装器