doujiaoben28596 2013-09-20 03:27
浏览 50
已采纳

使用AWS SDK PHP v2 upload(),Amazon S3设置元数据失败

I'm having problems setting the "Metadata" option when uploading files to Amazon S3 using the AWS SDK PHP v2. The documentation that I'm reading for the upload() method states that the the 5th parameter is an array of options...

*$options Custom options used when executing commands: - params: Custom parameters to use with the upload. The parameters must map to a PutObject or InitiateMultipartUpload operation parameters. - min_part_size: Minimum size to allow for each uploaded part when performing a multipart upload. - concurrency: Maximum number of concurrent multipart uploads. - before_upload: Callback to invoke before each multipart upload. The callback will receive a Guzzle\Common\Event object with context.*

My upload() code looks like this..

$upload = $client->upload(
  '<BUCKETNAME>',
  'metadatatest.upload.jpg',
  fopen('metadatatest.jpg','r'),
  'public-read',
  array('Metadata' => array(
    'SomeKeyString' => 'SomeValueString'
  ))
);

...and no meta data is set after upload.

If however I use putObject() as documented here, which I assume is a "lower level" method compared to upload()...

$putObject = $client->putObject(
  array(
    'Bucket' => '<BUCKETNAME>',
    'Key' => 'metadatatest.putobject.jpg',
    'Body' => file_get_contents('metadatatest.jpg'),
    'ACL' => 'public-read',
    'Metadata' => array(
      'SomeKeyString' => 'SomeValueString'
    )
  )
);

The meta data is successfully returned when I call getObject() or view the file directly in my browser when uploaded using putObject()

$getObject = $client->getObject(
  array(
    'Bucket' => '<BUCKETNAME>',
    'Key' => 'metadatatest.putobject.jpg'
  )
);

I would prefer to use the $client->upload() method as the documentation states

Upload a file, stream, or string to a bucket. If the upload size exceeds the specified threshold, the upload will be performed using parallel multipart uploads.

I'm not sure what I've missed?

  • 写回答

1条回答 默认 最新

  • doumi7854 2013-09-23 18:04
    关注

    There's really no difference in using upload() or putObject() if you don't do multipart uploads. You can have a look at the AWS PHP SDK source code but basically the upload method just calls putObject like this:

    // Perform a simple PutObject operation
    return $this->putObject(array(
        'Bucket' => $bucket,
        'Key'    => $key,
        'Body'   => $body,
        'ACL'    => $acl
    ) + $options['params']);
    

    This isn't very clear in the SDK documentation, but you need to send the last parameter as an array with the key params and its value being a second array with the Metadata key and value like this:

    $upload = $client->upload(
        '<BUCKETNAME>',
        'metadatatest.upload.jpg',
        fopen('metadatatest.jpg','r'),
        'public-read',
        array('params' => array(
            'Metadata' => array(
                'SomeKeyString' => 'SomeValueString'
        )))
    );
    

    However, I you could just use the putObject call to achieve the same thing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据