如何在 .net 中为 Google 云存储签名 url

2024-05-02

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

我已经根据要求创建了字符串

GET


1388534400
/bucket/objectname

但我现在想用 p12 密钥签署这个 url,然后想让它变得 url 友好

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

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

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

现在有一个网址签名者 https://googlecloudplatform.github.io/google-cloud-dotnet/docs/Google.Cloud.Storage.V1/api/Google.Cloud.Storage.V1.UrlSigner.html在预发布包中谷歌.云.存储.V1 http://googlecloudplatform.github.io/google-cloud-dotnet/docs/Google.Cloud.Storage.V1/index.html可用于提供对现有对象的只读访问:

// 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 .net 中为 Google 云存储签名 url 的相关文章

随机推荐