예제 #1
0
 /**
  * Perform the HTTP query for a list of objects and de-serialize the
  * results.
  */
 protected function objectQuery($params = [], $limit = null, $marker = null)
 {
     if (isset($limit)) {
         $params['limit'] = (int) $limit;
         if (!empty($marker)) {
             $params['marker'] = (string) $marker;
         }
     }
     // We always want JSON.
     $params['format'] = 'json';
     $query = http_build_query($params);
     $query = str_replace('%2F', '/', $query);
     $url = $this->url . '?' . $query;
     $headers = ['X-Auth-Token' => $this->token];
     $response = $this->client->get($url, ['headers' => $headers]);
     // The only codes that should be returned are 200 and the ones
     // already thrown by GET.
     if ($response->getStatusCode() != 200) {
         throw new Exception('An unknown exception occurred while processing the request.');
     }
     $json = $response->json();
     // Turn the array into a list of RemoteObject instances.
     $list = [];
     foreach ($json as $item) {
         if (!empty($item['subdir'])) {
             $list[] = new Subdir($item['subdir'], $params['delimiter']);
         } elseif (empty($item['name'])) {
             throw new Exception('Unexpected entity returned.');
         } else {
             //$url = $this->url . '/' . rawurlencode($item['name']);
             $url = self::objectUrl($this->url, $item['name']);
             $list[] = RemoteObject::newFromJSON($item, $this->token, $url, $this->client);
         }
     }
     return $list;
 }
예제 #2
0
 /**
  * Create a new RemoteObject from HTTP headers.
  *
  * This is used to create objects from GET and HEAD requests, which
  * return all of the metadata inside of the headers.
  *
  * @param string $name    The name of the object.
  * @param array  $headers An associative array of HTTP headers in the exact
  *                        format documented by OpenStack's API docs.
  * @param string $token   The current auth token (used for issuing subsequent
  *                        requests).
  * @param string $url     The URL to the object in the object storage. Used for
  *                        issuing subsequent requests.
  *
  * @return \OpenStack\ObjectStore\v1\Resource\RemoteObject A new RemoteObject.
  */
 public static function newFromHeaders($name, $headers, $token, $url, ClientInterface $client = null)
 {
     $object = new RemoteObject($name);
     //$object->allHeaders = $headers;
     $object->setHeaders($headers);
     //throw new \Exception(print_r($headers, true));
     // Fix inconsistant header.
     if (isset($headers['ETag'])) {
         $headers['Etag'] = $headers['ETag'];
     }
     $object->setContentType($headers['Content-Type']);
     $object->contentLength = empty($headers['Content-Length']) ? 0 : (int) $headers['Content-Length'];
     $object->etag = (string) $headers['Etag'];
     // ETag is now Etag.
     $object->lastModified = strtotime($headers['Last-Modified']);
     // Set the metadata, too.
     $object->setMetadata(Container::extractHeaderAttributes($headers));
     // If content encoding and disposition exist, set them on the
     // object.
     if (!empty($headers['Content-Disposition'])) {
         $object->setDisposition($headers['Content-Disposition']);
     }
     if (!empty($headers['Content-Encoding'])) {
         $object->setEncoding($headers['Content-Encoding']);
     }
     $object->token = $token;
     $object->url = $url;
     if (is_null($client)) {
         $client = GuzzleAdapter::create();
     }
     $object->setClient($client);
     return $object;
 }