-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(storage transfer): Added samples for storage transfer #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
43ed44d
quickstart and latest transfer check
thiyaguk09 3a999b6
added samples for nearline and combine test cases
thiyaguk09 a24fbc8
Merge pull request #1 from thiyaguk09/missing_transfer_service_samples
thiyaguk09 09bef5e
added samples for manifest and test cases
thiyaguk09 0756ae1
added samples and test cases for posix to GCS
thiyaguk09 5a9b2e8
fixes
thiyaguk09 21b22ce
variables fixes
thiyaguk09 83cd2ef
added samples and test cases for posix to posix
thiyaguk09 2c22adf
added samples and test cases for GCS to posix
thiyaguk09 285330d
Merge pull request #2 from thiyaguk09/sample_manifest
thiyaguk09 646298b
Merge branch 'GoogleCloudPlatform:main' into main
thiyaguk09 a32f6ad
Merge pull request #3 from thiyaguk09/sample_posix
thiyaguk09 f8a30e0
Merge branch 'GoogleCloudPlatform:main' into sample_transfer_between_…
thiyaguk09 9ece009
Merge pull request #4 from thiyaguk09/sample_transfer_between_posix
thiyaguk09 fc988fd
resolve conflict
thiyaguk09 067a654
Merge branch 'main' into sample_download_to_posix
thiyaguk09 d10c311
Merge pull request #5 from thiyaguk09/sample_download_to_posix
thiyaguk09 55ba8e3
added typehints
thiyaguk09 57d3b6e
added samples and test cases for event driven GCS transfer
thiyaguk09 b58b9fb
Merge pull request #6 from thiyaguk09/sample_event_driven_gcs
thiyaguk09 61f4ac5
lint fix
thiyaguk09 232bfa3
Merge branch 'main' into main
danielduhh 4db5442
Merge branch 'main' into main
bshaffer 2248b46
use latest library version
bshaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageTransfer; | ||
|
|
||
| # [START storagetransfer_get_latest_transfer_operation] | ||
| use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; | ||
| use Google\Cloud\StorageTransfer\V1\GetTransferJobRequest; | ||
|
|
||
| /** | ||
| * Checks the latest transfer operation for a given transfer job. | ||
| * | ||
| * @param string $projectId Your Google Cloud project ID. | ||
| * @param string $jobName Storage Transfer Service job name. | ||
| */ | ||
| function check_latest_transfer_operation($projectId, $jobName) | ||
| { | ||
| // $project = 'my-project-id'; | ||
| // $jobName = 'myJob/1234567890'; | ||
| $transferJob = new GetTransferJobRequest([ | ||
| 'project_id' => $projectId, | ||
| 'job_name' => $jobName | ||
| ]); | ||
|
|
||
| $client = new StorageTransferServiceClient(); | ||
| $request = $client->getTransferJob($transferJob); | ||
| $latestOperationName = $request->getLatestOperationName(); | ||
|
|
||
| if($latestOperationName){ | ||
| $transferOperation = $client->getOperationsClient()->getOperation($latestOperationName); | ||
|
|
||
| $operation = $transferOperation->getMetadata(); | ||
|
|
||
| printf('Latest transfer operation for %s is: %s ' . PHP_EOL, $jobName, $operation->serializeToJsonString()); | ||
| } else { | ||
| printf('Transfer job %s has not ran yet.' . PHP_EOL, $jobName); | ||
| } | ||
| } | ||
| # [END storagetransfer_get_latest_transfer_operation] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageTransfer; | ||
|
|
||
| # [START storagetransfer_manifest_request] | ||
|
|
||
| use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; | ||
| use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\GcsData; | ||
| use Google\Cloud\StorageTransfer\V1\PosixFilesystem; | ||
| use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob\Status; | ||
| use Google\Cloud\StorageTransfer\V1\TransferManifest; | ||
| use Google\Cloud\StorageTransfer\V1\TransferSpec; | ||
|
|
||
| /** | ||
| * Creates and runs a transfer from the local file system to the sink bucket | ||
| * | ||
| * @param string $projectId Your Google Cloud project ID. | ||
| * @param string $sourceAgentPoolName The agent pool associated with the POSIX data source. | ||
| * @param string $rootDirectory The root directory path on the source filesystem. | ||
| * @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to. | ||
| * @param string $manifestLocation Transfer manifest location. Must be a `gs:` URL. | ||
| */ | ||
| function manifest_request($projectId, $sourceAgentPoolName, $rootDirectory, $sinkGcsBucketName, $manifestLocation) | ||
| { | ||
| // $project = 'my-project-id'; | ||
| // $sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'; | ||
| // $rootDirectory = '/directory/to/transfer/source'; | ||
| // $sinkGcsBucketName = 'my-sink-bucket'; | ||
| // $manifestLocation = 'gs://my-bucket/sample_manifest.csv'; | ||
| $transferJob = new TransferJob([ | ||
| 'project_id' => $projectId, | ||
| 'transfer_spec' => new TransferSpec([ | ||
| 'source_agent_pool_name' => $sourceAgentPoolName, | ||
| 'posix_data_source' => new PosixFilesystem(['root_directory' => $rootDirectory]), | ||
| 'gcs_data_sink' => new GcsData(['bucket_name' => $sinkGcsBucketName]), | ||
| 'transfer_manifest' => new TransferManifest(['location' => $manifestLocation]) | ||
| ]), | ||
| 'status' => Status::ENABLED | ||
| ]); | ||
|
|
||
| $client = new StorageTransferServiceClient(); | ||
| $createRequest = (new CreateTransferJobRequest()) | ||
| ->setTransferJob($transferJob); | ||
| $response = $client->createTransferJob($createRequest); | ||
| $runRequest = (new RunTransferJobRequest()) | ||
| ->setJobName($response->getName()) | ||
| ->setProjectId($projectId); | ||
| $client->runTransferJob($runRequest); | ||
|
|
||
| printf('Created and ran transfer job from %s to %s using manifest %s with name %s ' . PHP_EOL, $rootDirectory, $sinkGcsBucketName, $manifestLocation, $response->getName()); | ||
| } | ||
| # [END storagetransfer_manifest_request] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageTransfer; | ||
|
|
||
| # [START storagetransfer_transfer_to_nearline] | ||
|
|
||
| use DateTime; | ||
| use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; | ||
| use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\GcsData; | ||
| use Google\Cloud\StorageTransfer\V1\ObjectConditions; | ||
| use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\Schedule; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob\Status; | ||
| use Google\Cloud\StorageTransfer\V1\TransferOptions; | ||
| use Google\Cloud\StorageTransfer\V1\TransferSpec; | ||
| use Google\Protobuf\Duration as ProtobufDuration; | ||
| use Google\Type\Date; | ||
| use Google\Type\TimeOfDay; | ||
|
|
||
| /** | ||
| * Create a daily migration from a GCS bucket to another GCS bucket for objects untouched for 30+ days. | ||
| * | ||
| * @param string $projectId Your Google Cloud project ID. | ||
| * @param string $description A useful description for your transfer job. | ||
| * @param string $sourceGcsBucketName The name of the GCS bucket to transfer objects from. | ||
| * @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to. | ||
| * @param string $startDate Date to start daily migration. | ||
| */ | ||
| function nearline_request($projectId, $description, $sourceGcsBucketName, $sinkGcsBucketName, $startDate) | ||
| { | ||
| // $project = 'my-project-id'; | ||
| // $description = 'My transfer job'; | ||
| // $sourceGcsBucketName = 'my-source-bucket'; | ||
| // $sinkGcsBucketName = 'my-sink-bucket'; | ||
| // $startDate = new DateTime(); | ||
|
|
||
| $dateTime = new DateTime($startDate); | ||
| $date = new Date([ | ||
| 'year' => $dateTime->format('Y'), | ||
| 'month' => $dateTime->format('m') + 1, | ||
| 'day' => $dateTime->format('d'), | ||
| ]); | ||
|
|
||
| $time = new TimeOfDay([ | ||
| 'hours' => $dateTime->format('H'), | ||
| 'minutes' => $dateTime->format('i'), | ||
| 'seconds' => $dateTime->format('s'), | ||
| ]); | ||
|
|
||
| $transferJob = new TransferJob([ | ||
| 'project_id' => $projectId, | ||
| 'description' => $description, | ||
| 'schedule' => new Schedule([ | ||
| 'schedule_start_date' => $date, | ||
| 'start_time_of_day' => $time | ||
| ]), | ||
| 'transfer_spec' => new TransferSpec([ | ||
| 'gcs_data_source' => new GcsData(['bucket_name' => $sourceGcsBucketName]), | ||
| 'gcs_data_sink' => new GcsData(['bucket_name' => $sinkGcsBucketName]), | ||
| 'object_conditions' => new ObjectConditions([ | ||
| 'min_time_elapsed_since_last_modification' => new ProtobufDuration([ | ||
| 'seconds' => 2592000 | ||
| ]) | ||
| ]), | ||
| 'transfer_options' => new TransferOptions(['delete_objects_from_source_after_transfer' => true]) | ||
| ]), | ||
| 'status' => Status::ENABLED | ||
| ]); | ||
|
|
||
| $client = new StorageTransferServiceClient(); | ||
| $createRequest = (new CreateTransferJobRequest()) | ||
| ->setTransferJob($transferJob); | ||
| $response = $client->createTransferJob($createRequest); | ||
| $runRequest = (new RunTransferJobRequest()) | ||
| ->setJobName($response->getName()) | ||
| ->setProjectId($projectId); | ||
| $client->runTransferJob($runRequest); | ||
|
|
||
| printf('Created and ran transfer job : %s' . PHP_EOL, $response->getName()); | ||
| } | ||
| # [END storagetransfer_transfer_to_nearline] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageTransfer; | ||
|
|
||
| # [START storagetransfer_download_to_posix] | ||
|
|
||
| use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; | ||
| use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\GcsData; | ||
| use Google\Cloud\StorageTransfer\V1\PosixFilesystem; | ||
| use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob\Status; | ||
| use Google\Cloud\StorageTransfer\V1\TransferSpec; | ||
|
|
||
| /** | ||
| * Creates a request to transfer from the local file system to the sink bucket | ||
| * | ||
| * @param string $projectId Your Google Cloud project ID. | ||
| * @param string $sinkAgentPoolName The agent pool associated with the POSIX data sink. Defaults to the default agent | ||
| * @param string $gcsSourceBucket Google Cloud Storage source bucket name. | ||
| * @param string $gcsSourcePath An optional path on the Google Cloud Storage bucket to download from. | ||
| * @param string $rootDirectory The root directory path on the destination filesystem. | ||
| */ | ||
| function posix_download($projectId, $sinkAgentPoolName, $gcsSourceBucket, $gcsSourcePath, $rootDirectory) | ||
| { | ||
thiyaguk09 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // $project = 'my-project-id'; | ||
| // $sinkAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'; | ||
| // $gcsSourceBucket = 'my-gcs-source-bucket'; | ||
| // $gcsSourcePath = 'foo/bar/'; | ||
| // $rootDirectory = '/directory/to/transfer/source'; | ||
| $transferJob = new TransferJob([ | ||
| 'project_id' => $projectId, | ||
| 'transfer_spec' => new TransferSpec([ | ||
| 'sink_agent_pool_name' => $sinkAgentPoolName, | ||
| 'gcs_data_source' => new GcsData([ | ||
| 'bucket_name' => $gcsSourceBucket, | ||
| 'path' => $gcsSourcePath | ||
| ]), | ||
| 'posix_data_sink' => new PosixFilesystem(['root_directory' => $rootDirectory]) | ||
| ]), | ||
| 'status' => Status::ENABLED | ||
| ]); | ||
|
|
||
| $client = new StorageTransferServiceClient(); | ||
| $createRequest = (new CreateTransferJobRequest()) | ||
| ->setTransferJob($transferJob); | ||
| $response = $client->createTransferJob($createRequest); | ||
| $runRequest = (new RunTransferJobRequest()) | ||
| ->setJobName($response->getName()) | ||
| ->setProjectId($projectId); | ||
| $client->runTransferJob($runRequest); | ||
|
|
||
| printf('Created and ran a transfer job from %s to %s with name %s ' . PHP_EOL, $gcsSourcePath, $rootDirectory, $response->getName()); | ||
| } | ||
| # [END storagetransfer_download_to_posix] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <?php | ||
| /** | ||
| * Copyright 2024 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| namespace Google\Cloud\Samples\StorageTransfer; | ||
|
|
||
| # [START storagetransfer_transfer_from_posix] | ||
|
|
||
| use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; | ||
| use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\GcsData; | ||
| use Google\Cloud\StorageTransfer\V1\PosixFilesystem; | ||
| use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob; | ||
| use Google\Cloud\StorageTransfer\V1\TransferJob\Status; | ||
| use Google\Cloud\StorageTransfer\V1\TransferSpec; | ||
|
|
||
| /** | ||
| * Creates and runs a transfer from the local file system to the sink bucket | ||
| * | ||
| * @param string $projectId Your Google Cloud project ID. | ||
| * @param string $sourceAgentPoolName The agent pool associated with the POSIX data source. | ||
| * @param string $rootDirectory The root directory path on the source filesystem. | ||
| * @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to. | ||
| */ | ||
| function posix_request($projectId, $sourceAgentPoolName, $rootDirectory, $sinkGcsBucketName) | ||
| { | ||
| // $project = 'my-project-id'; | ||
| // $sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'; | ||
| // $rootDirectory = '/directory/to/transfer/source'; | ||
| // $sinkGcsBucketName = 'my-sink-bucket'; | ||
| $transferJob = new TransferJob([ | ||
| 'project_id' => $projectId, | ||
| 'transfer_spec' => new TransferSpec([ | ||
| 'source_agent_pool_name' => $sourceAgentPoolName, | ||
| 'posix_data_source' => new PosixFilesystem(['root_directory' => $rootDirectory]), | ||
| 'gcs_data_sink' => new GcsData(['bucket_name' => $sinkGcsBucketName]) | ||
| ]), | ||
| 'status' => Status::ENABLED | ||
| ]); | ||
|
|
||
| $client = new StorageTransferServiceClient(); | ||
| $createRequest = (new CreateTransferJobRequest()) | ||
| ->setTransferJob($transferJob); | ||
| $response = $client->createTransferJob($createRequest); | ||
| $runRequest = (new RunTransferJobRequest()) | ||
| ->setJobName($response->getName()) | ||
| ->setProjectId($projectId); | ||
| $client->runTransferJob($runRequest); | ||
|
|
||
| printf('Created and ran transfer job from %s to %s with name %s ' . PHP_EOL, $rootDirectory, $sinkGcsBucketName, $response->getName()); | ||
| } | ||
| # [END storagetransfer_transfer_from_posix] | ||
|
|
||
| // The following 2 lines are only needed to run the samples | ||
| require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
| \Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.