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