ericnorris / amazon-s3-php
一个轻量且快速的PHP S3客户端。
This package is auto-updated.
Last update: 2024-09-27 09:32:46 UTC
README
受tpyo/amazon-s3-php-class启发,这是一个简单且可配置的S3 PHP库。它被编写得尽可能轻量,同时仍能访问AWS的所有功能(例如服务器端加密)。
此外,在执行批量操作时,使用curl_multi_exec(而不是curl_exec)以提高性能。
用法
$client = new S3($access_key, $secret_key [, $endpoint = null]);
配置
指定自定义Curl选项
$client->useCurlOpts($curl_opts_array)
为S3类提供在请求中使用任何curl选项。
以下选项默认传递,以防止'挂起'请求
curl_opts = array( CURLOPT_CONNECTTIMEOUT => 30, CURLOPT_LOW_SPEED_LIMIT => 1, CURLOPT_LOW_SPEED_TIME => 30 );
注意: 如果您调用此方法,则不会使用这些默认值。
发送额外的AWS头信息
所有可用的S3操作都接受一个可选的$headers数组,该数组将被传递到S3。这可以包括x-amz-meta-、x-amz-server-side-encryption、Content-Type等。任何指定的Amazon头信息都将根据AWS签名v2正确包含在AWS签名中。
所有请求的共同请求头信息位于此处。
S3Response类
S3类中的所有方法都将返回S3Response类的实例。
class S3Response { public $error; // null if no error public $code; // response code from AWS public $headers; // response headers from AWS public $body; // response body from AWS }
如果curl中出现错误或AWS返回错误,则$response->error将非空并设置为以下数组
$error = array(
'code' => xxx, // error code from either curl or AWS
'message' => xxx, // error string from either curl or AWS
'resource' => [optional] // the S3 resource frmo the request
)
方法
putObject($bucket, $path, $file [, $headers = array()])
- 将文件上传到指定的路径和存储桶。
$file可以是文件的原始表示(例如,file_get_contents()的结果)或有效的流资源。 - AWS文档
getObjectInfo($bucket, $path, [, $headers = array()])
- 检索对象的元数据。
- AWS文档
getObject($bucket, $path [, $resource = null [, $headers = array()]])
- 检索对象的全部内容。如果
$resource是有效的流资源,内容将被写入流。否则,$response->body将包含文件的内容。 - AWS文档
deleteObject($bucket, $path [, $headers = array()])
- 从S3中删除对象。
- AWS文档
getBucket($bucket [, $headers = array()])
- 返回S3中指定存储桶内容的解析响应。
- AWS文档
示例
实例化S3类
$client = new S3(ACCESS_KEY, SECRET_KEY); // [OPTIONAL] Specify different curl options $client->useCurlOpts(array( CURLOPT_MAX_RECV_SPEED_LARGE => 1048576, CURLOPT_CONNECTTIMEOUT => 10 ));
上传对象
$response = $client->putObject(
'bucket',
'hello_world.txt',
'hello world!',
array(
'Content-Type' => 'text/plain'
)
);
print_r($response);
输出
S3Response Object
(
[error] => null
[code] => 200
[headers] => Array
(
[x-amz-id-2] => ...
[x-amz-request-id] => ...
[ETag] => "..."
[Content-Length] => ...
[Server] => ...
)
[body] => null
)
下载对象
$resource = tmpfile(); $client->getObject('bucket', 'hello_world.txt', $resource); print_r($response); echo stream_get_contents($resource) . "\n";
输出
S3Response Object
(
[error] =>
[code] => 200
[headers] => Array
(
[x-amz-id-2] => ...
[x-amz-request-id] => ...
[ETag] => "..."
[Accept-Ranges] => bytes
[Content-Type] => text/plain
[Content-Length] => 12
[Server] => ...
)
[body] => Resource id #17
)
hello world!