Example #1
0
        public static void AuthExplicit(string projectId, string jsonPath)
        {
            var credential = GoogleCredential.FromFile(jsonPath);

            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "gcloud-dotnet/2.2.1",
            });
            string localPath     = @"C:\Users\poonamk\source\repos\GCP_test\GCP_test\combine issue.docx";
            var    request       = new BucketsResource.ListRequest(storage, projectId);
            var    requestResult = request.Execute();
            var    folder        = "00012";

            foreach (var bucket in requestResult.Items)
            {
                if (bucket.Name == "cf-staging-storage")
                {
                    AddFolder(folder, bucket.Name, credential);
                    string objectName = null;
                    // UploadFile(bucket.Name, localPath, objectName, credential);
                }
            }
            // return null;
        }
Example #2
0
        public object AuthImplicit(string projectId)
        {
            GoogleCredential credential =
                GoogleCredential.GetApplicationDefault();

            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "gcloud-dotnet/2.2.1",
            });
            var request       = new BucketsResource.ListRequest(storage, projectId);
            var requestResult = request.Execute();

            foreach (var bucket in requestResult.Items)
            {
                Console.WriteLine(bucket.Name);
            }
            return(null);
        }
        // [END auth_api_explicit]

        // [START auth_api_explicit_compute_engine]
        public object AuthExplicitComputeEngine(string projectId)
        {
            // Explicitly use service account credentials by specifying the
            // private key file.
            GoogleCredential credential =
                GoogleCredential.FromComputeCredential();

            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "DotNet Google Cloud Platform Auth Sample",
            });
            var request       = new BucketsResource.ListRequest(storage, projectId);
            var requestResult = request.Execute();

            foreach (var bucket in requestResult.Items)
            {
                Console.WriteLine(bucket.Name);
            }
            return(null);
        }
Example #4
0
        public static void AuthExplicitComputeEngine(string projectId)
        {
            // Explicitly use service account credentials by specifying the
            // private key file.
            GoogleCredential credential =
                GoogleCredential.FromComputeCredential();

            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "clear-fusion",
            });
            var request       = new BucketsResource.ListRequest(storage, projectId);
            var requestResult = request.Execute();
            var folder        = "00012";

            foreach (var bucket in requestResult.Items)
            {
                if (bucket.Name == "cf-staging-storage")
                {
                    // AddFolder(folder, bucket.Name);
                }
            }
            //return null;
        }
        // [END auth_api_implicit]

        // [START auth_api_explicit]
        public object AuthExplicit(string projectId, string jsonPath)
        {
            var credential = GoogleCredential.FromFile(jsonPath);

            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = "DotNet Google Cloud Platform Auth Sample",
            });
            var request       = new BucketsResource.ListRequest(storage, projectId);
            var requestResult = request.Execute();

            foreach (var bucket in requestResult.Items)
            {
                Console.WriteLine(bucket.Name);
            }
            return(null);
        }
        // [END auth_api_implicit]

        // [START auth_api_explicit]
        static object AuthApiExplicit(string projectId, string jsonPath)
        {
            GoogleCredential credential = null;
            using (var jsonStream = new FileStream(jsonPath, FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                credential = GoogleCredential.FromStream(jsonStream);
            }
            // Inject the Cloud Storage scope if required.
            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[]
                {
                    StorageService.Scope.DevstorageReadOnly
                });
            }
            var storage = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "DotNet Google Cloud Platform Auth Sample",
            });
            var request = new BucketsResource.ListRequest(storage, projectId);
            var requestResult = request.Execute();
            foreach (var bucket in requestResult.Items)
            {
                Console.WriteLine(bucket.Name);
            }
            return null;
        }
        /// <summary>
        /// Retrieves all buckets from the specified project and adding them to the blocking collection.
        /// </summary>
        /// <param name="project">Project to retrieve buckets from.</param>
        /// <param name="collections">Blocking collection to add buckets to.</param>
        private static async Task ListBucketsAsync(Project project, BlockingCollection <Bucket> collections)
        {
            // Using a new service on every request here ensures they can all be handled at the same time.
            BucketsResource.ListRequest request = GetNewService().Buckets.List(project.ProjectId);
            var allBuckets = new List <Bucket>();

            try
            {
                do
                {
                    Buckets buckets = await request.ExecuteAsync();

                    if (buckets.Items != null)
                    {
                        foreach (Bucket bucket in buckets.Items)
                        {
                            // BlockingCollecton does not have AddRange so we have to add each item individually.
                            collections.Add(bucket);
                        }
                    }
                    request.PageToken = buckets.NextPageToken;
                } while (request.PageToken != null);
            }
            catch (GoogleApiException e) when(e.HttpStatusCode == HttpStatusCode.Forbidden)
            {
            }
        }
Example #8
0
 /// <summary>
 /// Modifies the specified request for all non-null properties of this options object.
 /// </summary>
 /// <param name="request">The request to modify</param>
 internal void ModifyRequest(BucketsResource.ListRequest request)
 {
     if (Prefix != null)
     {
         request.Prefix = Prefix;
     }
     if (PageSize != null)
     {
         request.MaxResults = PageSize.Value;
     }
 }
Example #9
0
        private static async Task <IEnumerable <Bucket> > ListBucketsAsync(Project project)
        {
            // Using a new service on every request here ensures they can all be handled at the same time.
            BucketsResource.ListRequest request = GetNewService().Buckets.List(project.ProjectId);
            var allBuckets = new List <Bucket>();

            try
            {
                do
                {
                    Buckets buckets = await request.ExecuteAsync();

                    allBuckets.AddRange(buckets.Items ?? Enumerable.Empty <Bucket>());
                    request.PageToken = buckets.NextPageToken;
                } while (request.PageToken != null);
            }
            catch (GoogleApiException e) when(e.HttpStatusCode == HttpStatusCode.Forbidden)
            {
            }
            return(allBuckets);
        }