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