0% found this document useful (0 votes)
84 views2 pages

Use The Google Cloud API To Upload and Download Files Tofrom A Bucket Using Explicit Authentication in A C# App.

Use the Google Cloud API to upload and download files tofrom a bucket using explicit authentication in a C# app.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views2 pages

Use The Google Cloud API To Upload and Download Files Tofrom A Bucket Using Explicit Authentication in A C# App.

Use the Google Cloud API to upload and download files tofrom a bucket using explicit authentication in a C# app.

Uploaded by

hsuyip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Use the Google Cloud API to upload and download files tofrom 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 Getting problem while executing this line


using (var jsonStream = new FileStream("credentials.json", FileMode.Open,
FileAccess.Read, FileShare.Read))

can you please help ?

You might also like