Filestream Google
Can someone please provide an example of how to use Google.Apis.Storage.v1 for uploading files to google cloud storage in c#?
6 Answers
I found that this basic operation is not as straight forward as you might expect. Google's documentation about it's Storage API is lacking in information about using it in C# (or any other .NET language). Searching for 'how to upload file to google cloud storage in c#' didn't exactly help me, so here is my working solution with some comments:
Preparation:
You need to create OAuth2 account in your Google Developers Console - go to Project/APIs & auth/Credentials.
Copy Client ID & Client Secret to your code. You will also need your Project name.
Code (it assumes that you've added Google.Apis.Storage.v1 via NuGet):
First, you need to authorize your requests:
Sometimes you might also want to refresh authorization token via:
You also need to create Storage Service:
Now you can make requests to Google Storage API.Let's start with creating a new bucket:
And it's done. Now, you can send a request to get list of all of your buckets:
Last part is uploading new file:
And bam! New file will be visible in you Google Developers Console inside of selected bucket.
You can use Google Cloud APIs without SDK in the following ways:
- Required api-key.json file
- Install package Google.Apis.Auth.OAuth2 in order to authorize theHTTP web request
- You can set the default configuration for your application in thisway
- I did the same using .NET core web API and details are given below:
Url details:
'GoogleCloudStorageBaseUrl': 'https://www.googleapis.com/upload/storage/v1/b/', 'GoogleSpeechBaseUrl': 'https://speech.googleapis.com/v1/operations/', 'GoogleLongRunningRecognizeBaseUrl': 'https://speech.googleapis.com/v1/speech:longrunningrecognize', 'GoogleCloudScope': 'https://www.googleapis.com/auth/cloud-platform',
Get Oauth token:
To upload file to cloud bucket:
In order to perform any action to the cloud API, just need to make a HttpClient get/post request as per the requirement.
Thanks
This is for Google.Cloud.Storage.V1
(not Google.Apis.Storage.v1
), but appears to be a bit simpler to perform an upload now. I started with the Client libraries 'Getting Started' instructions to create a service account and bucket, then experimented to find out how to upload an image.
The process I followed was:
- Sign up for Google Cloud free trial
- Create a new project in Google Cloud (remember the project nameID for later)
- Create a Project Owner service account - this will result in a json file being downloaded that contains the service account credentials. Remember where you put that file.
- The getting started docs get you to add the path to the JSON credentials file into an environment variable called
GOOGLE_APPLICATION_CREDENTIALS
- I couldn't get this to work through the provided instructions. Turns out it is not required, as you can just read the JSON file into a string and pass it to the client constructor. - I created an empty WPF project as a starting point, and a single
ViewModel
to house the application logic. - Install the
Google.Cloud.Storage.V1
nuget package and it should pull in all the dependencies it needs.
Onto the code.
MainWindow.xaml
MainWindow.xaml.cs
ViewModel.cs
Use of Google.Apis.Storage.v1 for uploading files using SDK to google cloud storage in c#:
Required api-key.json file
Install the package Google.Cloud.Storage.V1; and Google.Apis.Auth.OAuth2;
The code is given below to upload the file to the cloud
To set the credentials
I used SDK in one of my window application. You can use the same code according to your needs/requirements.
You'll be happy to know it still works in 2016..I was googling all over using fancy key words like 'google gcp C# upload image', until I just plain asked the question: 'How do I upload an image to google bucket using C#'.. and here I am. I removed the .Result
in the user credential, and this was the final edit that worked for me.
Here is the link to their official C# example of '.NET Bookshelf App' using Google Cloud storage.
Source on github:
Nuget