c# - 如何在 .net 中为谷歌云存储注册 url

标签 c# php asp.net .net google-cloud-storage

我想知道如何在 .net 中使用谷歌云存储类生成 signurl

我已经按照要求创建了字符串

GET


1388534400
/bucket/objectname

但我现在想用 p12 key 签署这个 url,然后想让它 url 友好

这个库没有显示它的特定功能 -> https://developers.google.com/resources/api-libraries/documentation/storage/v1/csharp/latest/annotated.html

所以,基本上我需要 php 的 Google_Signer_P12 类的 .net 替代品

$signer = new Google_Signer_P12(file_get_contents(__DIR__.'/'."final.p12"), "notasecret");
$signature = $signer->sign($to_sign);

最佳答案

现在有一个UrlSigner在预发布包中 Google.Cloud.Storage.V1可用于提供对现有对象的只读访问:

// Create a signed URL which can be used to get a specific object for one hour.
UrlSigner urlSigner = UrlSigner.FromServiceAccountCredential(credential);
string url = urlSigner.Sign(
    bucketName,
    objectName,
    TimeSpan.FromHours(1),
    HttpMethod.Get);

或将特定对象内容放入存储桶的只写访问权限:

// Create a signed URL which allows the requester to PUT data with the text/plain content-type.
UrlSigner urlSigner = UrlSigner.FromServiceAccountCredential(credential);
var destination = "places/world.txt";
string url = urlSigner.Sign(
    bucketName,
    destination,
    TimeSpan.FromHours(1),
    HttpMethod.Put,
    contentHeaders: new Dictionary<string, IEnumerable<string>> {
        { "Content-Type", new[] { "text/plain" } }
    });

// Upload the content into the bucket using the signed URL.
string source = "world.txt";

ByteArrayContent content;
using (FileStream stream = File.OpenRead(source))
{
    byte[] data = new byte[stream.Length];
    stream.Read(data, 0, data.Length);
    content = new ByteArrayContent(data)
    {
        Headers = { ContentType = new MediaTypeHeaderValue("text/plain") }
    };
}

HttpResponseMessage response = await httpClient.PutAsync(url, content);

关于c# - 如何在 .net 中为谷歌云存储注册 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354609/

相关文章:

c# - 方法运行时 Richbox 不更新

c# - 在 SignalR V 2.0 中支持驼峰式属性名称

c# - 在 Swagger UI 中显示 HTTP 请求持续时间

c# - 在 WPF 中验证文本框时启用按钮

php - 当我注销时页面未正确重定向

c# - 如何查找英里范围内的邮政编码?

php - Doctrine onFlush 事件监听器服务的 Symfony 循环引用异常

php - 在php中获取上一页的URL

c# - 基于输入的动态返回类型 - asp.net

c# - '??' 在 C# 中是什么意思?