0

I have been trying to upload and download files to and from google cloud .

    public static class Program
    {
        public static void Main(string[] args)
        {
           

            string bucketName = "uploads";

            // Explicitly use service account credentials by specifying the private key file.
            GoogleCredential credential = null;
            using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                credential = GoogleCredential.FromStream(jsonStream);
            }
            var storageClient = StorageClient.Create(credential);

            //using (var fileStream = new FileStream("C:\\...", FileMode.Open,
            //    FileAccess.Read, FileShare.Read))
            //{
            //    storageClient.UploadObject(bucketName, "file1", "text/plain", fileStream);
            //}
            //List objects
            foreach (var obj in storageClient.ListObjects(bucketName, ""))
            {
                Console.WriteLine(obj.Name);
            }

            // Download file
            using (var fileStream = File.Create("C:\\...))
            
            {
                storageClient.DownloadObject(bucketName, "myfile.csv", fileStream);

It works fine with uploading and showing what files i have in the cloud bucket. But i am getting this error for downloading files.Unhandled Exception: Google.GoogleApiException: The service storage has thrown an exception. HttpStatusCode is NotFound. No error message was specified. ---> Newtonsoft.Json.JsonReaderException: Error parsing NaN value. Path '', line 1, position 1. ERROR

Any possible solution ?

I tried looking up if that happened to anyone but couldn't find anything.

5
  • Please provide the file stack trace as text in the question - we can't tell which operation has failed at the moment. Commented Apr 20, 2023 at 13:40
  • Fundamentally though, it sounds like you're trying to download a file that doesn't exist. (The JSON parsing error is unexpected, admittedly.) Commented Apr 20, 2023 at 13:41
  • The Path '', line 1, position 1 in the error generally means ‘this isn’t Json’ but yes, why it should try and parse a 404 is.. odd. Commented Apr 20, 2023 at 13:43
  • .. unless I suppose it’s trying to parse an error message expressed as Json, which DeserializeErrorAsync suggests might be the case Commented Apr 20, 2023 at 13:45
  • @stuartd: Yes, most of the time API errors do come with associated JSON. Sometimes it's HTML though, and sometimes there's nothing. (And it's hard to predicate that, so we try to parse everything.) I'm not sure what input is causing Json.NET to think it's a NaN though. Commented Apr 20, 2023 at 14:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.