Skip to content

Commit 7357db6

Browse files
samples(storage transfer): add sample and test case for storage transfer services. (#4)
2 parents ce4a089 + 139190a commit 7357db6

File tree

3 files changed

+154
-1
lines changed

3 files changed

+154
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
using System.Text;
21+
using System.IO;
22+
23+
24+
namespace StorageTransfer.Samples.Tests;
25+
[Collection(nameof(StorageFixture))]
26+
public class DownloadToPosixTest : IDisposable
27+
{
28+
private readonly StorageFixture _fixture;
29+
private string _transferJobName;
30+
private readonly ITestOutputHelper _outputHelper;
31+
public DownloadToPosixTest(StorageFixture fixture, ITestOutputHelper outputHelper)
32+
{
33+
_fixture = fixture;
34+
_outputHelper = outputHelper;
35+
}
36+
37+
[Fact]
38+
public void DownloadToPosix()
39+
{
40+
DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample(_outputHelper);
41+
var storage = StorageClient.Create();
42+
byte[] byteArray = Encoding.UTF8.GetBytes("flower.jpeg");
43+
MemoryStream stream = new MemoryStream(byteArray);
44+
storage.UploadObject(_fixture.BucketNameSource,"DownloadToPosixTestFile", "application/octet-stream", stream);
45+
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId,_fixture.SinkAgentPoolName,_fixture.BucketNameSource,_fixture.GcsSourcePath,_fixture.RootDirectory);
46+
Assert.Contains("transferJobs/", transferJob.Name);
47+
_transferJobName = transferJob.Name;
48+
}
49+
50+
public void Dispose()
51+
{
52+
try
53+
{
54+
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
55+
{
56+
ProjectId = _fixture.ProjectId,
57+
JobName = _transferJobName,
58+
TransferJob = new TransferJob()
59+
{
60+
Name = _transferJobName,
61+
Status = TransferJob.Types.Status.Deleted
62+
}
63+
});
64+
}
65+
catch (Exception)
66+
{
67+
// Do nothing, we delete on a best effort basis.
68+
}
69+
}
70+
}

storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class StorageFixture : IDisposable, ICollectionFixture<StorageFixture>
3030
public string BucketNameSink { get; } = Guid.NewGuid().ToString();
3131
public string JobName { get; }
3232
public string SourceAgentPoolName { get; }
33+
public string SinkAgentPoolName { get; }
34+
public string GcsSourcePath { get;}
3335
public string RootDirectory { get; } = "/tmp/uploads";
3436
public StorageClient Storage { get; } = StorageClient.Create();
3537
public string ManifestObjectName { get; } = "manifest.csv";
@@ -41,7 +43,9 @@ public StorageFixture()
4143
Random random = new Random();
4244
JobName = "transferJobs/" + random.NextInt64(1000000000000000, 9223372036854775807) + " ";
4345
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
44-
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/test_dotnet";
46+
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/source_test_dotnet";
47+
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/sink_test_dotnet";
48+
GcsSourcePath = "foo/bar/";
4549
if (string.IsNullOrWhiteSpace(ProjectId))
4650
{
4751
throw new Exception("You need to set the Environment variable 'GOOGLE_PROJECT_ID' with your Google Cloud Project's project id.");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)