8

Is it possible to get just the objects custom metadata from S3 without having to get the whole object? I've looked through the AWS SDK PHP 2 and searched google and SO with no clear answer, or maybe just not the answer I'm hoping for.

Thanks.

3
  • 1
    Any specific language you're looking for? I could throw a sample together if I knew how you wanted to do it. GetObjectMetaData would do exactly that. Commented Jul 18, 2013 at 17:56
  • Well that would make sense. I didn't see it listed for PHP on this page docs.aws.amazon.com/aws-sdk-php-2/guide/latest/service-s3.html . We usually use Java but because of the environment we are in we are using PHP. Commented Jul 18, 2013 at 18:18
  • It stinks that the list functions don't return the metadata. I need the metadata for all the objects in my bucket. After I call the list function, seems like I need to loop through every object and make a separate call to get the metadata. That's a lot of calls. Commented Jul 11, 2018 at 1:46

2 Answers 2

6

Maybe this would help for PHP 2? It uses the Guzzle framework which I'm not familiar with.

Executes a HeadObject command: The HEAD operation retrieves metadata from an object without returning the object itself. This operation is useful if you're only interested in an object's metadata. To use HEAD, you must have READ access to the object.

Final attempt using Guzzle framework (untested code):

use Guzzle\Service\Resource\Model use Aws\Common\Enum\Region; use Aws\S3\S3Client; $client = S3Client::factory(array( "key" => "YOUR ACCESS KEY ID", "secret" => "YOUR SECRET ACCESS KEY", "region" => Region::US_EAST_1, "scheme" => "http", )); // HEAD object $headers = $client->headObject(array( "Bucket" => "your-bucket", "Key" => "your-key" )); print_r($headers->toArray()); 

PHP 1.6.2 Solution

// Instantiate the class $s3 = new AmazonS3(); $bucket = 'my-bucket' . strtolower($s3->key); $response = $s3->get_object_metadata($bucket, 'üpløåd/î\'vé nøw béén üpløådéd.txt'); // Success? var_dump($response['ContentType']); var_dump($response['Headers']['content-language']); var_dump($response['Headers']['x-amz-meta-ice-ice-baby']); 

Credit to: http://docs.aws.amazon.com/AWSSDKforPHP/latest/#m=AmazonS3/get_object_metadata

Hope that helps!

Sign up to request clarification or add additional context in comments.

6 Comments

Looking for a solution with the AWS SDK PHP 2. Seems strange they would remove this from the latest SDK.
Yea I'm sorry! I'm trying to come up with something, but it looks like the docs for PHP 2 are a lot more convoluted than the previous version. I've edited my answer to add something that may help, however I don't know how to implement it.
So I'm not the only one the sees the PHP 2 docs are pretty bad :). Glad I'm not the only one. Really strange they would remove, or appear to remove that call. Really ideally we would like to be able to receive our custom meta data with the list, but I don't think that is possible with any SDK.
This is the correct answer, if you hit the "this" link he provided and scroll down, it's explained that metadata is returned as an array: Metadata - (hash[string => string])
Thanks David for pointing that out. Correct answer to link to doc :).
|
1

AWS HEAD Object http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html

use Aws\S3\S3Client; use Guzzle\Common\Collection; $client = S3Client::factory(array( 'key' => 'YOUR-AWS-KEY', 'secret' => 'YOUR-SECRET-KEY' )); // Use Guzzle's toArray() method. $result = $client->headObject(['Bucket' => 'YOUR-BUCKET-NAME', 'Key' => 'YOUR-FILE-NAME'])->toArray(); print_r($result['Metadata']); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.