Skip to content

Commit 8a06504

Browse files
samples(storage transfer): code changes in logic for creation of temp folder in posix file system (#5)
2 parents 7357db6 + 526b445 commit 8a06504

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
using Xunit;
2020
using System.Text;
2121
using System.IO;
22+
using System.Linq;
23+
using System.Threading.Tasks;
2224

2325

2426
namespace StorageTransfer.Samples.Tests;
@@ -38,12 +40,18 @@ public DownloadToPosixTest(StorageFixture fixture, ITestOutputHelper outputHelpe
3840
public void DownloadToPosix()
3941
{
4042
DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample(_outputHelper);
43+
Directory.CreateDirectory(_fixture.TempDirectory);
4144
var storage = StorageClient.Create();
4245
byte[] byteArray = Encoding.UTF8.GetBytes("flower.jpeg");
4346
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);
47+
string fileName = $"{_fixture.GcsSourcePath}{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
48+
string filePath = $"{_fixture.TempDirectory}/{fileName.Split('/').Last()}";
49+
storage.UploadObject(_fixture.BucketNameSource,fileName, "application/octet-stream", stream);
50+
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId,_fixture.SinkAgentPoolName,_fixture.BucketNameSource,_fixture.GcsSourcePath,_fixture.TempDirectory);
4651
Assert.Contains("transferJobs/", transferJob.Name);
52+
Assert.True( Directory.Exists(_fixture.TempDirectory));
53+
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(45));
54+
Assert.True( File.Exists(filePath));
4755
_transferJobName = transferJob.Name;
4856
}
4957

@@ -61,6 +69,7 @@ public void Dispose()
6169
Status = TransferJob.Types.Status.Deleted
6270
}
6371
});
72+
Directory.Delete(_fixture.TempDirectory, true);
6473
}
6574
catch (Exception)
6675
{

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.IO;
1920
using Google.Apis.Storage.v1.Data;
2021
using Google.Cloud.Storage.V1;
2122
using Google.Cloud.StorageTransfer.V1;
@@ -32,7 +33,8 @@ public class StorageFixture : IDisposable, ICollectionFixture<StorageFixture>
3233
public string SourceAgentPoolName { get; }
3334
public string SinkAgentPoolName { get; }
3435
public string GcsSourcePath { get;}
35-
public string RootDirectory { get; } = "/tmp/uploads";
36+
public string RootDirectory { get; } = System.IO.Path.GetTempPath();
37+
public string TempDirectory { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
3638
public StorageClient Storage { get; } = StorageClient.Create();
3739
public string ManifestObjectName { get; } = "manifest.csv";
3840
public StorageTransferServiceClient Sts { get; } = StorageTransferServiceClient.Create();
@@ -43,8 +45,8 @@ public StorageFixture()
4345
Random random = new Random();
4446
JobName = "transferJobs/" + random.NextInt64(1000000000000000, 9223372036854775807) + " ";
4547
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
46-
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/source_test_dotnet";
47-
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/sink_test_dotnet";
48+
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
49+
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
4850
GcsSourcePath = "foo/bar/";
4951
if (string.IsNullOrWhiteSpace(ProjectId))
5052
{

0 commit comments

Comments
 (0)