PHP Class Google\Cloud\Storage\Bucket

Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $storage = $cloud->storage(); $bucket = $storage->bucket('my-bucket');
Inheritance: use trait EncryptionTrait
Datei anzeigen Open project: GoogleCloudPlatform/gcloud-php Class Usage Examples

Public Methods

Method Description
__construct ( Google\Cloud\Storage\Connection\ConnectionInterface $connection, string $name, array $info = null )
acl ( ) : Acl Configure ACL for this bucket.
compose ( array $sourceObjects, string $name, array $options = [] ) : StorageObject Composes a set of objects into a single object.
defaultAcl ( ) : Acl Configure default object ACL for this bucket.
delete ( array $options = [] ) : void Delete the bucket.
exists ( ) : boolean Check whether or not the bucket exists.
getResumableUploader ( string | resource | Psr\Http\Message\StreamInterface $data, array $options = [] ) : ResumableUploader Get a resumable uploader which can provide greater control over the upload process. This is recommended when dealing with large files where reliability is key.
info ( array $options = [] ) : array Retrieves the bucket's details. If no bucket data is cached a network request will be made to retrieve it.
name ( ) : string Retrieves the bucket's name.
object ( string $name, array $options = [] ) : StorageObject Lazily instantiates an object. There are no network requests made at this point. To see the operations that can be performed on an object please see {@see Google\Cloud\Storage\StorageObject}.
objects ( array $options = [] ) : Generator Fetches all objects in the bucket.
reload ( array $options = [] ) : array Triggers a network request to reload the bucket's details.
update ( array $options = [] ) : array Update the bucket. Upon receiving a result the local bucket's data will be updated.
upload ( string | resource | Psr\Http\Message\StreamInterface $data, array $options = [] ) : StorageObject Upload your data in a simple fashion. Uploads will default to being resumable if the file size is greater than 5mb.

Method Details

__construct() public method

public __construct ( Google\Cloud\Storage\Connection\ConnectionInterface $connection, string $name, array $info = null )
$connection Google\Cloud\Storage\Connection\ConnectionInterface Represents a connection to Cloud Storage.
$name string The bucket's name.
$info array [optional] The bucket's metadata.

acl() public method

Example: $acl = $bucket->acl();
See also: https://cloud.google.com/storage/docs/access-control More about Access Control Lists
public acl ( ) : Acl
return Acl An ACL instance configured to handle the bucket's access control policies.

compose() public method

Please note that all objects to be composed must come from the same bucket. Example: $sourceObjects = ['log1.txt', 'log2.txt']; $singleObject = $bucket->compose($sourceObjects, 'combined-logs.txt'); Use an instance of StorageObject. $sourceObjects = [ $bucket->object('log1.txt'), $bucket->object('log2.txt') ]; $singleObject = $bucket->compose($sourceObjects, 'combined-logs.txt');
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/compose Objects compose API documentation
public compose ( array $sourceObjects, string $name, array $options = [] ) : StorageObject
$sourceObjects array The objects to compose.
$name string The name of the composed object.
$options array [optional] { Configuration options. @type string $predefinedAcl Predefined ACL to apply to the composed object. Acceptable values include, `"authenticatedRead"`, `"bucketOwnerFullControl"`, `"bucketOwnerRead"`, `"private"`, `"projectPrivate"`, and `"publicRead"`. **Defaults to** `"private"`. @type array $metadata Metadata to apply to the composed object. 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 $ifGenerationMatch Makes the operation conditional on whether the object's current generation matches the given value. @type string $ifMetagenerationMatch Makes the operation conditional on whether the object's current metageneration matches the given value. }
return StorageObject

defaultAcl() public method

Example: $acl = $bucket->defaultAcl();
See also: https://cloud.google.com/storage/docs/access-control More about Access Control Lists
public defaultAcl ( ) : Acl
return Acl An ACL instance configured to handle the bucket's default object access control policies.

delete() public method

Example: $bucket->delete();
See also: https://cloud.google.com/storage/docs/json_api/v1/buckets/delete Buckets delete API documentation.
public delete ( array $options = [] ) : void
$options array [optional] { Configuration options. @type string $ifMetagenerationMatch If set, only deletes the bucket if its metageneration matches this value. @type string $ifMetagenerationNotMatch If set, only deletes the bucket if its metageneration does not match this value. }
return void

exists() public method

