Google\Cloud\Storage\Bucket::upload PHP Method

upload() public method

Example: $object = $bucket->upload( fopen(__DIR__ . '/image.jpg', 'r') ); Upload an object in a resumable fashion while setting a new name for the object and including the content language. $options = [ 'resumable' => true, 'name' => '/images/new-name.jpg', 'metadata' => [ 'contentLanguage' => 'en' ] ]; $object = $bucket->upload( fopen(__DIR__ . '/image.jpg', 'r'), $options ); Upload an object with a customer-supplied encryption key. $key = base64_encode(openssl_random_pseudo_bytes(32)); // Make sure to remember your key. $object = $bucket->upload( fopen(__DIR__ . '/image.jpg', 'r'), ['encryptionKey' => $key] );
See also: https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload#resumable Learn more about resumable uploads.
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/insert Objects insert API documentation.
See also: https://cloud.google.com/storage/docs/encryption#customer-supplied Customer-supplied encryption keys.
public upload ( string | resource | Psr\Http\Message\StreamInterface $data, array $options = [] ) : StorageObject
$data string | resource | Psr\Http\Message\StreamInterface The data to be uploaded.
$options array [optional] { Configuration options. @type string $name The name of the destination. @type bool $resumable Indicates whether or not the upload will be performed in a resumable fashion. @type bool $validate Indicates whether or not validation will be applied using md5 hashing functionality. If true and the calculated hash does not match that of the upstream server the upload will be rejected. @type int $chunkSize If provided the upload will be done in chunks. The size must be in multiples of 262144 bytes. With chunking you have increased reliability at the risk of higher overhead. It is recommended to not use chunking. @type string $predefinedAcl Predefined ACL to apply to the object. Acceptable values include, `"authenticatedRead"`, `"bucketOwnerFullControl"`, `"bucketOwnerRead"`, `"private"`, `"projectPrivate"`, and `"publicRead"`. **Defaults to** `"private"`. @type array $metadata The available options for metadata are outlined at the [JSON API docs](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request-body). @type string $encryptionKey A base64 encoded AES-256 customer-supplied encryption key. @type string $encryptionKeySHA256 Base64 encoded SHA256 hash of the customer-supplied encryption key. This value will be calculated from the `encryptionKey` on your behalf if not provided, but for best performance it is recommended to pass in a cached version of the already calculated SHA. }
return StorageObject
    public function upload($data, array $options = [])
    {
        if (is_string($data) && !isset($options['name'])) {
            throw new \InvalidArgumentException('A name is required when data is of type string.');
        }
        $encryptionKey = isset($options['encryptionKey']) ? $options['encryptionKey'] : null;
        $encryptionKeySHA256 = isset($options['encryptionKeySHA256']) ? $options['encryptionKeySHA256'] : null;
        $response = $this->connection->insertObject($this->formatEncryptionHeaders($options) + ['bucket' => $this->identity['bucket'], 'data' => $data])->upload();
        return new StorageObject($this->connection, $response['name'], $this->identity['bucket'], $response['generation'], $response, $encryptionKey, $encryptionKeySHA256);
    }

Usage Example

 /**
  * Uploads a file to the Google Cloud Storage service.
  *
  * @param string $path
  * @param string|resource $contents
  * @param Config $config
  * @return array
  */
 protected function upload($path, $contents, Config $config)
 {
     $path = $this->applyPathPrefix($path);
     $options = $this->getOptionsFromConfig($config);
     $options['name'] = $path;
     $object = $this->bucket->upload($contents, $options);
     return $this->normaliseObject($object);
 }
All Usage Examples Of Google\Cloud\Storage\Bucket::upload