我想知道如何在 .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/