Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileNameInBucket">The filename to save as in the bucket</param>
        /// <param name="pathToFile">Path to the file (relative or absolute)</param>
        /// <param name="mimeType">The mimetype to set</param>
        /// <param name="permissionLevel">Permission level to set</param>
        /// <returns></returns>
        public Bucket UploadFile(string fileNameInBucket, string pathToFile, string mimeType, Permissions permissionLevel = Permissions.OwnerOnly)
        {
            pathToFile = UriExtensions.GetAbsoluteUri(pathToFile);

            using (var fileStream = File.OpenRead(pathToFile))
            {
                return(UploadStream(fileNameInBucket, fileStream, mimeType, permissionLevel));
            }
        }
Ejemplo n.º 2
0
        public ServiceAccountAuthenticator(string projectId, string serviceAccountEmail, string certificatePath, string secretKey)
        {
            if (string.IsNullOrWhiteSpace(serviceAccountEmail))
            {
                throw new ArgumentException(nameof(serviceAccountEmail));
            }

            if (string.IsNullOrWhiteSpace(certificatePath))
            {
                throw new ArgumentException(nameof(certificatePath));
            }

            if (string.IsNullOrWhiteSpace(secretKey))
            {
                throw new ArgumentException(nameof(secretKey));
            }

            if (string.IsNullOrWhiteSpace(projectId))
            {
                throw new ArgumentException(nameof(projectId));
            }

            // Service account email must be an email address. A lot of people use the client Id instead of email by accident,
            // so this simple check should save them some time.
            if (!serviceAccountEmail.Contains("@"))
            {
                throw new InvalidOperationException("The `serviceAccountEmail` parameter must be an email address. (Did you use a client Id by accident?)");
            }

            _projectId = projectId;

            var fullpath    = UriExtensions.GetAbsoluteUri(certificatePath);
            var certificate = new X509Certificate2(fullpath, secretKey, X509KeyStorageFlags.Exportable);

            var credential = new Google.Apis.Auth.OAuth2.ServiceAccountCredential(
                new Google.Apis.Auth.OAuth2.ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = new[]
                {
                    DatastoreService.Scope.Datastore,
                    DatastoreService.Scope.CloudPlatform,
                    StorageService.Scope.DevstorageReadWrite
                }
            }.FromCertificate(certificate));

            _initializer = new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName       = projectId
            };
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Downloads a file from the bucket and saves it to the specified local path.
        /// The directory will be created if it does not exist.
        /// E.g. DownloadFile("somefile.txt", "./pathToFile/somefile.txt");
        /// </summary>
        /// <param name="fileNameInBucket">The filename in the bucket</param>
        /// <param name="pathToLocalFileName">Path to save the file at (including the new filename)</param>
        public void DownloadFile(string fileNameInBucket, string pathToLocalFileName)
        {
            if (string.IsNullOrWhiteSpace(pathToLocalFileName))
            {
                throw new ArgumentNullException(nameof(pathToLocalFileName));
            }

            pathToLocalFileName = UriExtensions.GetAbsoluteUri(pathToLocalFileName);
            var directory = Path.GetDirectoryName(pathToLocalFileName);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (var fileStream = new FileStream(pathToLocalFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                DownloadStream(fileNameInBucket, fileStream);
            }
        }