【问题标题】:Override base Google Cloud Storage in .NET在 .NET 中覆盖基础 Google Cloud Storage
【发布时间】:2022-10-23 21:11:14
【问题描述】:

我正在尝试设置local emulator of GCP storage(非官方)与.NET client library 一起使用。但是我不确定如何覆盖似乎被硬编码为storage.googleapis.com 的基本存储 URL。
我在模拟器描述中看到STORAGE_EMULATOR_HOST 参数,但它似乎不适用于.NET 客户端库:|

我检查了一些库代码,并在内部 StorageService 类中找到了这个代码:

有什么方法可以覆盖 .NET 6 / Core 中的 BaseUri

我的部分代码/配置供参考:

// Configuration
private static void AddGcpServices(this IServiceCollection services, IConfiguration configuration)
{
    // Dev only for now
    var storageClient = StorageClient.CreateUnauthenticated();
    services.AddSingleton(storageClient);
    services.AddSingleton<ICloudStorage, CloudStorage>();
}
    

// ...
// Storage
public class CloudStorage : ICloudStorage
{
    private readonly StorageClient _client;

    public CloudStorage(StorageClient client)
    {
        _client = client;
    }
    

    public async Task<Uri> SaveFile(string containerName, string fullSavePath, Stream file, CancellationToken cancellationToken = default)
    {
        var result = await _client.UploadObjectAsync(containerName, fullSavePath, null, source: file,
            cancellationToken: cancellationToken);
        return result.MediaLink.ToUri();
    }
}

【问题讨论】:

    标签: c# .net google-cloud-platform google-cloud-storage emulation


    【解决方案1】:

    当我浏览文档时,我发现this sample

    using Google.Cloud.Storage.V1;
    using System;
    
    public class SetClientEndpointSample
    {
        public StorageClient SetClientEndpoint(string endpoint) => new StorageClientBuilder
        {
            BaseUri = endpoint
        }.Build();
    }
    

    原来你只需要通过StorageClientBuilder 创建客户端 :)

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 2020-03-03
      • 2014-03-23
      • 1970-01-01
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多