简体   繁体   English

使用来自C#的Json文件中的服务帐户的Google云存储桶

[英]Google cloud storage bucket using service account in Json file from C#

我是C#编码世界的新手,需要帮助才能使用C#的服务帐户Json文件连接到Google云存储桶,同样需要一些指针。

First of all you need to create a service account and create the JSON File. 首先,您需要创建一个服务帐户并创建JSON文件。

In the GCP Console, go to the Create service account key page. 在GCP控制台中,转到“创建服务帐户密钥”页面。

GO TO THE CREATE SERVICE ACCOUNT KEY PAGE From the Service account drop-down list, select New service account. 转到创建服务帐户关键页从“服务帐户”下拉列表中,选择“新服务帐户”。 In the Service account name field, enter a name . 在服务帐户名称字段中,输入名称。 From the Role drop-down list, select Project > Owner. 从角色下拉列表中,选择项目>所有者。 Click Create. 单击创建。 A JSON file that contains your key downloads to your computer. 包含密钥下载的JSON文件到您的计算机。

Then install the reauired plugin for your project. 然后为您的项目安装已注册的插件。 If you are using Visual Studio 2017 or higher, open nuget package manager window and type the following: 如果您使用的是Visual Studio 2017或更高版本,请打开nuget包管理器窗口并键入以下内容:

Install-Package Google.Cloud.Storage.V1

If you are using .NET Core command-line interface tools to install your dependencies, run the following command: 如果使用.NET Core命令行界面工具来安装依赖项,请运行以下命令:

dotnet add package Google.Cloud.Storage.V1

Then add the JSON file to your path and set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to the JSON file, so that StorageClient can find it. 然后将JSON文件添加到您的路径,并将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为引用JSON文件,以便StorageClient可以找到它。

For the application part, here is an exemple : 对于应用程序部分,这是一个示例:

using Google.Cloud.Storage.V1;
using System;
using System.Diagnostics;

namespace GoogleCloudSamples
{
    class StorageQuickstart
    {
        static void Main(string[] args)
        {
            // Your Google Cloud Platform project ID.
            string projectId = "YOUR-PROJECT-ID";


            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            // The name for the new bucket.
            string bucketName = projectId + "-test-bucket";
            try
            {
                // Creates the new bucket.
                storageClient.CreateBucket(projectId, bucketName);
                Console.WriteLine($"Bucket {bucketName} created.");
            }
            catch (Google.GoogleApiException e)
            when (e.Error.Code == 409)
            {
                // The bucket already exists.  That's fine.
                Console.WriteLine(e.Error.Message);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用服务帐户将文件上传到Google Merchant Center中生成的Google Cloud Storage存储桶 - Upload file using Service Account to Google Cloud Storage bucket that was generated in Google Merchant Center 使用RestSharp或C#从Google云存储桶中获取文件 - Get File from Google cloud storage bucket using RestSharp or C# 如何使用Google Cloud JSON Service帐户文件发出C#REST请求? - How to make a C# REST request using a Google Cloud JSON Service account file? 如何从 C# 访问谷歌云存储桶 - How to access Google cloud storage bucket from c# 在 C# 中列出 Google Cloud Storage Bucket 中的嵌套文件夹 - List nested folders in Google Cloud Storage Bucket in C# 将文件从本地 C# winforms 应用程序安全上传到生产中的 Google Cloud Storage Bucket - securely uploading files from local C# winforms app to Google Cloud Storage Bucket in production 使用 c# 中的 HMAC 密钥将文件上传到 Google Cloud Storage - Upload file to Google Cloud Storage using HMAC key in c# 使用C#从Google云存储下载对象 - download object from Google cloud storage using c# 使用 C# 拉出 Cloud Storage 存储桶 - Pulling out of a Cloud Storage bucket using C# 在C#应用程序中创建默认Google Cloud Storage存储桶区域时进行更改 - Change default Google Cloud Storage bucket region when creating it in C# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM