Reputation: 126455
I am able to download an object from my bucket
var bucket_name = "digital-jorgesys-454.appspot.com";
var object_name = "earnings_202107_212121-10.zip";
but now I am trying to download a file inside a directory
var bucket_name = "digital-jorgesys-454.appspot.com";
var object_name = "/earnings/earnings_202107_212121-10.zip";
I get only the message:
Exception thrown: 'Google.GoogleApiException' in mscorlib.dll
I have the required permissions, this is my Code:
private async Task DownloadFromBucket()
{
//To get Service Account keys go to : https://console.cloud.google.com/iam-admin/serviceaccounts
var GCSServiceAccountEmail = "google-financial-reports@digital-jorgesys-454.iam.gserviceaccount.com";
var P12KeyFullPath = @"C:\Data\jorgesys\GoogleFinancialReport\digital-jorgesys-454-f4e36f8fabcde.p12";
var P12KeySecret = "itsmysecret";
Console.WriteLine($"*** \" async Task DownloadFromBucket()()\" ....");
var bucket_name = "digital-jorgesys-454.appspot.com";
var object_name = "earnings/earnings_202107_2138916436889858-70.zip";
try
{
_ = DownloadFile(GCSServiceAccountEmail, P12KeyFullPath, P12KeySecret, bucket_name, object_name);
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("DownloadFile ERROR: " + e.Message);
}
}
}
private async Task DownloadFile(string GCSServiceAccountEmail, string P12KeyFullPath, string P12KeySecret, string GCSBucketName, string filePathInGcs)
{
try {
const string LocalDownloadPath = @"C:\Data\jorgesys\GoogleFinancialReport\";
const string GCSApplicationName = "Jorgesys";
var certificate
= new X509Certificate2(P12KeyFullPath, P12KeySecret,
X509KeyStorageFlags.MachineKeySet |
X509KeyStorageFlags.PersistKeySet |
X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(GCSServiceAccountEmail)
{
Scopes = new[] { StorageService.Scope.DevstorageReadWrite }
}.FromCertificate(certificate));
var service = new StorageService(
new BaseClientService.Initializer
{
ApplicationName = GCSApplicationName,
HttpClientInitializer = credential
}
);
var fileRequest = new ObjectsResource.GetRequest(service, GCSBucketName, filePathInGcs);
var fileMetaDataObj = fileRequest.Execute();
fileRequest.MediaDownloader.ProgressChanged += DownloadProgress;
fileRequest.MediaDownloader.ChunkSize = (256 * 1024);
var downloadStream =
new FileStream(Path.Combine(LocalDownloadPath, filePathInGcs), FileMode.Create, FileAccess.Write);
await fileRequest.MediaDownloader.DownloadAsync(fileMetaDataObj.MediaLink, downloadStream, CancellationToken.None);
service.Dispose();
}
catch(Exception e)
{
Console.WriteLine("DownloadFile() Error: " + e.Message);
}
}
Upvotes: 0
Views: 766
Reputation: 126455
Well, trying to get more info about this issue I found the problem, first this is one error:
No such object: digital-jorgesys-454.appspot.com//earnings/earnings_202107_2138916436889858-70.zip [404] Errors [ Message[No such object: digital-jorgesys-454.appspot.com//earnings/earnings_202107_212121-10.zip] Location[ - ] Reason[notFound] Domain[global] ]
The object was not found because a separator "/" was added automatically, so I made this change:
var bucket_name = "digital-jorgesys-454.appspot.com";
//var object_name = "/earnings/earnings_202107_212121-10.zip";
var object_name = "earnings/earnings_202107_212121-10.zip";
And now the path to access the object is correct:
digital-jorgesys-454.appspot.com/earnings/earnings_202107_212121-10.zip
Then, to download the file the path is incorrect (
Exception thrown: 'System.IO.DirectoryNotFoundException' in mscorlib.dll
) because I need a similar structure to download the file inside the same directory, so we must be sure that the download path must really contain the \earnings\
directory.
const string LocalDownloadPath = @"C:\Data\jorgesys\GoogleFinancialReport\";
Upvotes: 1