if_object_exists ( $bucket, $filename )

Gets whether or not the specified Amazon S3 object exists in the specified bucket.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket to use.

$filename

string

Required

The file name for the object.

Returns

Type

Description

boolean

A value of true if the object exists, or a value of false if it does not.

Examples

Check to see if an object exists.

Note: This does not check whether you can read the object — only if it exists.

// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket' . strtolower($s3->key);

$response = $s3->if_object_exists($bucket, 'test1.txt');

// Success? (Boolean, not a CFResponse object)
var_dump($response);
Result:
bool(true)

Source

Method defined in services/s3.class.php | Toggle source view (16 lines) | View on GitHub

public function if_object_exists($bucket, $filename)
{
    if ($this->use_batch_flow)
    {
        throw new S3_Exception(__FUNCTION__ . '() cannot be batch requested');
    }

    $header = $this->get_object_headers($bucket, $filename);

    if ($header->isOK()) { return true; }
    elseif ($header->status === 404) { return false; }

    // @codeCoverageIgnoreStart
    return null;
    // @codeCoverageIgnoreEnd
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback