|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// [START storagetransfer_transfer_to_nearline] |
| 15 | + |
| 16 | +using Google.Cloud.StorageTransfer.V1; |
| 17 | +using Google.Protobuf.WellKnownTypes; |
| 18 | +using System; |
| 19 | +using Xunit.Abstractions; |
| 20 | + |
| 21 | + |
| 22 | +namespace StorageTransfer.Samples |
| 23 | +{ |
| 24 | + |
| 25 | + public class TransferToNearlineSample |
| 26 | + { |
| 27 | + /*Creates a one-off transfer job that transfers objects from a standard GCS bucket that are more |
| 28 | + than 30 days old to a Nearline GCS bucket.*/ |
| 29 | + private readonly ITestOutputHelper _output; |
| 30 | + public TransferToNearlineSample(ITestOutputHelper output) |
| 31 | + { |
| 32 | + _output = output; |
| 33 | + } |
| 34 | + public TransferJob TransferToNearline( |
| 35 | + // Your Google Cloud Project ID |
| 36 | + string projectId = "my-project-id", |
| 37 | + // The GCS bucket to transfer objects from |
| 38 | + string sourceBucket = "my-source-bucket", |
| 39 | + // The GCS Nearline bucket to transfer old objects to |
| 40 | + string sinkBucket = "my-sink-bucket") |
| 41 | + { |
| 42 | + // A description of this job |
| 43 | + string jobDescription = $"Transfers old objects from standard bucket ({sourceBucket}) that haven't been modified in the last 30 days to a Nearline bucket ({sinkBucket})"; |
| 44 | + |
| 45 | + TransferJob transferJob = new TransferJob |
| 46 | + { |
| 47 | + ProjectId = projectId, |
| 48 | + Description = jobDescription, |
| 49 | + TransferSpec = new TransferSpec |
| 50 | + { |
| 51 | + GcsDataSink = new GcsData { BucketName = sinkBucket }, |
| 52 | + GcsDataSource = new GcsData { BucketName = sourceBucket }, |
| 53 | + ObjectConditions = new ObjectConditions { MinTimeElapsedSinceLastModification = Duration.FromTimeSpan(TimeSpan.FromSeconds(2592000)) }, |
| 54 | + TransferOptions = new TransferOptions { DeleteObjectsFromSourceAfterTransfer = true }, |
| 55 | + }, |
| 56 | + Status = TransferJob.Types.Status.Enabled, |
| 57 | + Schedule = new Schedule { ScheduleStartDate = Google.Type.Date.FromDateTime(System.DateTime.UtcNow.Date.AddMonths(1)), ScheduleEndDate = Google.Type.Date.FromDateTime(System.DateTime.UtcNow.Date.AddMonths(1)) } |
| 58 | + }; |
| 59 | + // Create a Transfer Service client |
| 60 | + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); |
| 61 | + // Create a Transfer job |
| 62 | + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); |
| 63 | + client.RunTransferJob(new RunTransferJobRequest |
| 64 | + { |
| 65 | + JobName = response.Name, |
| 66 | + ProjectId = projectId |
| 67 | + }); |
| 68 | + |
| 69 | + _output.WriteLine($"Created one-off transfer job from standard bucket {sourceBucket} to Nearline bucket {sinkBucket} with the name {response.Name}"); |
| 70 | + return response; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +// [END storagetransfer_transfer_to_nearline] |
0 commit comments