Skip to content

Commit 4b2c306

Browse files
feat(StorageTransfer): add a quickstart sample (GoogleCloudPlatform#1548)
1 parent a2a625e commit 4b2c306

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

storagetransfer/composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"require": {
3+
"google/cloud-storage-transfer": "0.1.1",
4+
"paragonie/random_compat": "^9.0.0"
5+
},
6+
"require-dev": {
7+
"google/cloud-storage": "^1.20.1"
8+
}
9+
}

storagetransfer/phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<phpunit bootstrap="../testing/bootstrap.php">
2+
<testsuites>
3+
<testsuite name="PHP storagetransfer test">
4+
<directory>testdirectory>
5+
testsuite>
6+
testsuites>
7+
<logging>
8+
<log type="coverage-clover" target="build/logs/clover.xml"/>
9+
logging>
10+
<filter>
11+
<whitelist>
12+
<directory suffix=".php">./srcdirectory>
13+
<exclude>
14+
<directory>./vendordirectory>
15+
exclude>
16+
whitelist>
17+
filter>
18+
<php>
19+
<env name="PHPUNIT_TESTS" value="1"/>
20+
php>
21+
phpunit>

storagetransfer/src/quickstart.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\StorageTransfer;
19+
20+
# [START storagetransfer_quickstart]
21+
use Google\Cloud\StorageTransfer\V1\StorageTransferServiceClient;
22+
use Google\Cloud\StorageTransfer\V1\TransferJob;
23+
use Google\Cloud\StorageTransfer\V1\TransferJob\Status;
24+
use Google\Cloud\StorageTransfer\V1\TransferSpec;
25+
use Google\Cloud\StorageTransfer\V1\GcsData;
26+
27+
/**
28+
* Creates and runs a tranfser job between two GCS buckets
29+
*
30+
* @param string $projectId Your Google Cloud project ID.
31+
* @param string $sourceGcsBucketName The name of the GCS bucket to transfer objects from.
32+
* @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to.
33+
*/
34+
function quickstart($projectId, $sourceGcsBucketName, $sinkGcsBucketName)
35+
{
36+
// $project = 'my-project-id';
37+
// $sourceGcsBucketName = 'my-source-bucket';
38+
// $sinkGcsBucketName = 'my-sink-bucket';
39+
$transferJob = new TransferJob([
40+
'project_id' => $projectId,
41+
'transfer_spec' => new TransferSpec([
42+
'gcs_data_sink' => new GcsData(['bucket_name' => $sourceGcsBucketName]),
43+
'gcs_data_source' => new GcsData(['bucket_name' => $sourceGcsBucketName])
44+
]),
45+
'status' => Status::ENABLED
46+
]);
47+
48+
$client = new StorageTransferServiceClient();
49+
$response = $client->createTransferJob($transferJob);
50+
$client->runTransferJob($response->getName(), $projectId);
51+
52+
printf('Created and ran transfer job from %s to %s with name %s ' . PHP_EOL, $sourceGcsBucketName, $sinkGcsBucketName, $response->getName());
53+
}
54+
# [END storagetransfer_quickstart]
55+
56+
// The following 2 lines are only needed to run the samples
57+
require_once __DIR__ . '/../../testing/sample_helpers.php';
58+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
/**
3+
* Copyright 2021 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\StorageTransfer;
19+
20+
use Google\Cloud\Storage\StorageClient;
21+
use Google\Cloud\StorageTransfer\V1\StorageTransferServiceClient;
22+
use Google\Cloud\StorageTransfer\V1\TransferJob\Status;
23+
use Google\Cloud\TestUtils\TestTrait;
24+
use Google\Cloud\StorageTransfer\V1\TransferJob;
25+
use PHPUnit\Framework\TestCase;
26+
27+
class StorageTransferTest extends TestCase
28+
{
29+
use TestTrait;
30+
31+
private static $sourceBucket;
32+
private static $sinkBucket;
33+
private static $storage;
34+
private static $sts;
35+
36+
public static function setUpBeforeClass(): void
37+
{
38+
self::checkProjectEnvVars();
39+
self::$storage = new StorageClient();
40+
self::$sts = new StorageTransferServiceClient();
41+
$uniqueBucketId = time() . rand();
42+
self::$sourceBucket = self::$storage->createBucket(
43+
sprintf('php-source-bucket-%s', $uniqueBucketId)
44+
);
45+
self::$sinkBucket = self::$storage->createBucket(
46+
sprintf('php-sink-bucket-%s', $uniqueBucketId)
47+
);
48+
49+
self::grantStsPermissions(self::$sourceBucket);
50+
self::grantStsPermissions(self::$sinkBucket);
51+
}
52+
53+
public static function tearDownAfterClass(): void
54+
{
55+
self::$sourceBucket->delete();
56+
self::$sinkBucket->delete();
57+
}
58+
59+
public function testQuickstart()
60+
{
61+
$output = $this->runFunctionSnippet('quickstart', [
62+
self::$projectId, self::$sinkBucket->name(), self::$sourceBucket->name()
63+
]);
64+
65+
$this->assertRegExp('/transferJobs\/.*/', $output);
66+
67+
preg_match('/transferJobs\/\d+/', $output, $match);
68+
$jobName = $match[0];
69+
$transferJob = new TransferJob([
70+
'name' => $jobName,
71+
'status' => Status::DELETED
72+
]);
73+
74+
self::$sts->updateTransferJob($jobName, self::$projectId, $transferJob);
75+
}
76+
77+
private static function grantStsPermissions($bucket)
78+
{
79+
$googleServiceAccount = self::$sts->getGoogleServiceAccount(self::$projectId);
80+
$email = $googleServiceAccount->getAccountEmail();
81+
$members = ['serviceAccount:' . $email];
82+
83+
$policy = $bucket->iam()->policy(['requestedPolicyVersion' => 3]);
84+
$policy['version'] = 3;
85+
86+
array_push(
87+
$policy['bindings'],
88+
['role' => 'roles/storage.objectViewer', 'members' => $members],
89+
['role' => 'roles/storage.legacyBucketReader', 'members' => $members],
90+
['role' => 'roles/storage.legacyBucketWriter', 'members' => $members]
91+
);
92+
93+
$bucket->iam()->setPolicy($policy);
94+
}
95+
}

0 commit comments

Comments
 (0)