Ejemplo n.º 1
0
 /**
  * Create an absolute URL that can be used by a user to asynchronously upload
  * a large blob. Upon completion of the upload, a callback is made to the
  * specified URL.
  *
  * @param string $success_path A relative URL which will be invoked after the
  * user successfully uploads a blob.
  * @param mixed[] $options A key value pair array of upload options. Valid
  * options are:
  * - max_bytes_per_blob: an integer value of the largest size that any one
  *   uploaded blob may be. Default value: unlimited.
  * - max_bytes_total: an integer value that is the total size that sum of all
  *   uploaded blobs may be. Default value: unlimited.
  * - gs_bucket_name: a string that is the name of a Google Cloud Storage
  *   bucket that the blobs should be uploaded to. Not specifying a value
  *   will result in the blob being uploaded to the application's default
  *   bucket.
  *
  * @return string The upload URL.
  *
  * @throws InvalidArgumentException If $success_path is not valid, or one of
  * the options is not valid.
  * @throws BlobstoreException Thrown when there is a failure using the
  * blobstore service.
  */
 public static function createUploadUrl($success_path, $options = array())
 {
     $req = new CreateUploadURLRequest();
     $resp = new CreateUploadURLResponse();
     if (!is_string($success_path)) {
         throw new \InvalidArgumentException('$success_path must be a string');
     }
     $req->setSuccessPath($success_path);
     if (array_key_exists('max_bytes_per_blob', $options)) {
         $val = $options['max_bytes_per_blob'];
         if (!is_int($val)) {
             throw new \InvalidArgumentException('max_bytes_per_blob must be an integer');
         }
         if ($val < 1) {
             throw new \InvalidArgumentException('max_bytes_per_blob must be positive.');
         }
         $req->setMaxUploadSizePerBlobBytes($val);
     }
     if (array_key_exists('max_bytes_total', $options)) {
         $val = $options['max_bytes_total'];
         if (!is_int($val)) {
             throw new \InvalidArgumentException('max_bytes_total must be an integer');
         }
         if ($val < 1) {
             throw new \InvalidArgumentException('max_bytes_total must be positive.');
         }
         $req->setMaxUploadSizeBytes($val);
     }
     if (array_key_exists('gs_bucket_name', $options)) {
         $val = $options['gs_bucket_name'];
         if (!is_string($val)) {
             throw new \InvalidArgumentException('gs_bucket_name must be a string');
         }
         $req->setGsBucketName($val);
     } else {
         $bucket = BlobstoreService::getDefaultGoogleStorageBucketName();
         if (!$bucket) {
             throw new \InvalidArgumentException('Application does not have a default Cloud Storage Bucket, ' . 'gs_bucket_name must be specified');
         }
         $req->setGsBucketName($bucket);
     }
     $extra_options = array_diff(array_keys($options), self::$create_upload_url_options);
     if (!empty($extra_options)) {
         throw new \InvalidArgumentException('Invalid options supplied: ' . implode(',', $extra_options));
     }
     try {
         ApiProxy::makeSyncCall('blobstore', 'CreateUploadURL', $req, $resp);
     } catch (ApplicationError $e) {
         throw BlobstoreService::ApplicationErrorToException($e);
     }
     return $resp->getUrl();
 }
Ejemplo n.º 2
0
 /**
  * Create an absolute URL that can be used by a user to asynchronously upload
  * a large blob. Upon completion of the upload, a callback is made to the
  * specified URL.
  *
  * @param string $success_path A relative URL which will be invoked after the
  * user successfully uploads a blob.
  * @param mixed[] $options A key value pair array of upload options. Valid
  * options are:<ul>
  * <li>'max_bytes_per_blob': integer The value of the largest size that any
  * one uploaded blob may be. Default value: unlimited.
  * <li>'max_bytes_total': integer The value that is the total size that sum of
  * all uploaded blobs may be. Default value: unlimited.
  * <li>'gs_bucket_name': string The name of a Google Cloud Storage
  *   bucket that the blobs should be uploaded to. Not specifying a value
  *   will result in the blob being uploaded to the application's default
  *   bucket.
  * <li>'url_expiry_time_seconds': integer The number of seconds that the
  *   generated URL can be used for to upload files to Google Cloud Storage.
  *   Once this timeout expires, the URL is no longer valid and any attempts
  *   to upload using the URL will fail. Must be a positive integer, maximum
  *   value is one day (86400 seconds). Default Value: 600 seconds.
  * </ul>
  * @return string The upload URL.
  *
  * @throws \InvalidArgumentException If $success_path is not valid, or one of
  * the options is not valid.
  * @throws CloudStorageException Thrown when there is a failure using the
  * blobstore service.
  */
 public static function createUploadUrl($success_path, $options = array())
 {
     $req = new CreateUploadURLRequest();
     $resp = new CreateUploadURLResponse();
     if (!is_string($success_path)) {
         throw new \InvalidArgumentException('$success_path must be a string');
     }
     $req->setSuccessPath($success_path);
     $max_upload_size_ini = self::getUploadMaxFileSizeInBytes();
     if (array_key_exists('max_bytes_per_blob', $options)) {
         $val = $options['max_bytes_per_blob'];
         if (!is_int($val)) {
             throw new \InvalidArgumentException('max_bytes_per_blob must be an integer');
         }
         if ($val < 1) {
             throw new \InvalidArgumentException('max_bytes_per_blob must be positive.');
         }
         $req->setMaxUploadSizePerBlobBytes($val);
     } else {
         if ($max_upload_size_ini > 0) {
             $req->setMaxUploadSizePerBlobBytes($max_upload_size_ini);
         }
     }
     if (array_key_exists('max_bytes_total', $options)) {
         $val = $options['max_bytes_total'];
         if (!is_int($val)) {
             throw new \InvalidArgumentException('max_bytes_total must be an integer');
         }
         if ($val < 1) {
             throw new \InvalidArgumentException('max_bytes_total must be positive.');
         }
         $req->setMaxUploadSizeBytes($val);
     }
     if (array_key_exists('url_expiry_time_seconds', $options)) {
         $val = $options['url_expiry_time_seconds'];
         if (!is_int($val)) {
             throw new \InvalidArgumentException('url_expiry_time_seconds must be an integer');
         }
         if ($val < 1) {
             throw new \InvalidArgumentException('url_expiry_time_seconds must be positive.');
         }
         if ($val > self::MAX_URL_EXPIRY_TIME_SECONDS) {
             throw new \InvalidArgumentException('url_expiry_time_seconds must not exceed ' . self::MAX_URL_EXPIRY_TIME_SECONDS);
         }
         $req->setUrlExpiryTimeSeconds($val);
     }
     if (array_key_exists('gs_bucket_name', $options)) {
         $val = $options['gs_bucket_name'];
         if (!is_string($val)) {
             throw new \InvalidArgumentException('gs_bucket_name must be a string');
         }
         $req->setGsBucketName($val);
     } else {
         $bucket = self::getDefaultGoogleStorageBucketName();
         if (!$bucket) {
             throw new \InvalidArgumentException('Application does not have a default Cloud Storage Bucket, ' . 'gs_bucket_name must be specified');
         }
         $req->setGsBucketName($bucket);
     }
     $extra_options = array_diff(array_keys($options), self::$create_upload_url_options);
     if (!empty($extra_options)) {
         throw new \InvalidArgumentException('Invalid options supplied: ' . htmlspecialchars(implode(',', $extra_options)));
     }
     try {
         ApiProxy::makeSyncCall('blobstore', 'CreateUploadURL', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::applicationErrorToException($e);
     }
     return $resp->getUrl();
 }