Scalr\Service\Aws\S3\V20060301\S3Api::listObjects PHP Method

listObjects() public method

This implementation of the GET operation returns some or all (up to 1000) of the objects in a bucket. You can use the request parameters as selection criteria to return a subset of the objects in a bucket. To use this implementation of the operation, you must have READ access to the bucket.
public listObjects ( string $bucketName, string $delimiter = null, string $marker = null, string $maxKeys = null, string $prefix = null ) : Scalr\Service\Aws\S3\DataType\ObjectList
$bucketName string A bucket Name.
$delimiter string optional A delimiter is a character you use to group keys. All keys that contain the same string between the prefix, if specified, and the first occurrence of the delimiter after the prefix are grouped under a single result element, CommonPrefixes. If you don't specify the prefix parameter, then the substring starts at the beginning of the key. The keys that are grouped under CommonPrefixes result element are not returned elsewhere in the response.
$marker string optional Specifies the key to start with when listing objects in a bucket. Amazon S3 lists objects in alphabetical order.
$maxKeys string optional Sets the maximum number of keys returned in the response body. The response might contain fewer keys but will never contain more. If there are additional keys that satisfy the search criteria but were not returned because max-keys was exceeded, the response contains true.To return the additional keys.
$prefix string optional Limits the response to keys that begin with the specified prefix. You can use prefixes to separate a bucket into different groupings of keys. (You can think of using prefix to make groups in the same way you'd use a folder in a file system.)
return Scalr\Service\Aws\S3\DataType\ObjectList Returns list of Objects
    public function listObjects($bucketName, $delimiter = null, $marker = null, $maxKeys = null, $prefix = null)
    {
        $result = null;
        $options = array('_subdomain' => (string) $bucketName);
        $aQueryString = array();
        if ($delimiter !== null) {
            $aQueryString[] = 'delimiter=' . self::escape($delimiter);
        }
        if ($marker !== null) {
            $aQueryString[] = 'marker=' . self::escape($marker);
        }
        if ($maxKeys !== null) {
            $aQueryString[] = 'max-keys=' . self::escape($maxKeys);
        }
        if ($prefix !== null) {
            $aQueryString[] = 'prefix=' . self::escape($prefix);
        }
        $response = $this->client->call('GET', $options, '/' . (!empty($aQueryString) ? '?' . join('&', $aQueryString) : ''));
        if ($response->getError() === false) {
            //Success
            $em = $this->getEntityManager();
            $sxml = simplexml_load_string($response->getRawContent());
            $result = new ObjectList();
            $result->setS3($this->s3);
            $result->setBucketName((string) $sxml->Name);
            if (!empty($sxml->Marker)) {
                $result->setMarker((string) $sxml->Marker);
            }
            if (!empty($sxml->Contents)) {
                foreach ($sxml->Contents as $v) {
                    $objectName = (string) $v->Key;
                    //Tries to find object in storage
                    $object = $this->s3->object->get(array((string) $bucketName, $objectName));
                    if ($object instanceof ObjectData) {
                        //Resets object's properties
                        $object->resetObject();
                    } else {
                        //Creates a new one.
                        $object = new ObjectData();
                        $na = true;
                    }
                    $ownerid = (string) $v->Owner->ID;
                    if (!empty($ownerid)) {
                        $owner = $em->getRepository('S3:Owner')->find($ownerid);
                        if ($owner === null) {
                            $owner = new OwnerData();
                            $owner->ownerid = $ownerid;
                            $owner->displayName = (string) $v->Owner->DisplayName;
                            $em->attach($owner);
                        }
                        $object->setOwner($owner);
                        unset($owner);
                    }
                    $object->setBucketName((string) $bucketName)->setObjectName($objectName)->setETag((string) $v->ETag)->setSize((string) $v->Size)->setStorageClass((string) $v->StorageClass)->setLastModified(new \DateTime((string) $v->LastModified, new \DateTimeZone('UTC')));
                    if (isset($na)) {
                        //For the new object we need to attach it
                        $em->attach($object);
                        unset($na);
                    }
                    $result->append($object);
                    unset($object);
                }
            }
        }
        return $result;
    }