I'm trying to upload file to my storage. I use this code
String serviceAccountEmail = "YOUR SERVICE EMAIL HERE";
var certificate = new X509Certificate2(@"PATH TO YOUR p12 FILE HERE", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { Google.Apis.Storage.v1.StorageService.Scope.DevstorageFullControl }
}.FromCertificate(certificate));
Google.Apis.Storage.v1.StorageService ss = new Google.Apis.Storage.v1.StorageService(new Google.Apis.Services.BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "YOUR APPLICATION NAME HERE",
});
var fileobj = new Google.Apis.Storage.v1.Data.Object()
{
Bucket = "YOUR BUCKET NAME HERE",
Name = "file"
};
Stream stream = null;
stream = new MemoryStream(img);
Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload insmedia;
insmedia = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(ss, fileobj, "YOUR BUCKET NAME HERE", stream, "image/jpeg");
insmedia.Upload();
I made a new project -> Console application ( C# ) and I have problem with references I think because I'm getting those errors:
The type or namespace name 'Storage' does not exist in the namespace 'Google.Apis' (are you missing an assembly reference?)
My usings:
using Google.Apis.Auth.OAuth2;
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
What am I doing wrong? Please help me :)
It looks like you're missing a NuGet package - you should install the Google.Apis.Storage.v1
package in your project. After that, you should be able to add a using
directive of
using Google.Apis.Storage.v1;
... and use code of:
var insmedia = new ObjectsResource.InsertMediaUpload(ss, fileobj,
"YOUR BUCKET NAME HERE", stream, "image/jpeg");
insmedia.Upload();
(After setting the bucket name, of course.)
See more on this question at Stackoverflow