Example: if ($bucket->exists()) { echo 'Bucket exists!'; }
public exists ( ) : boolean
return boolean

getResumableUploader() public method

Example: $uploader = $bucket->getResumableUploader( fopen(__DIR__ . '/image.jpg', 'r') ); try { $object = $uploader->upload(); } catch (GoogleException $ex) { $resumeUri = $uploader->getResumeUri(); $object = $uploader->resume($resumeUri); }
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.
public getResumableUploader ( string | resource | Psr\Http\Message\StreamInterface $data, array $options = [] ) : ResumableUploader
$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 $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 Google\Cloud\Upload\ResumableUploader

info() public method

Example: $info = $bucket->info(); echo $info['location'];
See also: https://cloud.google.com/storage/docs/json_api/v1/buckets/get Buckets get API documentation.
public info ( array $options = [] ) : array
$options array [optional] { Configuration options. @type string $ifMetagenerationMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration matches the given value. @type string $ifMetagenerationNotMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration does not match the given value. @type string $projection Determines which properties to return. May be either 'full' or 'noAcl'. }
return array

name() public method

Example: echo $bucket->name();
public name ( ) : string
return string

object() public method

Example: $object = $bucket->object('file.txt');
public object ( string $name, array $options = [] ) : StorageObject
$name string The name of the object to request.
$options array [optional] { Configuration options. @type string $generation Request a specific revision of the object. @type string $encryptionKey A base64 encoded AES-256 customer-supplied encryption key. It will be neccesary to provide this when a key was used during the object's creation. @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

objects() public method

Example: Get all objects beginning with the prefix 'photo' $objects = $bucket->objects([ 'prefix' => 'photo', 'fields' => 'items/name,nextPageToken' ]); foreach ($objects as $object) { echo $object->name() . PHP_EOL; }
See also: https://cloud.google.com/storage/docs/json_api/v1/objects/list Objects list API documentation.
public objects ( array $options = [] ) : Generator
$options array [optional] { Configuration options. @type string $delimiter Returns results in a directory-like mode. Results will contain only objects whose names, aside from the prefix, do not contain delimiter. Objects whose names, aside from the prefix, contain delimiter will have their name, truncated after the delimiter, returned in prefixes. Duplicate prefixes are omitted. @type integer $maxResults Maximum number of results to return per request. Defaults to `1000`. @type string $prefix Filter results with this prefix. @type string $projection Determines which properties to return. May be either 'full' or 'noAcl'. @type bool $versions If true, lists all versions of an object as distinct results. The default is false. @type string $fields Selector which will cause the response to only return the specified fields. }
return Generator

reload() public method

Example: $bucket->reload(); $info = $bucket->info(); echo $info['location'];
See also: https://cloud.google.com/storage/docs/json_api/v1/buckets/get Buckets get API documentation.
public reload ( array $options = [] ) : array
$options array [optional] { Configuration options. @type string $ifMetagenerationMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration matches the given value. @type string $ifMetagenerationNotMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration does not match the given value. @type string $projection Determines which properties to return. May be either 'full' or 'noAcl'. }
return array

update() public method

Example: Enable logging on an existing bucket. $bucket->update([ 'logging' => [ 'logBucket' => 'myBucket', 'logObjectPrefix' => 'prefix' ] ]);
See also: https://cloud.google.com/storage/docs/json_api/v1/buckets/patch Buckets patch API documentation.
public update ( array $options = [] ) : array
$options array [optional] { Configuration options. @type string $ifMetagenerationMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration matches the given value. @type string $ifMetagenerationNotMatch Makes the return of the bucket metadata conditional on whether the bucket's current metageneration does not match the given value. @type string $predefinedAcl Apply a predefined set of access controls to this bucket. @type string $predefinedDefaultObjectAcl Apply a predefined set of default object access controls to this bucket. @type string $projection Determines which properties to return. May be either 'full' or 'noAcl'. @type string $fields Selector which will cause the response to only return the specified fields. @type array $acl Access controls on the bucket. @type array $cors The bucket's Cross-Origin Resource Sharing (CORS) configuration. @type array $defaultObjectAcl Default access controls to apply to new objects when no ACL is provided. @type array $lifecycle The bucket's lifecycle configuration. @type array $logging The bucket's logging configuration, which defines the destination bucket and optional name prefix for the current bucket's logs. @type array $versioning The bucket's versioning configuration. @type array $website The bucket's website configuration. }
return array

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