Bobcares

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*Get up to $4000, Offer valid till November 30th, 2024

For every $500 you spend, we will provide you with a $500 credit on your account*

BLACK FRIDAY SPECIAL

*Get up to $4000, Offer valid till November 30th, 2024

Google Cloud Storage StorageClient | All About

by | Oct 26, 2022

In this article, we will look into the details of “StorageClient” in Google Cloud Storage. As a part of our Google Cloud Platform Support Services, Bobcares offer solutions to your Google Cloud Queries.

“StorageClient” in Google Cloud Storage

The storage client is the client for communicating with the Google Cloud Storage API. The syntax is as follows:

classgoogle.cloud.storage.client.Client(project=Nonecredentials=Nonehttp=None)
Bases: google.cloud.client.JSONClient
google cloud storage storageclient

Parameters

  • project: the project on behalf of which the client acts. When creating a topic, this parameter will pass while creating a topic. If not passed, the default taken from the environment is used.
  • credentials: The OAuth2 credentials to use for this client’s connection. If not passed (and no http object is passed), the default inferred from the environment is used.
  • http: An HTTP object to make requests. If no credentials are provided, an http object is created and bound to the current object’s credentials.

Functions/Methods of “StorageClient” in Google Cloud Storage

1. batch() – Factory constructor for batch object.

Return type: google.cloud.storage.batch.Batch 

Returns: The batch object created.

2. bucket(bucket_name) – Factory constructor for bucket object.

Parameter: bucket_name, which is the name of the bucket to be instantiated.

Return type: google.cloud.storage.bucket.Bucket

Returns: The bucket object created.

3. connection – Get connection or batch on the client.

Return type: google.cloud.storage.connection.Connection

Returns: The connection set on the client, or the batch if one is set.

4. create_bucket(bucket_name) – Create a new bucket.

Parameter: bucket_name, which is the bucket name to create.

Return type: google.cloud.storage.bucket.Bucket

Returns: The newly created bucket.

5. current_batch – Currently-active batch.

Return type: google.cloud.storage.batch.Batch

Returns: The batch at the top of the batch stack.

6. get_bucket(bucket_name) – Get a bucket by name.

Parameter: bucket_name, which is the name of the bucket to get.

Return type: google.cloud.storage.bucket.Bucket

Returns: The bucket matches the name provided.

Raises: google.cloud.exceptions.NotFound 

7. list_buckets(max_results=None, page_token=None, prefix=None, projection='noAcl', fields=None) – Get all buckets in the client project.

Parameters:

  • max_results: Maximum number of buckets to return.
  • page_token: Opaque marker for the next “page” of buckets. If not passed, will return the first page of buckets.
  • prefix: Filter results to buckets whose names begin with this prefix.
  • projection: If used, must be ‘full’ or ‘noAcl’. Defaults to ‘noAcl’. Specifies the set of properties to return.
  • fields: Selector showing which fields to include in a partial response.

Return type: iterable of google.cloud.storage.bucket.Bucket objects.

Returns: All buckets in this project.

8. lookup_bucket(bucket_name) – Get a bucket by name, returning None if not found.

Parameter: bucket_name, which is the name of the bucket to get. 

Return type: google.cloud.storage.bucket.Bucket

Returns: The bucket that matches the name provided or None if not found.

How to establish a connection for “StorageClient” in Google Cloud Storage?

Connection creates or communicate with Google Cloud Storage connections. The syntax is as follows:

classgoogle.cloud.storage.connection.Connection(credentials=Nonehttp=None)

Parameters:

  • credentials: (Optional) The OAuth2 Credentials to use for this connection.
  • http: (Optional) HTTP object to make requests.

Examples

public function testWithStorage()
 {
     $storage = new StorageClient();
     $bucket = $storage->bucket('test-bucket');
     $object = $bucket->object('test-object.jpg');
     $gcsUri = 'gs://test-bucket/test-object.jpg';
     $image = new Image($object, ['landmarks']);
     $res = $image->requestObject();
     $this->assertEquals($res['image']['source']['gcsImageUri'], $gcsUri);
     $this->assertEquals($res['features'], [['type' => 'LANDMARK_DETECTION']]);
 }

[Need a response to another question? We’re only a click away.]

Conclusion

In this post, we came across an explanation of the “StorageClient” in Google Cloud Storage.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.