Skip to content

Instantly share code, notes, and snippets.

@henrikj242
Last active January 29, 2024 20:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikj242/af06ac41fc9554dab387c0bb3a994f85 to your computer and use it in GitHub Desktop.
Save henrikj242/af06ac41fc9554dab387c0bb3a994f85 to your computer and use it in GitHub Desktop.
Use the Google Cloud API to upload and download files to/from a bucket using explicit authentication in a C# app.
using Google.Apis.Auth.OAuth2; // Google.Apis.Auth --version 1.30.0
using Google.Cloud.Storage.V1; // Google.Cloud.Storage.V1
using System;
using System.IO;
using System.Runtime.InteropServices;
using static System.Console;
public static class Program
{
public static void Main(string[] args)
{
string message = "Dot Net example: Upload file to Google Cloud Bucket. And also download";
string bucketName = "my-bucket";
// Explicitly use service account credentials by specifying the private key file.
// The service account should have Object Manage permissions for the bucket.
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("Program.cs", FileMode.Open,
FileAccess.Read, FileShare.Read))
{
storageClient.UploadObject(bucketName, "Program.cs", "text/plain", fileStream);
}
// List objects
foreach (var obj in storageClient.ListObjects(bucketName, ""))
{
Console.WriteLine(obj.Name);
}
// Download file
using (var fileStream = File.Create("Program-copy.cs"))
{
storageClient.DownloadObject(bucketName, "Program.cs", fileStream);
}
foreach (var obj in Directory.GetFiles("."))
{
Console.WriteLine(obj);
}
WriteLine("**Environment**");
WriteLine($"Platform: .NET Core 2.0");
WriteLine($"OS: {RuntimeInformation.OSDescription}");
WriteLine(message);
WriteLine();
}
}
@henrikj242
Copy link
Author

@henrikj242 Getting problem while executing this line
using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
FileAccess.Read, FileShare.Read))

can you please help ?

@Sakshi1707 what kind of problem?

@Sakshi1707
Copy link

@henrikj242 Getting problem while executing this line
using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
FileAccess.Read, FileShare.Read))
can you please help ?

@Sakshi1707 what kind of problem?

@henrikj242 the code exits at this line is there anything i need to check ?

@Sakshi1707
Copy link

@henrikj242 Getting problem while executing this line
using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
FileAccess.Read, FileShare.Read))
can you please help ?

@Sakshi1707 what kind of problem?

@henrikj242 the code exits at this line is there anything i need to check ?

Solved the issue . Problem was with path of file
But now i am getting another problem ,
When trying to upload file :Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"invalid_grant", Description:"Invalid JWT Signature."
image

@dckoma
Copy link

dckoma commented Jan 29, 2024

amazing! Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment