keekinc/s3-client

基于支持V2签名的S3 Client的分支

1.0.11 2016-06-29 16:13 UTC

This package is not auto-updated.

Last update: 2024-09-28 11:10:02 UTC


README

为PHP 5.3或更高版本提供的轻量级Amazon S3连接器实现

特别感谢: akeeba/s3 为原始分支所作的贡献。

在与亚马逊的基于Guzzle的AWS SDK遇到大量难以调试的问题后,我们决定为Amazon S3创建自己的连接器。这绝对不是一个完整的实现,而只是S3所需功能的一个小子集。设计目标是简洁和低内存占用。

此代码基于Donovan Schonknecht编写的S3.php,可在 http://undesigned.org.za/2007/10/22/amazon-s3-php-class 下以BSD类似许可证获得。此存储库不再反映原始作者的工作,不应与之一致。

此软件根据GNU通用公共许可证版本3分发,或者根据您的选择,由自由软件基金会(FSF)发布的任何后续版本。简而言之,它是“GPLv3+”。

使用连接器

获取连接器对象

$configuration = new Configuration(
	'YourAmazonAccessKey',
	'YourAmazonSecretKey'
);

$connector = new Connector($configuration);

列出存储桶

$listing = $connector->listBuckets(true);

返回类似此的数组

array(2) {
  'owner' =>
  array(2) {
    'id' =>
    string(64) "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
    'name' =>
    string(8) "someUserName"
  }
  'buckets' =>
  array(3) {
    [0] =>
    array(2) {
      'name' =>
      string(10) "mybucket"
      'time' =>
      int(1267730711)
    }
    [1] =>
    array(2) {
      'name' =>
      string(10) "anotherbucket"
      'time' =>
      int(1269516249)
    }
    [2] =>
    array(2) {
      'name' =>
      string(11) "differentbucket"
      'time' =>
      int(1354458048)
    }
  }
}

列出存储桶内容

$listing = $connector->getBucket('mybucket', 'path/to/list/');

如果您要列出“子目录”,则需要执行以下操作

$listing = $connector->getBucket('mybucket', 'path/to/list/', null, null, '/', true);

最后一个参数(公共前缀)控制“子目录”的列出

上传(小型)文件

从文件

$input = Input::createFromFile($sourceFile);
$connector->putObject($input, 'mybucket', 'path/to/myfile.txt');

从字符串

$input = Input::createFromData($sourceString);
$connector->putObject($input, 'mybucket', 'path/to/myfile.txt');

从流资源

$input = Input::createFromResource($streamHandle, false);
$connector->putObject($input, 'mybucket', 'path/to/myfile.txt');

在所有情况下,整个文件都必须加载到内存中。

使用分块上传上传大文件

文件以5MB块上传。

$input = Input::createFromFile($sourceFile);
$uploadId = $connector->startMultipart($input, 'mybucket', 'mypath/movie.mov');

$eTags = array();
$eTag = null;
$partNumber = 0;

do
{
	// IMPORTANT: You MUST create the input afresh before each uploadMultipart call
	$input = Input::createFromFile($sourceFile);
	$input->setUploadID($uploadId);
	$input->setPartNumber(++$partNumber);

	$eTag = $connector->uploadMultipart($input, 'mybucket', 'mypath/movie.mov');

	if (!is_null($eTag))
	{
		$eTags[] = $eTag;
	}
}
while (!is_null($eTag));

// IMPORTANT: You MUST create the input afresh before finalising the multipart upload
$input = Input::createFromFile($sourceFile);
$input->setUploadID($uploadId);
$input->setEtags($eTags);

$connector->finalizeMultipart($input, 'mybucket', 'mypath/movie.mov');

只要您跟踪UploadId、PartNumber和ETags,就可以在每个单独的页面加载中调用每个上传Multipart调用,以防止超时。

获取预签名URL

允许浏览器直接下载文件,而无需暴露您的凭据,也不需要通过您的服务器

$preSignedURL = $connector->getAuthenticatedURL('mybucket', 'path/to/file.jpg', 60);

最后一个参数控制此URL在未来的多少秒内有效。

下载

到绝对路径 $targetFile 的文件

$connector->getObject('mybucket', 'path/to/file.jpg', $targetFile);

到字符串

$content = $connector->getObject('mybucket', 'path/to/file.jpg', false);

删除对象

$connector->deleteObject('mybucket', 'path/to/file.jpg');