如何在.net中为谷歌云存储登录url


How to sign url in .net for google cloud storage

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

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

GET

1388534400
/bucket/objectname

但我现在想用p12密钥签署这个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);

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

// 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);

我知道这个问题是针对P12的,但当我想为更新的首选JSON方法做这件事时,谷歌把我带到了这里。我把这个和我发现的其他样本和网站拼凑在一起。希望这能节省一些时间。

    public string GetSignedURL()
    {
        var myObj = "theObject";
        var scopes = new string[] { "https://www.googleapis.com/auth/devstorage.read_write" };
        var myBucket = "theBucket";
        ServiceAccountCredential cred;
        using ( var stream = new FileStream(@"'path to'private-key.json", FileMode.Open, FileAccess.Read) )
        {
            cred = GoogleCredential.FromStream(stream)
                                   .CreateScoped(scopes)
                                   .UnderlyingCredential as ServiceAccountCredential;
        }
        var urlSigner = UrlSigner.FromServiceAccountCredential(cred);
        return urlSigner.Sign(myBucket, myObj, TimeSpan.FromHours(1), HttpMethod.Get);
    }

范围列表可在此处找到

.NET客户端不支持对URL进行签名(它是一个基于XML的API),因此您需要对gsutil等工具进行调用,或者在应用程序内部生成RSA签名(使用RSA C#对签名进行签名和验证)

这是我的谷歌签名者代码,可以根据他们的需求使其更加动态

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Security.Cryptography.X509Certificates;
namespace HHAFSGoogle
{
    static class GoogleSigner
    {
        private static string hashAlgo = "SHA256";
        public static string ServiceAccountEmail
        {
            get
            {
                return "XXXXXXXXXXXXX-YYYYYYYYYYYYYYYYYYYYYYYY@developer.gserviceaccount.com";
            }
        }
        public static string GoogleSecreat
        {
            get
            {
                return "notasecret";
            }
        }
        public static string GoogleBucketDir
        {
            get
            {
                return "MyBucketDirectory";
            }
        }
        public static string GoogleBucketName
        {
            get
            {
                return "MyBucket";
            }
        }
        public static string CertiFilelocation
        {
            get
            {
                return System.Web.HttpContext.Current.Server.MapPath("p12file.p12");
            }
        }
        /// <summary>
        /// Get URL signature
        /// </summary>
        /// <param name="base64EncryptedData"></param>
        /// <param name="certiFilelocation"></param>
        /// <returns></returns>
        public static string GetSignature(string base64EncryptedData, string certiFilelocation)
        {
            X509Certificate2 certificate = new X509Certificate2(certiFilelocation, GoogleSecreat, X509KeyStorageFlags.Exportable);
            RSACryptoServiceProvider csp = (RSACryptoServiceProvider)certificate.PrivateKey;
            RSACryptoServiceProvider privateKey1 = new RSACryptoServiceProvider();
            privateKey1.ImportParameters(csp.ExportParameters(true));
            csp.ImportParameters(privateKey1.ExportParameters(true));
            byte[] data = Encoding.UTF8.GetBytes(base64EncryptedData.Replace("'r", ""));
            byte[] signature = privateKey1.SignData(data, hashAlgo);
            bool isValid = privateKey1.VerifyData(data, hashAlgo, signature);
            if (isValid)
            {
                return Convert.ToBase64String(signature);
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// Get signed URL by Signature
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="method"></param>
        /// <param name="content_type"></param>
        /// <param name="duration"></param>
        /// <returns></returns>
        public static string GetSignedURL(string fileName, string method = "GET", string content_type = "", int duration = 10)
        {
            TimeSpan span = (DateTime.UtcNow.AddMinutes(10) - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
            var expires = Math.Round(span.TotalSeconds, 0);
            // Encode filename, so URL characters like %20 for space could be handled properly in signature
            fileName = HttpUtility.UrlPathEncode(fileName);
            // Generate a string to sign
            StringBuilder sbFileParam = new StringBuilder();
            sbFileParam.AppendLine(method);  //Could be GET, PUT, DELETE, POST
            //  /* Content-MD5 */ "'n" .
            sbFileParam.AppendLine();
            sbFileParam.AppendLine(content_type);   // Type of content you would upload e.g. image/jpeg
            sbFileParam.AppendLine(expires.ToString());     // Time when link should expire and shouldn't work longer
            sbFileParam.Append("/" + GoogleBucketName + "/" + fileName);
            var signature = System.Web.HttpContext.Current.Server.UrlEncode(GetSignature(sbFileParam.ToString(), CertiFilelocation));
            return ("https://storage.googleapis.com/MyBucket/" + fileName +
                        "?response-content-disposition=attachment;&GoogleAccessId=" + ServiceAccountEmail +
                        "&Expires=" + expires + "&Signature=" + signature);
        }
    }
}

并下载文件调用以上类以获得签名的url

GoogleSigner.GetSignedURL(bucketFileName)

并上传文件调用以上类,以获得用于上传url 的签名url

GoogleSigner.GetSignedURL(fileName, "PUT", type);