Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 谷歌云存储上传不使用C API_C#_Google Api_Google Cloud Storage_Google Api Dotnet Client - Fatal编程技术网

C# 谷歌云存储上传不使用C API

C# 谷歌云存储上传不使用C API,c#,google-api,google-cloud-storage,google-api-dotnet-client,C#,Google Api,Google Cloud Storage,Google Api Dotnet Client,我正试图通过C API将一个简单的图像上传到Google云存储。它似乎成功了,但我在我的谷歌云存储桶里什么也看不到 到目前为止,我掌握的代码是: Google.Apis.Services.BaseClientService.Initializer init = new Google.Apis.Services.BaseClientService.Initializer(); init.ApiKey = "@@myapikey@@"; init.ApplicationName = "@@myapp

我正试图通过C API将一个简单的图像上传到Google云存储。它似乎成功了,但我在我的谷歌云存储桶里什么也看不到

到目前为止,我掌握的代码是:

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

任何想这样做的人,我都会帮你,因为我不想让你花一天的时间挠头,所以这里是你如何做到这一点

首先,创建一个服务帐户类型凭据,该凭据将为您提供一个扩展名为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();

任何想这样做的人,我都会帮你,因为我不想让你花一天的时间挠头,所以这里是你如何做到这一点

首先,创建一个服务帐户类型凭据,该凭据将为您提供一个扩展名为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();

创建服务帐户位于开发人员控制台的Api&Auth->Credentials下创建服务帐户位于开发人员控制台的Api&Auth->Credentials下