AWS PHP SDK Version 2 S3 putObject Error

时光总嘲笑我的痴心妄想 提交于 2019-12-24 15:33:30

问题


So the AWS php sdk 2.x library has been put out recently and I've taken a turkey day plunge into upgrading from 1.5x. My first was to upgrade my S3 backup class. I've quickly run into an error:

Fatal error: Class 'EntityBody' not found in /usr/share/php/....my file here

when trying to upload a zipped file to an S3 bucket. I wrote a class to abstract the writing a bit to allow for multi-region backup, so the code below references to $this are that.

$response1 = $s3->create_object(
        $this->bucket_standard,
        $this->filename,
        array(
         'fileUpload'  => $this->filename,
         'encryption' => 'AES256',
         //'acl'         => AmazonS3::ACL_PRIVATE,
         'contentType' => 'text/plain',
         'storage'     => AmazonS3::STORAGE_REDUCED,
         'headers'     => array( // raw headers
                              'Cache-Control'    => 'max-age',
                              //'Content-Encoding' => 'gzip',
                              'Content-Language' => 'en-US'
                              //'Expires'       => 'Thu, 01 Nov 2012 16:00:00 GMT'
                            ),
          'meta'     => array(
                              'param1' => $this->backupDateTime->format('Y-m-d H:i:s'),     // put some info on the file in meta tags
                              'param2' => $this->hostOrigin
                            ) 
            )
      );

The above worked fine on 1.5.x.

Now, in 2.x, I'm looking into their docs and they've changed just about everything (great...maximum sarcasm)

$s3opts=array('key'=> $this->accessKey, 'secret' => $this->secretKey,'region' => 'us-east-1');
$s3 = Aws\S3\S3Client::factory($s3opts);

so now I've got a new S3 object. And here is my 2.x syntax to do the same exact thing. My problem arises where they've (sinisterly) changed the old "fileupload" to "Body" and made it more abstract in how to actually attach a file! I've tried both and I'm thinking it has to do with the dependencies (Guzzle or Smyfony etc), but I get the error above (or substitute Stream if you like) whenever I try to execute this.

I've tried using Composer with composer.json, and the aws.phar but before I get into that, is there something dumb I'm missing?

$response1 = $s3->putObject(array(
        'Bucket' => $this->bucket_standard,
        'Key'    => $this->filename,
        'ServerSideEncryption' => 'AES256',
        'StorageClass' => 'REDUCED_REDUNDANCY',
        'Body' => EntityBody::factory(fopen($this->filename, 'r')),
        //'Body' => new Stream(fopen($fullPath, 'r')),

        'MetaData' => array(
                              'BackupTime' => $this->backupDateTime->format('Y-m-d H:i:s'),     // put some info on the file in meta tags
                              'HostOrigin' => $this->hostOrigin
                            ) 
      ));

Thanks as always,

R


回答1:


Did you import the EntityBody into your namespace?

use Guzzle\Http\EntityBody;

Otherwise, you'd have to do

'Body' => \Guzzle\Http\EntityBody::factory(fopen($this->filename, 'r')),


来源:https://stackoverflow.com/questions/13504065/aws-php-sdk-version-2-s3-putobject-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!