Cloud Storage

Google Cloud Storage allows world-wide storage and retrieval of any amount of data at any time. It can be used for a range of scenarios including serving website content, storing data for archival and disaster recovery, or distributing large data objects to users via direct download.

Enable APIs

For the purposes of this article, you will have to enable the Google Cloud Storage API and Google Cloud Storage JSON API. Please read the Getting Started for more information on how you can achieve that.

Step 1: Create the Windows Universal Application

Create a standard Windows Universal application and add 3 Buttons and a RadListView control to it. The first button will list all of the files uploaded in our storage. The second button will upload a file and the third one will delete the selected file in the RadListView.

[XAML] Example 1: Defining the view

        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="200"/>

    <data:RadListView x:Name="radListView" 
                      ItemsSource="{Binding FileNames}" 
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
    <Grid Grid.Column="1">
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>

        <Button HorizontalAlignment="Stretch"
                Content="List Items" 
                Command="{Binding ListItemsCommand}"/>

        <Button Content="Upload Item" 
                Command="{Binding UploadItemCommand}"  

        <Button Content="Delete item" 
                Command="{Binding DeleteItemCommand}" 

Step 2: Install the NuGet package

Open the NuGet Package Manager and install the Google.Cloud.Storage.V1 package.

Google Cloud Storage Nuget

Step 3: Define the ViewModel

The next step is to create the ViewModel. It will need a StorageClient object which will be used for uploading, deleting and listing files. We also need to implement all of the commands that the Buttons are bound to.

In order to locate your project id, please read the Locate the project ID help article.

[C#] Example 2: Defining the ViewModel

public class ViewModel
    public StorageClient StorageClient { get; set; }
    private string jsonPath = @"json used for validation";

    private const string ProjectId = "your-project-id";
    private const string BucketName = ProjectId + "_test_bucket";

    private object selectedItem;
    private ObservableCollection<string> fileNames;

    public object SelectedItem
        get { return this.selectedItem; }
        set { this.selectedItem = value; }

    public ObservableCollection<string> FileNames
        get { return this.fileNames; }
        set { this.fileNames = value; }

    public ViewModel()
        this.ListItemsCommand = new Command(() => ListItems());
        this.DeleteItemCommand = new Command(() => DeleteItem());
        this.UploadItemCommand = new Command(() => UploadItem());

        GoogleCredential credentials = GoogleCredential.FromJson(jsonPath);
        this.StorageClient = StorageClient.Create(credentials);
        if (StorageClient.GetBucket(BucketName) == null)
            StorageClient.CreateBucket(ProjectId, BucketName);
        this.fileNames = new ObservableCollection<string>();

    private void ListItems()
        var items = StorageClient.ListObjects(BucketName);


        foreach (Google.Apis.Storage.v1.Data.Object item in items)

    private async void UploadItem()
        FileOpenPicker openPicker = new FileOpenPicker();
        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

        var inputFile = await openPicker.PickSingleFileAsync();
        if (inputFile == null)

            var streamToUpload = await inputFile.OpenStreamForReadAsync();

            await StorageClient.UploadObjectAsync(
        catch (Exception)

    private async void DeleteItem()
        if (this.SelectedItem == null)
            var dialog = new MessageDialog("Please select an Item");

        StorageClient.DeleteObject(BucketName, this.SelectedItem.ToString());

    public Command ListItemsCommand { get; set; }
    public Command UploadItemCommand { get; set; }
    public Command DeleteItemCommand { get; set; }

The example uses verification through a JSON string, however, there are different options. Please read the Setting Up Authentication for Server to Server Production Applications article.

All that is left now is to set the DataContext of the MainPage and run the application.

[C#] Example 3: Set the DataContext

public MainPage()
        this.DataContext = new ViewModel();

Figure 1: Example after uploading a couple of files and listing them within the RadListView control

Google Cloud Storage Upload

See Also