通过PHP SDK 2从S3抓取对象的内容。

12 浏览
0 Comments

通过PHP SDK 2从S3抓取对象的内容。

我一直在尝试找出如何从S3存储桶中获取内容,以便将其包含在一个ZipArchive中,以供客户使用。他们现在需要创建报告,其中包含由客户推送到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.";
}

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

[获取对象](http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Aws.S3.S3Client.html#_getObject)

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)

[返回的Body](http://docs.amazonwebservices.com/aws-sdk-php-2/latest/class-Guzzle.Http.EntityBody.html)

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

任何信息都会非常感激,谢谢!

解决方案:

我花了一些时间才找到解决方法,但我能找到一个指引我正确方向的gist,为了获取文件的内容,你需要做以下操作:

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.";
}

0