Skip to content

Commit 81beca4

Browse files
samples(storage transfer): add samples and test cases for transfer from posix (#3)
2 parents 1acec24 + 375f1e0 commit 81beca4

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
15+
using Google.Cloud.Storage.V1;
16+
using Google.Cloud.StorageTransfer.V1;
17+
using System;
18+
using Xunit.Abstractions;
19+
using Xunit;
20+
21+
22+
namespace StorageTransfer.Samples.Tests;
23+
[Collection(nameof(StorageFixture))]
24+
public class TransferFromPosixTest : IDisposable
25+
{
26+
private readonly StorageFixture _fixture;
27+
private string _transferJobName;
28+
private readonly ITestOutputHelper _outputHelper;
29+
public TransferFromPosixTest(StorageFixture fixture, ITestOutputHelper outputHelper)
30+
{
31+
_fixture = fixture;
32+
_outputHelper = outputHelper;
33+
}
34+
35+
[Fact]
36+
public void TransferFromPosix()
37+
{
38+
TransferFromPosixSample transferFromPosixSample = new TransferFromPosixSample(_outputHelper);
39+
var transferJob = transferFromPosixSample.TransferFromPosix(_fixture.ProjectId, _fixture.SourceAgentPoolName, _fixture.RootDirectory,_fixture.BucketNameSink);
40+
var storage = StorageClient.Create();
41+
Assert.Contains("transferJobs/", transferJob.Name);
42+
_transferJobName = transferJob.Name;
43+
}
44+
45+
public void Dispose()
46+
{
47+
try
48+
{
49+
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
50+
{
51+
ProjectId = _fixture.ProjectId,
52+
JobName = _transferJobName,
53+
TransferJob = new TransferJob()
54+
{
55+
Name = _transferJobName,
56+
Status = TransferJob.Types.Status.Deleted
57+
}
58+
});
59+
}
60+
catch (Exception)
61+
{
62+
// Do nothing, we delete on a best effort basis.
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)