谷歌云存储上传与C#API工作 [英] Google cloud storage upload not working with C# API
本文介绍了谷歌云存储上传与C#API工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过C#API上传一个简单的图像,以谷歌的云存储。它似乎成功,但我没有看到我的谷歌云斗什么
我到目前为止的代码:
Google.Apis.Services.BaseClientService.Initializer的init =新Google.Apis.Services.BaseClientService.Initializer();
init.ApiKey =@@ myapikey @@
init.ApplicationName =@@ myapplicationname @@
Google.Apis.Storage.v1.StorageService SS =新Google.Apis.Storage.v1.StorageService(INIT);
变种FileObj文件=新Google.Apis.Storage.v1.Data.Object()
{
桶=图像,
名称=一些文件 - +新的随机()和Next(1,666)
};
流流= NULL;
=流新的MemoryStream(IMG);
Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload insmedia;
insmedia =新Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(SS,FileObj文件,图像,流,图像/ JPEG);
insmedia.Upload();
response.message = img.Length.ToString();
解决方案
任何人都希望做到这一点,我要帮助你们出去,因为我不想让你花一天时间所以在这里抓你的头,你是如何做到这一点。
首先创建一个服务帐户类型的凭据。这会给你P12扩展私钥保存在某个地方,你可以在你的服务器上引用
现在做到这一点:
字符串serviceAccountEmail =您的服务邮箱HERE;
无功证书=新X509Certificate2(@PATH TO YOUR P12文件的位置,notasecret,X509KeyStorageFlags.Exportable);
ServiceAccountCredential证书=新ServiceAccountCredential(
新ServiceAccountCredential.Initializer(serviceAccountEmail)
{
作用域=新[] {Google.Apis.Storage.v1.StorageService。 Scope.DevstorageFullControl}
} .FromCertificate(证书));
Google.Apis.Storage.v1.StorageService SS =新Google.Apis.Storage.v1.StorageService(新Google.Apis.Services.BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
应用程序名称=应用程式的名称HERE,
});
变种FileObj文件=新Google.Apis.Storage.v1.Data.Object()
{
桶=你的水桶NAME HERE,
NAME =文件
};
流流= NULL;
=流新的MemoryStream(IMG);
Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload insmedia;
insmedia =新Google.Apis.Storage.v1.ObjectsResource.InsertMediaUpload(SS,FileObj文件,你的水桶名在此流图像/ JPEG);
insmedia.Upload();
I'm trying to upload a simple image to Google cloud storage via the C# API. It seems to succeed but I don't see anything in my Google cloud bucket.
The code I have so far:
Google.Apis.Services.BaseClientService.Initializer init = new Google.Apis.Services.BaseClientService.Initializer();
init.ApiKey = "@@myapikey@@";
init.ApplicationName = "@@myapplicationname@@";
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();
解决方案
Anyone looking to do this I'm going to help you out since I don't want you to spend a day scratching your heads so here is how you do this.
First of all create a "Service Account" type credentials which will give you a private key with p12 extension save this somewhere you can reference on your server.
Now do this:
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();
这篇关于谷歌云存储上传与C#API工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文