I'm trying to download some files from google cloud storage (log files from a google play published app).
My code looks like this
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "my-service-account-credential.json", EnvironmentVariableTarget.Process);
StorageClient storageClient = StorageClient.Create();
var bucketName = "mybucketname";
var buckets = storageClient.GetBucket(bucketName);
var objects = storageClient.ListObjects(bucketName).ToList();
foreach (var o in objects)
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(o.Name));
using (var fs = File.Open(o.Name, FileMode.OpenOrCreate))
{
await storageClient.DownloadObjectAsync(bucketName, o.Name, fs);
}
}
catch (Exception e)
{
if (e.Message.StartsWith("Incorrect hash"))
{
continue;
}
throw;
}
}
The code actually seems to work fine (judging by looking at the actual downloaded file content, it is csv files). But as you can see I have implemented a nasty try catch / hack because every file I download throws an exception stating that the hash is incorrect. I'm assuming the client library compare the hash of the downloaded content with the hash of the bucket and these are not identical resulting it an exception.
The exception is:
System.IO.IOException: Incorrect hash: expected 'DXpVGw==' (base64), was '2RMrcw==' (base64)
at Google.Cloud.Storage.V1.StorageClientImpl.<DownloadObjectAsyncImpl>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MyClass.GoogleBucket.Functions.<DownloadGoogleBucketLogs>d__1.MoveNext() in mycode.cs:line 51
So my question is how do you download objects, without getting this exception, clearly one is not supposed to do what I have done.