Google云端存储上传不适用于C#API

问题描述:

我想通过C#API将简单图片上传到Google云端存储。它似乎成功了,但我没有看到我的Google云存储桶中有任何东西。Google云端存储上传不适用于C#API

的代码,我到目前为止有:

Google.Apis.Services.BaseClientService.Initializer init = new Google.Apis.Services.BaseClientService.Initializer(); 
init.ApiKey = "@@[email protected]@"; 
init.ApplicationName = "@@[email protected]@"; 

Google.Apis.Storage.v1.StorageService ss = new Google.Apis.Storage.v1.StorageService(init); 

var fileobj = new Google.Apis.Storage.v1.Data.Object() 
{ 
    Bucket = "images", 
        Name = "some-file-" + new Random().Next(1, 666) 
}; 

Stream stream = null; 
stream = new MemoryStream(img); 

Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload insmedia; 
insmedia = new Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(ss, fileobj, "images", stream, "image/jpeg"); 
insmedia.Upload(); 
response.message = img.Length.ToString(); 

任何人要做到这一点,我会帮助你,因为我不想让你花一天时间所以在这里抓你的头是怎么了做这个。

首先创建一个“服务帐户”类型的凭据,这将给你与P12扩展私钥保存在某个地方,你可以在你的服务器上引用。

现在做到这一点:

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(); 
+0

创建服务帐户在阿比和验证您的开发人员控制台 - >凭证 – Saumil 2014-11-22 18:55:33