c# - 谷歌云存储桶在 C# 的 Json 文件中使用服务帐户
问题描述
我是 C# 编码世界的新手,需要帮助才能使用 C# 中的服务帐户 Json 文件连接到 Google 云存储桶,需要一些相同的指针。
解决方案
首先,您需要创建一个服务帐户并创建 JSON 文件。
在 GCP Console 中,转到创建服务帐号密钥页面。
转到创建服务帐户密钥页面 从服务帐户下拉列表中,选择新建服务帐户。在服务帐户名称字段中,输入名称。从角色下拉列表中,选择项目 > 所有者。单击创建。一个 JSON 文件,其中包含您下载到计算机的密钥。
然后为您的项目安装重新安装的插件。如果您使用的是 Visual Studio 2017 或更高版本,请打开 nuget 包管理器窗口并键入以下内容:
Install-Package Google.Cloud.Storage.V1
如果您使用 .NET Core 命令行界面工具安装依赖项,请运行以下命令:
dotnet add package Google.Cloud.Storage.V1
然后将 JSON 文件添加到您的路径并设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量以引用 JSON 文件,以便 StorageClient 可以找到它。
对于应用程序部分,这是一个示例:
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);
}
}
}
}
推荐阅读
- keras - Keras save_weights h5 文件显示为空
- flutter - 为什么我从这个回调中得到一个空值?
- laravel - 如何只向发件人广播?
- python - Python字典到pandas DataFrame的转换
- node.js - 如何解决 TypeError: Cannot read property 'then' of undefined
- python - Django python问题找到h1元素和标题
- flutter - 我如何显示平均收视率
- html - 高分辨率移动设备上的响应式 CSS 设计
- android - Android Coinbase API 抛出 javax.net.ssl.SSLHandshakeException
- pandas - 如何过滤我的数据框的所有行