/** * Runs a load job which loads data from a file in a Storage bucket into the * table. * * Example: * ``` * $object = $storage->bucket('myBucket')->object('important-data.csv'); * $table->load($object); * ``` * * @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs insert API Documentation. * * @param StorageObject $destination The object to load data from. * @param array $options [optional] { * Configuration options. * * @type array $jobConfig Configuration settings for a load job are * outlined in the [API Docs for `configuration.load`](https://goo.gl/j6jyHv). * If not provided default settings will be used. * } * @return Job */ public function loadFromStorage(StorageObject $object, array $options = []) { $objIdentity = $object->identity(); $options['jobConfig']['sourceUris'] = ['gs://' . $objIdentity['bucket'] . '/' . $objIdentity['object']]; return $this->load(null, $options); }
/** * Formats the request for the API. * * @param string|StorageObject $content The content to analyze. * @param array $options [optional] Configuration Options. * @return array */ private function formatRequest($content, array $options) { $docOptions = ['type', 'language', 'content', 'gcsContentUri']; $options += ['encodingType' => 'UTF8', 'type' => 'PLAIN_TEXT']; if ($content instanceof StorageObject) { $objIdentity = $content->identity(); $options['gcsContentUri'] = 'gs://' . $objIdentity['bucket'] . '/' . $objIdentity['object']; } else { $options['content'] = $content; } foreach ($options as $option => $value) { if (in_array($option, $docOptions)) { $options['document'][$option] = $value; unset($options[$option]); } } return $options; }
/** * Formats the request for the API. * * @param resource|string|StorageObject $audio * @param array $options * @return array * @throws \InvalidArgumentException */ private function formatRequest($audio, array $options) { $analyzedFileInfo = null; $fileFormat = null; $recognizeOptions = ['encoding', 'sampleRate', 'languageCode', 'maxAlternatives', 'profanityFilter', 'speechContext']; if ($audio instanceof StorageObject) { $objIdentity = $audio->identity(); $options['audio']['uri'] = 'gs://' . $objIdentity['bucket'] . '/' . $objIdentity['object']; $fileFormat = pathinfo($options['audio']['uri'], PATHINFO_EXTENSION); } elseif (is_resource($audio)) { $options['audio']['content'] = base64_encode(stream_get_contents($audio)); $fileFormat = pathinfo(stream_get_meta_data($audio)['uri'], PATHINFO_EXTENSION); } else { $options['audio']['content'] = base64_encode($audio); } if (isset($options['encoding'])) { $options['encoding'] = strtoupper($options['encoding']); } else { $analyzedFileInfo = $this->analyzeAudio($audio); $options['encoding'] = isset($analyzedFileInfo['fileformat']) ? $this->determineEncoding($analyzedFileInfo['fileformat']) : $this->determineEncoding($fileFormat); } if (isset($options['sampleRate'])) { $options['sampleRate'] = (int) $options['sampleRate']; } else { if (!$analyzedFileInfo) { $analyzedFileInfo = $this->analyzeAudio($audio); } $options['sampleRate'] = isset($analyzedFileInfo['audio']['sample_rate']) ? $analyzedFileInfo['audio']['sample_rate'] : $this->determineSampleRate($options['encoding']); } if (!$options['encoding']) { throw new \InvalidArgumentException('Unable to determine encoding. Please provide the value manually.'); } if (!$options['sampleRate']) { throw new \InvalidArgumentException('Unable to determine sample rate. Please provide the value manually.'); } foreach ($options as $option => $value) { if (in_array($option, $recognizeOptions)) { $options['config'][$option] = $value; unset($options[$option]); } } return $options; }
public function testGetsIdentity() { $object = new StorageObject($this->connection->reveal(), $name = 'object.txt', $bucketName = 'bucket'); $this->assertEquals($name, $object->identity()['object']); $this->assertEquals($bucketName, $object->identity()['bucket']); }
/** * Returns a dictionary of object metadata from an object. * * @param StorageObject $object * @return array */ protected function normaliseObject(StorageObject $object) { $name = $object->name(); $info = $object->info(); $isDir = substr($name, -1) === '/'; if ($isDir) { $name = rtrim($name, '/'); } return ['type' => $isDir ? 'dir' : 'file', 'dirname' => Util::dirname($name), 'path' => $name, 'timestamp' => strtotime($info['updated']), 'mimetype' => $info['contentType'], 'size' => $info['size']]; }
/** * Create an image with all required configuration. * * @param resource|string|StorageObject $image An image to configure with * the given settings. This parameter will accept a resource, a * string of bytes, or an instance of * {@see Google\Cloud\Storage\StorageObject}. * @param array $features A list of cloud vision * [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#type) * to apply to the image. Google Cloud Platform Client Library provides a set of abbreviated * names which can be used in the interest of brevity in place of * the names offered by the cloud vision service. These names are * `faces`, `landmarks`, `logos`, `labels`, `text`, `safeSearch` * and `imageProperties`. * @param array $options { * Configuration Options * * @type array $maxResults A list of features and the maximum number of * results to return. Keys should correspond to feature names * given in the `$features` array, and values should be of type * int. In all cases where `$maxResults` does not contain a value * for a feature, all results will be returned. In cases where * a `$maxResults` value is specified, the cloud vision service * will return results up to the `$maxResults` value, or the full * results, whichever is fewer. * @type array $imageContext See * [ImageContext](https://cloud.google.com/vision/reference/rest/v1/images/annotate#imagecontext) * for full usage details. * } * @throws InvalidArgumentException */ public function __construct($image, array $features, array $options = []) { $this->options = $options + ['imageContext' => [], 'maxResults' => []]; $this->features = $this->normalizeFeatures($features); if ($image instanceof StorageObject) { $identity = $image->identity(); $uri = sprintf('gs://%s/%s', $identity['bucket'], $identity['object']); $this->type = self::TYPE_STORAGE; $this->image = $uri; } elseif (is_string($image)) { $this->type = self::TYPE_STRING; $this->image = $image; } elseif (is_resource($image)) { $this->type = self::TYPE_BYTES; $this->image = Psr7\stream_for($image); } else { throw new InvalidArgumentException('Given image is not valid. ' . 'Image must be a string of bytes, a google storage object, or a resource.'); } }