18

我一直在试图弄清楚如何从 S3 存储桶中获取内容以包含在 ZipArchive 中,以供在 S3 上存储文件的客户使用,他们现在需要创建包含客户推送到 S3 的文件的报告。我使用 PHP SDK 2 API(与 PEAR 一起安装)尝试了以下操作:

require 'AWSSDKforPHP/aws.phar';

use Aws\S3\S3Client;
use Aws\Common\Enum\Region;

$config = array(
    'key'    => 'the-aws-key',
    'secret' => 'the-aws-secret',
    'region' => Region::US_EAST_1
);

$aws_s3 = S3Client::factory($config);
$app_config['s3']['bucket'] = 'the-aws-bucket';
$app_config['s3']['prefix'] = '';
$attach_name = 'hosted-test-file.jpg';
try {
    $result = $aws_s3->getObject(
        array(
            'Bucket' => $app_config['s3']['bucket'],
            'Key' => $app_config['s3']['prefix'].$attach_name
        )
    );
    var_dump($result);
    $body = $result->get('Body');
    var_dump($body);
    $handle = fopen('php://temp', 'r');
    $content = stream_get_contents($handle);
    echo "String length: ".strlen($content);
} catch(Aws\S3\Exception\S3Exception $e) {
    echo "Request failed.<br />";
}

但是,它返回的只是一个Guzzle\Http\EntityBody对象,不确定如何获取实际内容,以便将其推送到 zip 文件中。

抓取物体

object(Guzzle\Service\Resource\Model)[126]
    protected 'structure' => object(Guzzle\Service\Description\Parameter)[109]
    protected 'name' => null
    protected 'description' => null
    protected 'type' => string 'object' (length = 6)
    protected 'required' => boolean false
    protected 'enum' => null
    protected 'additionalProperties' => boolean true
    protected 'items' => null
    protected 'parent' => null
    protected 'ref' => null
    protected 'format' => null
    protected 'data' => array (size = 11)
        'Body' => object(Guzzle\Http\EntityBody)[97]
            protected 'contentEncoding' => boolean false
            protected 'rewindFunction' => null
            protected 'stream' => resource(292, stream)
            protected 'size' => int 3078337
            protected 'cache' => array (size = 9)
            ...
        'DeleteMarker' => string '' (length = 0)
        'Expiration' => string '' (length = 0)
        'WebsiteRedirectLocation' => string '' (length = 0)
        'LastModified' => string 'Fri, 30 Nov 2012 21:07:30 GMT' (length = 29)
        'ContentType' => string 'binary/octet-stream' (length = 19)
        'ContentLength' => string '3078337' (length = 7)
        'ETag' => string '"the-etag-of-the-file"' (length = 34)
        'ServerSideEncryption' => string '' (length = 0)
        'VersionId' => string '' (length = 0)
        'RequestId' => string 'request-id' (length = 16)

从身体返回

object(Guzzle\Http\EntityBody)[96]
    protected 'contentEncoding' => boolean false
    protected 'rewindFunction' => null
    protected 'stream' => resource(292, stream)
    protected 'size' => int 3078337
    protected 'cache' => array (size = 9)
        'wrapper_type' => string 'php' (length = 3)
        'stream_type' => string 'temp' (length = 4)
        'mode' => string 'w+b' (length = 3)
        'unread_bytes' => int 0
        'seekable' => boolean true
        'uri' => string 'php://temp' (length = 10)
        'is_local' => boolean true
        'is_readable' => boolean true
        'is_writable' => boolean true

// Echo of strlen()
String length: 0

任何信息将不胜感激,谢谢!

解决方案

我花了一段时间才弄清楚,但我找到了一个指向正确方向的要点,为了获取文件的内容,您需要执行以下操作:

require 'AWSSDKforPHP/aws.phar';

use Aws\S3\S3Client;
use Aws\Common\Enum\Region;

$config = array(
    'key'    => 'the-aws-key',
    'secret' => 'the-aws-secret',
    'region' => Region::US_EAST_1
);

$aws_s3 = S3Client::factory($config);
$app_config['s3']['bucket'] = 'the-aws-bucket';
$app_config['s3']['prefix'] = '';
$attach_name = 'hosted-test-file.jpg';
try {
    $result = $aws_s3->getObject(
        array(
            'Bucket' => $app_config['s3']['bucket'],
            'Key' => $app_config['s3']['prefix'].$attach_name
        )
    );
    $body = $result->get('Body');
    $body->rewind();
    $content = $body->read($result['ContentLength']);
} catch(Aws\S3\Exception\S3Exception $e) {
    echo "Request failed.<br />";
}
4

5 回答 5

32

响应的主体存储在一个Guzzle\Http\EntityBody对象中。这用于保护您的应用程序免于下载非常大的文件和内存不足。

如果需要将 EntityBody 对象的内容用作字符串,可以将对象强制转换为字符串:

$result = $s3Client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $key
));

// Cast as a string
$bodyAsString = (string) $result['Body'];

// or call __toString directly
$bodyAsString = $result['Body']->__toString();

如果需要,您也可以直接下载到目标文件:

use Guzzle\Http\EntityBody;

$s3Client->getObject(array(
  'Bucket' => $bucket,
  'Key'    => $key,
  'command.response_body' => EntityBody::factory(fopen("/tmp/{$key}", 'w+'))
));
于 2012-12-05T18:54:47.097 回答
4

这对我有用:

$tempSave = 'path/to/save/filename.txt';

return $this->S3->getObject([
     'Bucket' => 'test',
     'Key'    => $keyName,
     'SaveAs' => $tempSave,
]);
于 2018-08-09T05:55:49.630 回答
0

调用时getObject,您可以传入一个选项数组。在这些选项中,您可以指定是否要将对象下载到文件系统。

$bucket = "bucketName";
$file = "fileName";
$downloadTo = "path/to/save";

$opts = array(  // array of options
    'fileDownload' => $downloadTo . $file   // tells the SDK to download the 
                                             // file to this location
);

$result = $aws_s3->getObject($bucket, $file, $opts);

获取对象参考

于 2012-12-03T15:32:48.933 回答
0

我对 2.00 SDK 版本不是很熟悉,但看起来您已经在php://temp. 通过查看您更新的问题并简要浏览文档,该流似乎可以作为:

$result = $aws_s3->getObject(
    array(
        'Bucket' => $app_config['s3']['bucket'],
        'Key' => $app_config['s3']['prefix'].$attach_name
    )
);
$stream = $result->get('stream');
$content = file_get_contents($stream);
于 2012-12-03T16:21:30.780 回答
-1
<?php
   $o_iter = $client->getIterator('ListObjects', array(
    'Bucket' => $bucketname
   ));
   foreach ($o_iter as $o) {
    echo "{$o['Key']}\t{$o['Size']}\t{$o['LastModified']}\n";
   }
于 2014-05-24T18:36:36.027 回答