Skip to content

Commit

Permalink
samples(storage transfer): code changes in logic for creation of temp…
Browse files Browse the repository at this point in the history
… folder in posix file system (#5)
  • Loading branch information
mahendra-google authored Nov 6, 2024
2 parents 7357db6 + 526b445 commit 8a06504
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using Xunit;
using System.Text;
using System.IO;
using System.Linq;
using System.Threading.Tasks;


namespace StorageTransfer.Samples.Tests;
Expand All @@ -38,12 +40,18 @@ public DownloadToPosixTest(StorageFixture fixture, ITestOutputHelper outputHelpe
public void DownloadToPosix()
{
DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample(_outputHelper);
Directory.CreateDirectory(_fixture.TempDirectory);
var storage = StorageClient.Create();
byte[] byteArray = Encoding.UTF8.GetBytes("flower.jpeg");
MemoryStream stream = new MemoryStream(byteArray);
storage.UploadObject(_fixture.BucketNameSource,"DownloadToPosixTestFile", "application/octet-stream", stream);
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId,_fixture.SinkAgentPoolName,_fixture.BucketNameSource,_fixture.GcsSourcePath,_fixture.RootDirectory);
string fileName = $"{_fixture.GcsSourcePath}{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt";
string filePath = $"{_fixture.TempDirectory}/{fileName.Split('/').Last()}";
storage.UploadObject(_fixture.BucketNameSource,fileName, "application/octet-stream", stream);
var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId,_fixture.SinkAgentPoolName,_fixture.BucketNameSource,_fixture.GcsSourcePath,_fixture.TempDirectory);
Assert.Contains("transferJobs/", transferJob.Name);
Assert.True( Directory.Exists(_fixture.TempDirectory));
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(45));
Assert.True( File.Exists(filePath));
_transferJobName = transferJob.Name;
}

Expand All @@ -61,6 +69,7 @@ public void Dispose()
Status = TransferJob.Types.Status.Deleted
}
});
Directory.Delete(_fixture.TempDirectory, true);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using Google.Cloud.StorageTransfer.V1;
Expand All @@ -32,7 +33,8 @@ public class StorageFixture : IDisposable, ICollectionFixture<StorageFixture>
public string SourceAgentPoolName { get; }
public string SinkAgentPoolName { get; }
public string GcsSourcePath { get;}
public string RootDirectory { get; } = "/tmp/uploads";
public string RootDirectory { get; } = System.IO.Path.GetTempPath();
public string TempDirectory { get; } = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
public StorageClient Storage { get; } = StorageClient.Create();
public string ManifestObjectName { get; } = "manifest.csv";
public StorageTransferServiceClient Sts { get; } = StorageTransferServiceClient.Create();
Expand All @@ -43,8 +45,8 @@ public StorageFixture()
Random random = new Random();
JobName = "transferJobs/" + random.NextInt64(1000000000000000, 9223372036854775807) + " ";
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/source_test_dotnet";
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/sink_test_dotnet";
SourceAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
SinkAgentPoolName = "projects/" + ProjectId + "/agentPools/transfer_service_default";
GcsSourcePath = "foo/bar/";
if (string.IsNullOrWhiteSpace(ProjectId))
{
Expand Down

0 comments on commit 8a06504

Please sign in to comment.