How to sign url in .net for google cloud storage

前端 未结 4 1339
名媛妹妹
名媛妹妹 2021-01-07 01:35

I want to know that how to generate signurl using google cloud storage classes in .net

I have created string as per the requirement

GET


1388534400
         


        
4条回答
  •  孤城傲影
    2021-01-07 01:47

    I know the question was for P12, but Google lead me here when I was looking to do this for the newer, preferred JSON method. I pieced this together with other samples and sites I found. Hope this help save some time.

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

    A list of Scopes can be found here

提交回复
热议问题