Skip to content

Commit 31f5423

Browse files
authored
feat(storage): upgrade all samples and add new ones (GoogleCloudPlatform#1421)
1 parent 39b0473 commit 31f5423

File tree

109 files changed

+2988
-1646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+2988
-1646
lines changed

storage/README.md

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,80 @@ This simple command-line application demonstrates how to invoke
1616

1717
* See [LICENSE](../../LICENSE)
1818

19-
## Build and Run
20-
1. **Enable APIs** - [Enable the Storage API](https://console.cloud.google.com/flows/enableapi?apiid=storage_api)
21-
and create a new project or select an existing project.
22-
2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials"
23-
and select "Service Account Key". Create a new service account, use the JSON key type, and
24-
select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
25-
to the path of the JSON key that was downloaded.
26-
3. **Clone the repo** and cd into this directory
27-
28-
```sh
29-
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
30-
$ cd php-docs-samples/storage
31-
```
32-
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
19+
### Authentication
20+
21+
Authentication is typically done through [Application Default Credentials][adc]
22+
which means you do not have to change the code to authenticate as long as
23+
your environment has credentials. You have a few options for setting up
24+
authentication:
25+
26+
1. When running locally, use the [Google Cloud SDK][google-cloud-sdk]
27+
28+
gcloud auth application-default login
29+
30+
1. When running on App Engine or Compute Engine, credentials are already
31+
set-up. However, you may need to configure your Compute Engine instance
32+
with [additional scopes][additional_scopes].
33+
34+
1. You can create a [Service Account key file][service_account_key_file]. This file can be used to
35+
authenticate to Google Cloud Platform services from any environment. To use
36+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
37+
the path to the key file, for example:
38+
39+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
40+
41+
[adc]: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
42+
[additional_scopes]: https://cloud.google.com/compute/docs/authentication#using
43+
[service_account_key_file]: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
44+
45+
## Install Dependencies
46+
47+
1. [Enable the Cloud Storage API](https://console.cloud.google.com/flows/enableapi?apiid=storage.googleapis.com).
48+
49+
1. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
3350
Run `php composer.phar install` (if composer is installed locally) or `composer install`
3451
(if composer is installed globally).
35-
5. Run `php storage.php` to see a list of commands:
36-
37-
```sh
38-
bucket-lock Manage Cloud Storage retention policies and holds
39-
object-acl Manage the ACL for Cloud Storage objects
40-
objects Manage Cloud Storage objects
41-
requester-pays Manage Cloud Storage requester pays buckets and objects
42-
uniform-bucket-level-access Manage Cloud Storage uniform bucket-level access buckets
43-
get-object-v2-signed-url Generate a v2 signed URL for downloading an object.
44-
get-object-v4-signed-url Generate a v4 signed URL for downloading an object.
45-
get-object-v4-upload-signed-url Generate a v4 signed URL for uploading an object.
46-
hmac-sa-manage Manage HMAC Service Account keys.
47-
hmac-sa-list List HMAC Service Account keys.
48-
hmac-sa-create Create an HMAC Service Account key.
49-
```
50-
6. Run `php storage.php COMMAND --help` to print information about the usage of each command.
52+
53+
1. Create a service account at the
54+
[Service account section in the Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts/)
55+
56+
1. Download the json key file of the service account.
57+
58+
1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable pointing to that file.
59+
60+
## Samples
61+
62+
To run the Storage Samples, run any of the files in `src/` on the CLI:
63+
64+
```
65+
$ php src/create_bucket.php
66+
67+
Usage: create_bucket.php $bucketName
68+
69+
@param string $projectId The Project ID
70+
@param string $bucketName The Storage bucket name
71+
```
72+
73+
## Troubleshooting
74+
75+
If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID:
76+
77+
```
78+
[Google\Cloud\Core\Exception\GoogleException]
79+
No project ID was provided, and we were unable to detect a default project ID.
80+
```
81+
82+
## The client library
83+
84+
This sample uses the [Google Cloud Client Library for PHP][google-cloud-php].
85+
You can read the documentation for more details on API usage and use GitHub
86+
to [browse the source][google-cloud-php-source] and [report issues][google-cloud-php-issues].
87+
88+
[google-cloud-php]: https://googlecloudplatform.github.io/google-cloud-php
89+
[google-cloud-php-source]: https://github.com/GoogleCloudPlatform/google-cloud-php
90+
[google-cloud-php-issues]: https://github.com/GoogleCloudPlatform/google-cloud-php/issues
91+
[google-cloud-sdk]: https://cloud.google.com/sdk/
92+
5193

5294
## Contributing changes
5395

storage/composer.json

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,7 @@
11
{
22
"require": {
33
"google/cloud-storage": "^1.20.1",
4-
"paragonie/random_compat": "^9.0.0",
5-
"symfony/console": " ^5.0"
6-
},
7-
"autoload": {
8-
"files": [
9-
"src/add_bucket_conditional_iam_binding.php",
10-
"src/add_bucket_iam_member.php",
11-
"src/add_object_acl.php",
12-
"src/copy_object.php",
13-
"src/create_hmac_key.php",
14-
"src/delete_object.php",
15-
"src/delete_object_acl.php",
16-
"src/delete_hmac_key.php",
17-
"src/disable_bucket_lifecycle_management.php",
18-
"src/disable_uniform_bucket_level_access.php",
19-
"src/disable_default_event_based_hold.php",
20-
"src/disable_requester_pays.php",
21-
"src/deactivate_hmac_key.php",
22-
"src/download_file_requester_pays.php",
23-
"src/download_object.php",
24-
"src/enable_bucket_lifecycle_management.php",
25-
"src/enable_uniform_bucket_level_access.php",
26-
"src/enable_default_event_based_hold.php",
27-
"src/enable_requester_pays.php",
28-
"src/activate_hmac_key.php",
29-
"src/generate_v4_post_policy.php",
30-
"src/get_uniform_bucket_level_access.php",
31-
"src/get_object_acl.php",
32-
"src/get_object_acl_for_entity.php",
33-
"src/get_object_v2_signed_url.php",
34-
"src/get_object_v4_signed_url.php",
35-
"src/upload_object_v4_signed_url.php",
36-
"src/get_requester_pays_status.php",
37-
"src/get_retention_policy.php",
38-
"src/get_default_event_based_hold.php",
39-
"src/get_hmac_key.php",
40-
"src/list_objects.php",
41-
"src/list_objects_with_prefix.php",
42-
"src/list_hmac_keys.php",
43-
"src/lock_retention_policy.php",
44-
"src/make_public.php",
45-
"src/move_object.php",
46-
"src/object_metadata.php",
47-
"src/release_event_based_hold.php",
48-
"src/release_temporary_hold.php",
49-
"src/remove_bucket_iam_member.php",
50-
"src/remove_bucket_conditional_iam_binding.php",
51-
"src/remove_retention_policy.php",
52-
"src/set_event_based_hold.php",
53-
"src/set_retention_policy.php",
54-
"src/set_temporary_hold.php",
55-
"src/upload_object.php",
56-
"src/view_bucket_iam_members.php"
57-
]
4+
"paragonie/random_compat": "^9.0.0"
585
},
596
"require-dev": {
607
"guzzlehttp/guzzle": "^7.0"

storage/src/activate_hmac_key.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
/**
3030
* Activate an HMAC key.
3131
*
32+
* @param string $projectId The ID of your Google Cloud Platform project.
3233
* @param string $accessId Access ID for an inactive HMAC key.
33-
* @param string $projectId Google Cloud Project ID.
34-
*
3534
*/
36-
function activate_hmac_key($accessId, $projectId)
35+
function activate_hmac_key($projectId, $accessId)
3736
{
37+
// $projectId = 'my-project-id';
38+
// $accessId = 'GOOG0234230X00';
39+
3840
$storage = new StorageClient();
3941
// By default hmacKey will use the projectId used by StorageClient().
4042
$hmacKey = $storage->hmacKey($accessId, $projectId);
@@ -45,3 +47,7 @@ function activate_hmac_key($accessId, $projectId)
4547
printf('HMAC key Metadata: %s' . PHP_EOL, print_r($hmacKey->info(), true));
4648
}
4749
# [END storage_activate_hmac_key]
50+
51+
// The following 2 lines are only needed to run the samples
52+
require_once __DIR__ . '/../../testing/sample_helpers.php';
53+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/add_bucket_acl.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
/**
3030
* Add an entity and role to a bucket's ACL.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $entity The entity to update access controls for.
34-
* @param string $role The permissions to add for the specified entity. May
35-
* be one of 'OWNER', 'READER', or 'WRITER'.
36-
* @param array $options
37-
*
38-
* @return void
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $entity The entity for which to update access controls.
34+
* @param string $role The permissions to add for the specified entity.
3935
*/
40-
function add_bucket_acl($bucketName, $entity, $role, $options = [])
36+
function add_bucket_acl($bucketName, $entity, $role)
4137
{
38+
// $bucketName = 'my-bucket';
39+
// $entity = '[email protected]';
40+
// $role = 'OWNER';
41+
4242
$storage = new StorageClient();
4343
$bucket = $storage->bucket($bucketName);
4444
$acl = $bucket->acl();
45-
$acl->add($entity, $role, $options);
45+
$acl->add($entity, $role);
4646
printf('Added %s (%s) to gs://%s ACL' . PHP_EOL, $entity, $role, $bucketName);
4747
}
4848
# [END storage_add_bucket_owner]

storage/src/add_bucket_conditional_iam_binding.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,25 @@
2929
/**
3030
* Adds a conditional IAM binding to a bucket's IAM policy.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $role the role that will be given to members in this binding.
34-
* @param string[] $members the member(s) that is associated to this binding.
35-
* @param string $title condition's title
36-
* @param string $description condition's description
37-
* @param string $expression the condition specified in CEL expression language.
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $role The role that will be given to members in this binding.
34+
* @param string[] $members The member(s) associated with this binding.
35+
* @param string $title The title of the condition.
36+
* @param string $description The description of the condition.
37+
* @param string $expression The condition specified in CEL expression language.
3838
*
3939
* To see how to express a condition in CEL, visit:
4040
* @see https://cloud.google.com/storage/docs/access-control/iam#conditions.
41-
*
42-
* @return void
4341
*/
44-
function add_bucket_conditional_iam_binding($bucketName, $role, $members, $title, $description, $expression)
42+
function add_bucket_conditional_iam_binding($bucketName, $role, array $members, $title, $description, $expression)
4543
{
44+
// $bucketName = 'my-bucket';
45+
// $role = 'roles/storage.objectViewer';
46+
// $members = ['group:[email protected]'];
47+
// $title = 'Title';
48+
// $description = 'Condition Description';
49+
// $expression = 'resource.name.startsWith("projects/_/buckets/bucket-name/objects/prefix-a-")';
50+
4651
$storage = new StorageClient();
4752
$bucket = $storage->bucket($bucketName);
4853

@@ -72,3 +77,7 @@ function add_bucket_conditional_iam_binding($bucketName, $role, $members, $title
7277
printf(' Expression: %s' . PHP_EOL, $expression);
7378
}
7479
# [END storage_add_bucket_conditional_iam_binding]
80+
81+
// The following 2 lines are only needed to run the samples
82+
require_once __DIR__ . '/../../testing/sample_helpers.php';
83+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/add_bucket_default_acl.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
/**
3030
* Add an entity and role to a bucket's default ACL.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $entity The entity to update access controls for.
34-
* @param string $role The permissions to add for the specified entity. May
35-
* be one of 'OWNER', 'READER', or 'WRITER'.
36-
* @param array $options
37-
*
38-
* @return void
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $entity The entity for which to update access controls.
34+
* @param string $role The permissions to add for the specified entity.
3935
*/
40-
function add_bucket_default_acl($bucketName, $entity, $role, $options = [])
36+
function add_bucket_default_acl($bucketName, $entity, $role)
4137
{
38+
// $bucketName = 'my-bucket';
39+
// $entity = '[email protected]';
40+
// $role = 'OWNER';
41+
4242
$storage = new StorageClient();
4343
$bucket = $storage->bucket($bucketName);
4444
$acl = $bucket->defaultAcl();
45-
$acl->add($entity, $role, $options);
45+
$acl->add($entity, $role);
4646
printf('Added %s (%s) to gs://%s default ACL' . PHP_EOL, $entity, $role, $bucketName);
4747
}
4848
# [END storage_add_bucket_default_owner]

storage/src/add_bucket_iam_member.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929
/**
3030
* Adds a new member / role IAM pair to a given Cloud Storage bucket.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $role the role you want to add a given member to.
34-
* @param string[] $members the member(s) you want to give the new role for the Cloud
35-
* Storage bucket.
36-
*
37-
* @return void
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $role The role to which the given member should be added.
34+
* @param string[] $members The member(s) to be added to the role.
3835
*/
39-
function add_bucket_iam_member($bucketName, $role, $members)
36+
function add_bucket_iam_member($bucketName, $role, array $members)
4037
{
38+
// $bucketName = 'my-bucket';
39+
// $role = 'roles/storage.objectViewer';
40+
// $members = ['group:[email protected]'];
41+
4142
$storage = new StorageClient();
4243
$bucket = $storage->bucket($bucketName);
4344

@@ -57,3 +58,7 @@ function add_bucket_iam_member($bucketName, $role, $members)
5758
}
5859
}
5960
# [END storage_add_bucket_iam_member]
61+
62+
// The following 2 lines are only needed to run the samples
63+
require_once __DIR__ . '/../../testing/sample_helpers.php';
64+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/src/add_bucket_label.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@
2929
/**
3030
* Adds or updates a bucket label.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $labelName the name of the label to add.
34-
* @param string $labelValue the value of the label to add.
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $labelName The name of the label to add.
34+
* @param string $labelValue The value of the label to add.
3535
*/
3636
function add_bucket_label($bucketName, $labelName, $labelValue)
3737
{
38+
// $bucketName = 'my-bucket';
39+
// $labelName = 'label-key-to-add';
40+
// $labelValue = 'label-value-to-add';
41+
3842
$storage = new StorageClient();
3943
$bucket = $storage->bucket($bucketName);
4044
$newLabels = [$labelName => $labelValue];

storage/src/add_object_acl.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,27 @@
2929
/**
3030
* Add an entity and role to an object's ACL.
3131
*
32-
* @param string $bucketName the name of your Cloud Storage bucket.
33-
* @param string $objectName the name of your Cloud Storage object.
34-
* @param string $entity The entity to update access controls for.
35-
* @param string $role The permissions to add for the specified entity. May
36-
* be one of 'OWNER', 'READER', or 'WRITER'.
37-
* @param array $options
38-
*
39-
* @return void
32+
* @param string $bucketName The name of your Cloud Storage bucket.
33+
* @param string $objectName The name of your Cloud Storage object.
34+
* @param string $entity The entity for which to update access controls.
35+
* @param string $role The permissions to add for the specified entity.
4036
*/
41-
function add_object_acl($bucketName, $objectName, $entity, $role, $options = [])
37+
function add_object_acl($bucketName, $objectName, $entity, $role)
4238
{
39+
// $bucketName = 'my-bucket';
40+
// $objectName = 'my-object';
41+
// $entity = '[email protected]';
42+
// $role = 'OWNER';
43+
4344
$storage = new StorageClient();
4445
$bucket = $storage->bucket($bucketName);
4546
$object = $bucket->object($objectName);
4647
$acl = $object->acl();
47-
$acl->add($entity, $role, $options);
48+
$acl->add($entity, $role);
4849
printf('Added %s (%s) to gs://%s/%s ACL' . PHP_EOL, $entity, $role, $bucketName, $objectName);
4950
}
5051
# [END storage_add_file_owner]
52+
53+
// The following 2 lines are only needed to run the samples
54+
require_once __DIR__ . '/../../testing/sample_helpers.php';
55+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)