Reading an Object

Reading an object is easy. Simply send an HTTP GET request to the URL that identifies the object you want to read, with the appropriate authentication header if required.

If the object you're fetching is publicly readable, you can read it by simply pasting the URL into your favorite browser, or place a link to the object on a web page for others to click. For developers that want to serve URLs to users without having to make their objects publicly readable, Amazon S3 offers a query-string authentication mechanism for GET operations that enables you to include the request signature in the URL itself rather than in the request header. This enables you to pre-sign a request and then hand it out to your customers. For more information about query-string authentication, go to the Amazon Simple Storage Service Developer Guide.

The following sample code snippets demonstrate retrieving an Amazon S3 object.

Java

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    GetResponse response = conn.get("[bucket-name]", "[key-name]", null);
    
    String value = response.object.data;
    
    Map metadata = response.object.metadata;
    
    List values = (List)metadata.get("title");
    
    String title = (String)values.get(0);

C#

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    GetResponse response = conn.get("[bucket-name]", "[key-name]", null);
    
    string value = response.Object.Data;
    
    SortedList metadata = response.Object.Metadata;
    
    string title = metadata["title"] as string;

Perl

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    my $response = $conn->get("[bucket-name]", "[key-name]");
    
    my $data = $response->object->data;
    
    my $metadata = $response->object->metadata;
    
    my $title = $metadata->{title};

PHP

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    $req =& new HTTP_Request("http://s3.amazonaws.com/[bucket-name]/[key-name]");
    
    $req->setMethod("GET");
    
    setAuthorizationHeader($req);
    
    $req->sendRequest();
    
    
    
    if ($req->getResponseCode() == 200) {
    
        $data = $req->getResponseBody();
    
    } else {
    
        // something bad happened
    
    }

Ruby

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    response = conn.get("[bucket-name]", "[key-name]");
    
    data = response.object.data
    
    metadata = response.object.metadata
    
    title = metadata["title"]

Python

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the name of your bucket and key-name with the name of the object to read.

    response = conn.get("[bucket-name]", "[key-name]");
    
    data = response.object.data
    
    metadata = response.object.metadata
    
    title = metadata["title"]

HTTP

The following is an example of an HTTP request.

# get object request



GET /[bucket-name]/[key-name] HTTP/1.0

Date: Wed, 08 Mar 2006 04:06:18 GMT

Authorization: AWS [aws-access-key-id]:[header-signature]

Host: s3.amazonaws.com



# get object response



HTTP/1.1 200 OK

x-amz-id-2: FbGpiykb9oJEdJd0bcfwkL6S3lc06X0y7XSeA/GWyRdvlNEZ0irthljxKoeGFfB6

x-amz-request-id: 9298531013923634

Date: Wed, 08 Mar 2006 04:06:18 GMT

Last-Modified: Wed, 08 Mar 2006 04:06:16 GMT

ETag: "54b0c58c7ce9f2a8b551351102ee0938"

x-amz-meta-title: my title

Content-Type: text/plain

Content-Length: 14

Connection: keep-alive

Server: AmazonS3



this is a test