Exemplo n.º 1
0
        public GoogleStorageProvider(GoogleCredential credential, GoogleProviderOptions options)
        {
            if (credential == null)
            {
                throw new ArgumentNullException(nameof(credential));
            }

            _client       = StorageClient.Create(credential);
            _serviceEmail = options.Email;
            _bucket       = options.Bucket;

            if (credential.UnderlyingCredential is ServiceAccountCredential cred)
            {
                _urlSigner = UrlSigner.FromServiceAccountCredential(cred);
            }
        }
        public GoogleStorageProvider(GoogleProviderOptions options)
        {
            if (options.P12PrivateKey == null)
            {
                throw new StorageException(new StorageError
                {
                    Code            = 1007,
                    Message         = "P12 private key is required.",
                    ProviderMessage = "P12 private key is required."
                }, null);
            }

            try
            {
                _certificate = new X509Certificate2(Convert.FromBase64String(options.P12PrivateKey), "notasecret", X509KeyStorageFlags.Exportable);
                var credential =
                    new ServiceAccountCredential(new ServiceAccountCredential.Initializer(options.Email)
                {
                    Scopes = new[] { StorageService.Scope.DevstorageFullControl }
                }.FromCertificate(_certificate));

                _storageService = new StorageService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential
                });
            }
            catch (Exception ex)
            {
                throw new StorageException(new StorageError
                {
                    Code            = 1000,
                    Message         = "Invalid P12 private key.",
                    ProviderMessage = ex.Message
                }, ex);
            }

            _serviceEmail = options.Email;
            _bucket       = options.Bucket;
        }