get_bucket_filesize ( $bucket, $friendly_format )

Gets the cumulative file size of the contents of the Amazon S3 bucket.

Access

public

Parameters

Parameter

Type

Required

Description

$bucket

string

Required

The name of the bucket to use.

$friendly_format

boolean

Optional

A value of true will format the return value to 2 decimal points using the largest possible unit (i.e., 3.42 GB). A value of false will format the return value as the raw number of bytes.

Returns

Type

Description

integer
string

The number of bytes as an integer, or the friendly format as a string.

Examples

Get the size of the contents of a bucket, in bytes.

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

$response = $s3->get_bucket_filesize($bucket);

// Success?
var_dump($response);

Get the size of the contents of a bucket, measured in the largest reasonable unit.

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

$response = $s3->get_bucket_filesize($bucket, true);

// Success?
var_dump($response);

Source

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

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

    $filesize = 0;
    $list = $this->list_objects($bucket);

    foreach ($list->body->Contents as $filename)
    {
        $filesize += (integer) $filename->Size;
    }

    while ((string) $list->body->IsTruncated === 'true')
    {
        $body = (array) $list->body;
        $list = $this->list_objects($bucket, array(
            'marker' => (string) end($body['Contents'])->Key
        ));

        foreach ($list->body->Contents as $object)
        {
            $filesize += (integer) $object->Size;
        }
    }

    if ($friendly_format)
    {
        $filesize = $this->util->size_readable($filesize);
    }

    return $filesize;
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback