Giter Club home page Giter Club logo

google-cloud-php's Introduction

Google Cloud PHP Client

Idiomatic PHP client for Google Cloud Platform services.

CI Status

PHP Version Status
Latest Stable Version Packagist Kokoro CI

View the list of supported APIs and Services.

If you need support for other Google APIs, please check out the Google APIs Client Library for PHP.

Quick Start

We recommend installing individual component packages. A list of available packages can be found on Packagist.

For example:

$ composer require google/cloud-storage
$ composer require google/cloud-bigquery
$ composer require google/cloud-datastore

You can then include the autoloader and create your client:

require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;

$storage = new StorageClient();

$bucket = $storage->bucket('my_bucket');

// Upload a file to the bucket.
$bucket->upload(
    fopen('/data/file.txt', 'r')
);

// Download and store an object from the bucket locally.
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('/data/file_backup.txt');

Authentication

Authentication is handled by the client library automatically. You just need to provide the authentication details when creating a client. Generally, authentication is accomplished using a Service Account. For more information on obtaining Service Account credentials, see our Authentication Guide.

Once you've obtained your credentials file, it may be used to create an authenticated client.

require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;

// Authenticate using a keyfile path
$cloud = new StorageClient([
    'keyFilePath' => 'path/to/keyfile.json'
]);

// Authenticate using keyfile data
$cloud = new StorageClient([
    'keyFile' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);

If you do not wish to embed your authentication information in your application code, you may also make use of Application Default Credentials.

require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json');

$cloud = new StorageClient();

The GOOGLE_APPLICATION_CREDENTIALS environment variable may be set in your server configuration.

gRPC and Protobuf

Many clients in Google Cloud PHP offer support for gRPC, either as an option or a requirement. gRPC is a high-performance RPC framework created by Google. To use gRPC in PHP, you must install the gRPC PHP extension on your server. While not required, it is also recommended that you install the protobuf extension whenever using gRPC in production.

$ pecl install grpc
$ pecl install protobuf

Caching Access Tokens

By default the library will use a simple in-memory caching implementation, however it is possible to override this behavior by passing a PSR-6 caching implementation in to the desired client.

The following example takes advantage of Symfony's Cache Component.

require 'vendor/autoload.php';

use Google\Cloud\Storage\StorageClient;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

// Please take the proper precautions when storing your access tokens in a cache no matter the implementation.
$cache = new ArrayAdapter();

$storage = new StorageClient([
    'authCache' => $cache
]);

This library provides a PSR-6 implementation with the SystemV shared memory at Google\Auth\Cache\SysVCacheItemPool. This implementation is only available on *nix machines, but it's the one of the fastest implementations and you can share the cache among multiple processes. The following example shows how to use it.

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Spanner\SpannerClient;
use Google\Auth\Cache\SysVCacheItemPool;

$cache = new SysVCacheItemPool();

$spanner = new SpannerClient([
    'authCache' => $cache
]);

PHP Versions Supported

All client libraries support PHP 8.0 and above.

Versioning

This library follows Semantic Versioning.

Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.

GA: Libraries defined at a GA quality level are stable, and will not introduce backwards-incompatible changes in any minor or patch releases. We will address issues and requests with the highest priority. Please note, for any components which include generated clients the GA guarantee will only apply to clients which interact with stable services. For example, in a component which hosts V1 and V1beta1 generated clients, the GA guarantee will only apply to the V1 client as the service it interacts with is considered stable.

Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we're working towards their release candidate. We will address issues and requests with a higher priority.

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information on how to get started.

This repository is not an official support channel. If you have support questions, file a support request through the normal Google support channels, or post questions on a forum such as StackOverflow.

License

Apache 2.0 - See LICENSE for more information.

google-cloud-php's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

google-cloud-php's Issues

Make an ExponentialBackoff for Jobs

What if we made a Google\Cloud\ExponentialBackoff for job completion, i.e.:

$job = $table->load(fopen($source, 'r'), $options);
$backoff = new ExponentialBackoff(10);
$backoff->execute(function() use ($job, $output) {
    $job->reload();
    return $job->isComplete();
});

Guzzle PSR7 Incompatibilities

Can you upgrade the guzzlehttp/psr7 to the 1.3.* versions rather than keeping it on the 1.2? It causes incompabilities with many libraries that have already upgraded to those. I'm not sure if there are breaking incompatibilities with the 1.3 version but it would be nice to at least have the possibility of 1.3 if it does require it. Thanks!

Storage > Automatically check the file public checkbox

Hello,

After use $bucket->upload($content, $options);
I want to automatically set it to public as if I upload a file on Google Cloud Storage.

share checkbox
( « share to public » on Google Cloud Storage UI )

It seems you don’t develop this functionality yet, did you think about it ?

Files remain in blobstore after uploading to cloud storage

I have a PHP-based GAE app that takes file uploads, and uses the special app engine handled upload URLs to have users upload files directly to google cloud storage engine. This works great, but I just noticed that for some reason every file that has ever been uploaded also shows up in Blobstore. This is true even of files that I have already deleted from the storage bucket in question. Furthermore, when I try to delete these files from blobstore, I get a message saying they could not be deleted. How can I delete this? I don't want to be billed for them.. Also how can I prevent this from happening?

screenshot_blobs

Packagist.org not up to date?

Packagist delivers the wrong commit hash for dev-master - could you check the setup for the webhook?

Thank you in advance! :)

Asynchronous request support

Using this issue as a place to house research and share ideas as to how we can implement async request support.

Namespace to "Google\Cloud"

Sorry to go back on this, but after a long discussion, we have decided to go with Google\Cloud for the namesplace, and the term gcloud should be used/referenced as little as possible (i.e. the reponame and CLI, and ideally nothing else).

This is not pressing, but should be done before we cut a release.

cURL error 35: Encountered end of file

I have this code:

...
$bucket_obj = $bucket->object($img_file);
if ($bucket_obj && $bucket_obj->exists()) {
            $cloud_image = $bucket_obj->downloadAsString([
                'retries' => 0,
                'httpOptions' => [ 'debug' => $fp ]
            ]);
            if ($cloud_image) {
                $file_found = true;
            }
        }

...
..

but while calling the downloadAsString method I receive this error:

nginx_error www 2016/08/08 12:48:22 [error] 9523#0: *1064934 FastCGI sent in stderr: "PHP message: PHP Notice: Error while requesting [f9fa6866f3f06bd6474f2fed564aafba66af.jpg] at Google Cloud Storage [cURL error 35: Encountered end of file (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)]. Trace: #0 /var/www/html/public/vendor/google/cloud/src/RequestWrapper.php(151): GoogleCloudRequestWrapper->convertToGoogleException(Object(GuzzleHttpExceptionConnectException))
nginx_error www #1 /var/www/html/public/vendor/google/cloud/src/Storage/Connection/Rest.php(196): GoogleCloudRequestWrapper->send(Object(GuzzleHttpPsr7Request), Array)
nginx_error www #2 /var/www/html/public/vendor/google/cloud/src/Storage/Object.php(208): GoogleCloudStorageConnectionRest->downloadObject(Array)
nginx_error www #3 /var/www/html/public/test.php(727): GoogleCloudStorageObject->downloadAsString(Array)

What can it be and how do I prevent this from happening?

I tried to pass some options to the method to get some debug info:
[ 'retries' => 0, httpOptions' => [ 'debug' => $fp ] ]

but apparently this doesn't work either.
Is there a documentation or anything about these options? The only thing I can find in the doc is
options array Configuration options. which doesn't help that much :p

Undefined variable $key

/**
* Get a list of properties excluded from datastore indexes
*
* @param array $entityData The incoming entity data
* @return array
*/
public function responseToExcludeFromIndexes(array $entityData)
{
$excludes = [];

    foreach ($entityData as $property) {
        $type = key($property);

        if (isset($property['excludeFromIndexes']) && $property['excludeFromIndexes']) {
            $excludes[] = $key;
        }
    }

    return $excludes;
}

On the line where $key is being added to the excludes array, $key is undefined.

Correct way for copying files through storage API?

Hi there;

from what I see, the copy operation for the storage API (storage.objects.copy in the service definition) is not implemented [yet]. Is this by intention, or by accident?

If by intention, what would be the correct way to copy an object then?

Thx!

Storage API

My focus this week will be the Storage API. Looking through our various gcloud-* projects I see a mixture of wrapping/rolling new. Do we have any set guidelines which help set a precedence for this decision?

Before I get too deep I'd love to get some feedback/thoughts on what the best approach may be here. Currently my vote goes for rolling our own approach as I love the flexibility we would have, but I am especially curious on the thoughts of anyone familiar with the google-api-php-client.

Thanks all!

gRPC - Keep the versions of auth in sync across libraries

#62 (comment)

We are using auth 0.9 while gRPC and GAX are tied to 0.7. If it is at all possible to remove the auth dependency from gRPC and GAX that would allow us much greater flexibility.

At the very least lets make sure we are all on the same version as there are breaking changes in 0.9.

Streamline Metadata Reader

I propose to:

  • formalize StreamReader into some kind of MetadataReaderInterface to allow injecting shinier mocks
    and custom implementations (L1 caching for example) through the setter on Metadata
  • add an accessor and mutator to StreamReader for the resource context (ofc to the interface as well)

That would be great. If you're interested, I'd go ahead and create some of the above.

Can `Logger` have `list` (or `entries`) method?

Since the logger knows the formatted logName, I think it's rather easy to implement.

It's great if we can do

        $logging = new LoggingClient(['projectId' => $project]);
        $logger = $logging->logger('my_logger');
        $entry = $logger->entry($message, [
            'type' => 'gcs_bucket',
            'labels' => [
                'bucket_name' => 'my_bucket'
            ]
        ]);
        $logger->write($entry);
        foreach ($logger->list() as $entry) {
            // do something
        }

Need to ensure we use lower-case gRPC header keys

This might be a no-op, but please double check. Filing this issue just in case.

We need to ensure that all headers used for gRPC are written in lower-case. Please review this Pull Request for reference of what needs to be updated.

Again, perhaps this doesn't apply to your language. Feel free to comment and then close the issue (after double checking).

Support GCS stream wrapper

We are working on getting WordPress to work in Docker. @tmatsuo has a pretty sweet setup here using gs:// paths:
https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/managed_vms/wordpress/README.md

This is supported in the App Engine PHP SDK, but we'd like to bring support over to the veneer:
https://github.com/GoogleCloudPlatform/appengine-php-sdk/tree/725347f9e9321fc6fe839e58759cb95716c4febd/google/appengine/ext/cloud_storage_streams

This would allow us to change the wordpress plugin to work in k8s and gce.

InvalidArgumentException

After the change to PSR7, Request now returns an [InvalidArgumentException] Invalid resource type: array on exists function in Object in Storage:
$exists = $bucket->object('object')->exists();

The proposed fix is on the RequestBuilder class under build is to change it from

return new Request(
$action['httpMethod'],
$uri,
['Content-Type' => 'application/json'],
$body ? json_encode($body) : []
);

to

return new Request(
$action['httpMethod'],
$uri,
['Content-Type' => 'application/json'],
$body ? json_encode($body) : null
);

This will get PSR7 to register the body as empty and proceed as such. Thanks!

Document how to filter log entries

When I'm trying to write sample code for the Logging API, it is not clear how to apply filters.

Here is an example, writing the log with this code:

        $logging = new LoggingClient(['projectId' => $project]);
        $logger = $logging->logger('my_logger');
        $entry = $logger->entry($message, [
            'type' => 'gcs_bucket',
            'labels' => [
                'bucket_name' => 'my_bucket'
            ]
        ]);
        $logger->write($entry);

then trying to list entries:

        $logging = new LoggingClient(['projectId' => $project]);
        $options = [
            'filter' => [
                'resource.type' => 'gcs_bucket'
            ]
        ];
        foreach ($logging->entries($options) as $entry) {
            /* @var $entry \Google\Cloud\Logging\Entry */
            var_dump($entry->info());
        }

I'm getting the following error:

  [Google\Cloud\Exception\BadRequestException]                                                                        
  Client error: `POST https://logging.googleapis.com/v2beta1/entries:list` resulted in a `400 Bad Request` response:  
  {                                                                                                                   
    "error": {                                                                                                        
      "code": 400,                                                                                                    
      "message": "Invalid JSON payload received. Unknown name \"resource.type\" at 'filter (truncated...)             


list-entries [--project PROJECT]

How can I filter entries by resource.type, and also with other fields?

[Discussion] Should cloud test utilities be in this repo?

I'm thinking to upload our testing libraries to packagist. I'm thinking to have a namespace Google/Cloud/TestUtils or something.

The library is now in one of our repo:
https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/testing

GaeApp.php represents App Engine application, you can do something like:

$gaeApp = new GaeApp('project_id', 'version_id');
$gaeApp->run(); // Run devappserver
$gaeApp->deploy(); // Deploy to the cloud
$gaeApp->getLocalBaseUrl(); // returns a base URL of the devserver
$gaeApp->getBaseUrl(); // returns a base URL of deployed app

LocalTestTrait.php and E2EDeploymentTrait.php allows you to easily write e2e tests for your app (examples; 1, 2).

I'd like to ask you guys. Do you think this library should be in this repo? or should I create a new repo?

Thanks!

Downloading object from google storage buckets

How we can download a object from bucktes.
Suppose my bucket is bucket1 and contains many objects(folders) with in it.
bucket
|---------object1
|        |--------folder1
|        |--------folder2
|
|---------object2

I want to download object 1 in zip format..how we can done it with php..?

Datastore Key not being seen properly

if (!is_string($key) && !($key instanceof Key)) {
throw new InvalidArgumentException(
'$key must be an instance of Key or a string'
);
}

This line in Operation.php, is not working because my key is the Key class but it is not recognizing that.

Thanks!

PHP7 compat with Object class

PHP7 says that a class with the name Object is forbidden in PHP. It probably should be renamed to something else other than Object.

Inline Docs

Find a standard library to write docs alongside the code, similar to JSDocs.

Testing

Any preferred testing libraries to incorporate here?

gRPC - Objects are not deserializing properly

#62 (comment)

Objects are not deserializing properly as there is an issue with gRPC. The repositories keyword applies to the root context only, meaning the forked code with the deserialize method (and possibly other changes) won't make it in to gcloud-php or whatever other projects include the gRPC dependency.

We should be able to move past this blocker when the new library to replace https://github.com/drslump/Protobuf-PHP is made available.

tag v0.4

It would be great to get a v0.4 version tagged if the library is ready, because of the updating of google/auth from v0.7 to v0.9, which matches google/apiclient

Build Process

Integrate with Travis to run tests, build docs, etc.

BigQuery: How do I stream insert rows?

Looks like I can load a CSV, or load from GCS, but how do I do a single row insert? Is there a way to pass in one row at a time, kind of like firing an event?

For comparison, gcloud-node has an "import" versus "insert" (https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.37.0/bigquery/table?method=insert) where insert is the "here's some rows" method, and import is the "here's a file or pointer to GCS" method.

I think under the hood they both use "insertAll", but I'm not 100% sure. (https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.