简体   繁体   English

通过PHP SDK 2从S3中获取Object的内容?

[英]Grabbing contents of Object from S3 via PHP SDK 2?

I have been trying to figure out how to grab contents from an S3 bucket to include in a ZipArchive for a client who is storing files on S3, they now need to create reports that hold the files that were pushed up to S3 by their customers. 我一直试图弄清楚如何从S3存储桶中获取内容以包含在ZipArchive中,以便在S3上存储文件的客户端,他们现在需要创建报告来保存由客户推送到S3的文件。 I have tried the following with the PHP SDK 2 API (Installed with PEAR): 我已经尝试了以下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 />";
}

However, all it returns is an Guzzle\\Http\\EntityBody object, not sure how to grab the actual content so I can push it into the zip file. 但是,它返回的只是一个Guzzle\\Http\\EntityBody对象,不知道如何抓取实际内容,所以我可以把它推入zip文件。

Grabbing Object 抓住对象

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)

Returned from Body 从身体归来

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

Any information would be high appreciated, thanks! 任何信息都将受到高度赞赏,谢谢!

Solution

It me a while to figure it out but I was able to find a gist that pointed me in the right direction, in order to get the contents of the file you need to do the following: 我想了解它,但我找到了一个指向正确方向的要点,以获取您需要执行以下操作的文件内容:

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

The body of the response is stored in a Guzzle\\Http\\EntityBody object. 响应的主体存储在Guzzle\\Http\\EntityBody对象中。 This is used to protect your application from downloading extremely large files and running out of memory. 这用于保护您的应用程序免于下载超大文件和内存不足。

If you need to use the contents of the the EntityBody object as a string, you can cast the object to a string: 如果需要将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();

You can also download directly to the target file if needed: 如果需要,您还可以直接下载到目标文件:

use Guzzle\Http\EntityBody;

$s3Client->getObject(array(
  'Bucket' => $bucket,
  'Key'    => $key,
  'command.response_body' => EntityBody::factory(fopen("/tmp/{$key}", 'w+'))
));

When calling getObject , you can pass in an array of options. 调用getObject ,可以传入一组选项。 In these options, you can specify if you want to download the object to your file system. 在这些选项中,您可以指定是否要将对象下载到文件系统。

$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);

getObject Reference getObject参考

I am not that familiar with the version 2.00 SDK, but it looks like you have been passed a stream context on php://temp . 我对版本2.00 SDK并不熟悉,但看起来你已经在php://temp上传递了一个流上下文。 From looking at your updated question and from a brief glance at the documentation, it seems the stream may be available as: 从查看您更新的问题和简要浏览文档,似乎可以使用以下流:

$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);
<?php
   $o_iter = $client->getIterator('ListObjects', array(
    'Bucket' => $bucketname
   ));
   foreach ($o_iter as $o) {
    echo "{$o['Key']}\t{$o['Size']}\t{$o['LastModified']}\n";
   }

This worked for me: 这对我有用:

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

return $this->S3->getObject([
     'Bucket' => 'test',
     'Key'    => $keyName,
     'SaveAs' => $tempSave,
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM