|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 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_transfer_to_nearline] |
| 21 | + |
| 22 | +use DateTime; |
| 23 | +use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient; |
| 24 | +use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest; |
| 25 | +use Google\Cloud\StorageTransfer\V1\GcsData; |
| 26 | +use Google\Cloud\StorageTransfer\V1\ObjectConditions; |
| 27 | +use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest; |
| 28 | +use Google\Cloud\StorageTransfer\V1\Schedule; |
| 29 | +use Google\Cloud\StorageTransfer\V1\TransferJob; |
| 30 | +use Google\Cloud\StorageTransfer\V1\TransferJob\Status; |
| 31 | +use Google\Cloud\StorageTransfer\V1\TransferOptions; |
| 32 | +use Google\Cloud\StorageTransfer\V1\TransferSpec; |
| 33 | +use Google\Protobuf\Duration as ProtobufDuration; |
| 34 | +use Google\Type\Date; |
| 35 | +use Google\Type\TimeOfDay; |
| 36 | + |
| 37 | +/** |
| 38 | + * Create a daily migration from a GCS bucket to another GCS bucket for objects untouched for 30+ days. |
| 39 | + * |
| 40 | + * @param string $projectId Your Google Cloud project ID. |
| 41 | + * @param string $description A useful description for your transfer job. |
| 42 | + * @param string $sourceGcsBucketName The name of the GCS bucket to transfer objects from. |
| 43 | + * @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to. |
| 44 | + * @param string $startDate Date to start daily migration. |
| 45 | + */ |
| 46 | +function nearline_request( |
| 47 | + string $projectId, |
| 48 | + string $description, |
| 49 | + string $sourceGcsBucketName, |
| 50 | + string $sinkGcsBucketName, |
| 51 | + string $startDate |
| 52 | +): void { |
| 53 | + // $project = 'my-project-id'; |
| 54 | + // $description = 'My transfer job'; |
| 55 | + // $sourceGcsBucketName = 'my-source-bucket'; |
| 56 | + // $sinkGcsBucketName = 'my-sink-bucket'; |
| 57 | + // $startDate = new DateTime(); |
| 58 | + |
| 59 | + $dateTime = new DateTime($startDate); |
| 60 | + $date = new Date([ |
| 61 | + 'year' => $dateTime->format('Y'), |
| 62 | + 'month' => $dateTime->format('m'), |
| 63 | + 'day' => $dateTime->format('d'), |
| 64 | + ]); |
| 65 | + |
| 66 | + $time = new TimeOfDay([ |
| 67 | + 'hours' => $dateTime->format('H'), |
| 68 | + 'minutes' => $dateTime->format('i'), |
| 69 | + 'seconds' => $dateTime->format('s'), |
| 70 | + ]); |
| 71 | + |
| 72 | + $transferJob = new TransferJob([ |
| 73 | + 'project_id' => $projectId, |
| 74 | + 'description' => $description, |
| 75 | + 'schedule' => new Schedule([ |
| 76 | + 'schedule_start_date' => $date, |
| 77 | + 'start_time_of_day' => $time |
| 78 | + ]), |
| 79 | + 'transfer_spec' => new TransferSpec([ |
| 80 | + 'gcs_data_source' => new GcsData(['bucket_name' => $sourceGcsBucketName]), |
| 81 | + 'gcs_data_sink' => new GcsData(['bucket_name' => $sinkGcsBucketName]), |
| 82 | + 'object_conditions' => new ObjectConditions([ |
| 83 | + 'min_time_elapsed_since_last_modification' => new ProtobufDuration([ |
| 84 | + 'seconds' => 2592000 |
| 85 | + ]) |
| 86 | + ]), |
| 87 | + 'transfer_options' => new TransferOptions(['delete_objects_from_source_after_transfer' => true]) |
| 88 | + ]), |
| 89 | + 'status' => Status::ENABLED |
| 90 | + ]); |
| 91 | + |
| 92 | + $client = new StorageTransferServiceClient(); |
| 93 | + $createRequest = (new CreateTransferJobRequest()) |
| 94 | + ->setTransferJob($transferJob); |
| 95 | + $response = $client->createTransferJob($createRequest); |
| 96 | + $runRequest = (new RunTransferJobRequest()) |
| 97 | + ->setJobName($response->getName()) |
| 98 | + ->setProjectId($projectId); |
| 99 | + $client->runTransferJob($runRequest); |
| 100 | + |
| 101 | + printf('Created and ran transfer job : %s' . PHP_EOL, $response->getName()); |
| 102 | +} |
| 103 | +# [END storagetransfer_transfer_to_nearline] |
| 104 | + |
| 105 | +// The following 2 lines are only needed to run the samples |
| 106 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 107 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments