Retrieve object user metadata in S3 - aws sdk v3 php

你离开我真会死。 提交于 2019-12-07 17:40:42

问题


I'm looking to retrieve user-defined meta-data from objects in my S3 bucket, from the php sdk.

As per Editing Object Meta Data, User metadata is stored with the object and returned with it, and begin with "x-amz-meta-"

I have defined user metadata on objects through the console like "x-amz-meta-test", at both upload time, and adding it after the upload (through web console, not the upload API).

The test metadata is never returned. I always get the same system metadata. That is, I get only the following keys in @metadata:

Folder

statuscode
effectiveUri
headers
   x-amz-id-2
   x-amz-request-id
   date
   x-amz-bucket-region
   content-type
   transfer-encoding
   server

Objects

Key
LastModified
   date
   timezone_type
   timezone
ETag
Size
StorageClass

However, to achieve this in other languages, a simple method call is involved.

Get User Metadata in Android SDK

Get User Metadata in Java SDK

How do I go about accomplishing the same task in the PHP SDK?

Any help would be greatly appreciated :)


回答1:


I had the same issue with v3 AWS SDK for PHP. After some research and testing, I determined I could use headObject:

<?php
    $headers = $s3->headObject(array(
      "Bucket" => $bucket,
      "Key" => $key
    ));

    print_r($headers->toArray());
?>

Example output with System-Defined Metadata and other identifying information REMOVED:

Array
(
/* REMOVED */
    [Metadata] => Array
        (
            [orderdate] => Mon, 31 Aug 2015 19:03:52 +0000
            [color] => green
            [fruit] => apple
            [price] => 99.95
        )
/* REMOVED */
    [@metadata] => Array
        (
            [statusCode] => 200
            [effectiveUri] => https://s3.amazonaws.com/REMOVED/REMOVED
            [headers] => Array
                (
                    [x-amz-id-2] => REMOVED
                    [x-amz-request-id] => REMOVED
                    [date] => Wed, 02 Sep 2015 04:43:02 GMT
                    [x-amz-meta-orderdate] => Mon, 31 Aug 2015 19:03:52 +0000
                    [x-amz-meta-color] => green
                    [x-amz-meta-fruit] => apple
                    [x-amz-meta-price] => 99.95
                    [last-modified] => Wed, 02 Sep 2015 04:11:13 GMT
                    [etag] => "REMOVED"
                    [x-amz-storage-class] => REDUCED_REDUNDANCY
                    [accept-ranges] => bytes
                    [content-type] => application/octet-stream
                    [content-length] => 80771
                    [server] => AmazonS3
                )
        )
)


来源:https://stackoverflow.com/questions/32191946/retrieve-object-user-metadata-in-s3-aws-sdk-v3-php

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