/**
  * Construct an object of CloudStorageClient.
  *
  * @param string $bucket The name of the bucket.
  * @param string $object The name of the object, or null if there is no
  * object.
  * @param resource $context The stream context to use.
  */
 public function __construct($bucket, $object = null, $context = null)
 {
     $this->bucket_name = $bucket;
     $this->object_name = $object;
     if (!isset($context)) {
         $context = stream_context_get_default();
     }
     $context_array = stream_context_get_options($context);
     if (array_key_exists("gs", $context_array)) {
         $this->context_options = array_merge(self::$default_gs_context_options, $context_array["gs"]);
     } else {
         $this->context_options = self::$default_gs_context_options;
     }
     $this->anonymous = util\FindByKeyOrNull($this->context_options, "anonymous");
     $this->url = $this->createObjectUrl($bucket, $object);
 }
示例#2
0
 /**
  * Serve a Google Cloud Storage file as the response.
  *
  * @param string $gcs_filename The name of the Google Cloud Storage object to
  * serve.
  * @param mixed $options Array of additional options for serving the object.
  *   content_type: Content-Type to override when known.
  *   save_as: If True then send the file as an attachment.
  *   start: Start index of content-range to send.
  *   end: End index of content-range to send. End index is inclusive.
  *   use_range: Use provided content range from the request's Range header.
  *     Mutually exclusive with start and end.
  *
  * @throws InvalidArgumentException If invalid options are supplied.
  */
 public static function serve($gcs_filename, $options = [])
 {
     $extra_options = array_diff(array_keys($options), self::$serve_options);
     if (!empty($extra_options)) {
         throw new \InvalidArgumentException('Invalid options supplied: ' . implode(',', $extra_options));
     }
     // Determine the range to send
     $start = util\FindByKeyOrNull($options, "start");
     $end = util\FindByKeyOrNull($options, "end");
     $use_range = util\FindByKeyOrNull($options, "use_range");
     $request_range_header = util\FindByKeyOrNull($_SERVER, "HTTP_RANGE");
     $range_header = BlobstoreService::checkRanges($start, $end, $use_range, $request_range_header);
     $save_as = util\FindByKeyOrNull($options, "save_as");
     if (isset($save_as) && !is_string($save_as)) {
         throw new \InvalidArgumentException("Unexpected value for save_as.");
     }
     $blob_key = BlobstoreService::createGsKey($gcs_filename);
     BlobstoreService::sendHeader(self::BLOB_KEY_HEADER, $blob_key);
     if (isset($range_header)) {
         BlobstoreService::sendHeader(self::BLOB_RANGE_HEADER, $range_header);
     }
     $content_type = util\FindByKeyOrNull($options, "content_type");
     if (isset($content_type)) {
         BlobstoreService::sendHeader("Content-Type", $content_type);
     }
     if (isset($save_as)) {
         BlobstoreService::sendHeader("Content-Disposition", sprintf("attachment; filename=%s", $save_as));
     }
 }
 /**
  * Parse the supplied path and extract the bucket and object names from it.
  * It is possible that there is no object name in the path and a null will be
  * returned in the $object parameters if this is the case.
  */
 private function getBucketAndObjectFromPath($path, &$bucket, &$object)
 {
     // Decompose the $path into the GCS url components and check
     $url_parts = parse_url($path);
     if ($url_parts === false) {
         return false;
     }
     if ($url_parts['scheme'] !== 'gs' || empty($url_parts['host'])) {
         trigger_error(sprintf("Invalid Google Cloud Storage path: %s", $path), E_USER_ERROR);
         return false;
     }
     $bucket = $url_parts['host'];
     $object = null;
     $path = util\FindByKeyOrNull($url_parts, 'path');
     if (isset($path) && $path !== "/") {
         $object = $path;
     }
     return true;
 }