|
| 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_from_posix] |
| 15 | +using Google.Cloud.StorageTransfer.V1; |
| 16 | +using Xunit.Abstractions; |
| 17 | + |
| 18 | + |
| 19 | +namespace StorageTransfer.Samples |
| 20 | +{ |
| 21 | + public class TransferFromPosixSample |
| 22 | + { |
| 23 | + /*Create a transfer from a POSIX file system to a GCS sink bucket*/ |
| 24 | + private readonly ITestOutputHelper _output; |
| 25 | + public TransferFromPosixSample(ITestOutputHelper output) |
| 26 | + { |
| 27 | + _output = output; |
| 28 | + } |
| 29 | + public TransferJob TransferFromPosix( |
| 30 | + // Your Google Cloud Project ID |
| 31 | + string projectId = "my-project-id", |
| 32 | + // The agent pool associated with the POSIX data source. If not provided, defaults to the default agent |
| 33 | + string sourceAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", |
| 34 | + // The root directory path on the source filesystem |
| 35 | + string rootDirectory = "/tmp/uploads", |
| 36 | + // The GCS bucket to transfer data to |
| 37 | + string sinkBucket = "my-sink-bucket") |
| 38 | + { |
| 39 | + |
| 40 | + // # A useful description for your transfer job |
| 41 | + string jobDescription = $"Transfers objects from a POSIX file system to a sink bucket ({sinkBucket})"; |
| 42 | + |
| 43 | + TransferJob transferJob = new TransferJob |
| 44 | + { |
| 45 | + ProjectId = projectId, |
| 46 | + Description = jobDescription, |
| 47 | + TransferSpec = new TransferSpec |
| 48 | + { |
| 49 | + GcsDataSink = new GcsData { BucketName = sinkBucket }, |
| 50 | + SourceAgentPoolName = sourceAgentPoolName, |
| 51 | + PosixDataSource = new PosixFilesystem { RootDirectory = rootDirectory } |
| 52 | + }, |
| 53 | + Status = TransferJob.Types.Status.Enabled, |
| 54 | + }; |
| 55 | + |
| 56 | + |
| 57 | + // Create a Transfer Service client |
| 58 | + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); |
| 59 | + |
| 60 | + // Create a Transfer job |
| 61 | + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); |
| 62 | + |
| 63 | + client.RunTransferJob(new RunTransferJobRequest |
| 64 | + { |
| 65 | + JobName = response.Name, |
| 66 | + ProjectId = projectId |
| 67 | + }); |
| 68 | + |
| 69 | + _output.WriteLine($"Created and ran transfer job from {rootDirectory} to {sinkBucket} with the name {response.Name}"); |
| 70 | + return response; |
| 71 | + |
| 72 | + |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +// [END storagetransfer_transfer_from_posix] |
| 77 | + |
| 78 | + |
0 commit comments