Beispiel #1
0
        static void Main(string[] args)
        {
            // Google Cloud Platform project ID.
            string projectId = "sit313-2018";


            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            // The name for the new bucket.
            string bucketName = projectId + "-test-bucket";

            try
            {
                // Creates the new bucket.
                storageClient.CreateBucket(projectId, bucketName);
                Console.WriteLine($"Bucket {bucketName} created.");
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    // The bucket already exists.  That's fine.
                    Console.WriteLine(e.Error.Message);
                }
        }
Beispiel #2
0
        private VarlikResult <string> CreateBucket()
        {
            var bucketProjectId = WebConfigurationManager.AppSettings["bucketProjectId"];

            if (string.IsNullOrEmpty(bucketProjectId))
            {
                Log.Error("CreateBucket : Cannot Get Bucket Project Id");
                return(new VarlikResult <string>());
            }

            var    result    = new VarlikResult <string>();
            string projectId = bucketProjectId;

            try
            {
                StorageClient storageClient = StorageClient.Create();
                string        bucketName    = projectId + "_files";
                result.Data = bucketName;
                result.Success();
                storageClient.CreateBucket(projectId, bucketName);
            }
            catch (Google.GoogleApiException e)
            {
                if (e.Error.Code != 409)
                {
                    result.Status = ResultStatus.UnknownError;
                }
            }
            return(result);
        }
        public BigQueryFixtureBase(string bucketPrefix)
        {
            StorageClient = StorageClient.Create();

            StorageBucketName = IdGenerator.FromDateTime(prefix: bucketPrefix);
            StorageClient.CreateBucket(ProjectId, StorageBucketName);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string projectID = "ciconi-1";

            StorageClient sc         = StorageClient.Create();
            string        buckedName = projectID + "-test-bucket";

            try
            {
                sc.CreateBucket(projectID, buckedName);
                Console.WriteLine($"Bucket{buckedName} created.");


                //upload coisas
                MemoryStream ms = new MemoryStream();
                using (FileStream file = new FileStream("C:\Users\ZeusAC\Desktop\1.png",
                                                        FileMode.Open, FileAccess.Read)) file.CopyTo(ms);

                var obj1 = sc.UploadObject(buckedName, "testc.pnj", "images/png", ms);

                // FileStream mp4 = new
                //parei aqui//
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    Console.WriteLine(e.Error.Message);
                }
        }
Beispiel #5
0
        private Bucket CreateBucket(StorageClient client)
        {
            Bucket bucket = client.CreateBucket(BucketName, BucketName);

            Console.WriteLine($"Created {BucketName}.");
            return(bucket);
        }
        void CreateBucket()
        {
            // Your Google Cloud Platform project ID.
            string projectId = "YOUR-PROJECT-ID";


            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            // The name for the new bucket.
            string bucketName = projectId + "-test-bucket";

            try
            {
                // Creates the new bucket.
                storageClient.CreateBucket(projectId, bucketName);
                Console.WriteLine($"GoogleBucket.cs - Bucket {bucketName} created.");
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    // The bucket already exists.  That's fine.
                    Console.WriteLine("GoogleBucket.cs - " + e.Error.Message);
                }
        }
        static void Main(string[] args)
        {
            // Your Google Cloud Platform project ID.
            string projectId = "YOUR-PROJECT-ID";

            // [END storage_quickstart]
            Debug.Assert("YOUR-PROJECT-" + "ID" != projectId,
                         "Edit Program.cs and replace YOUR-PROJECT-ID with your Google Project Id.");
            // [START storage_quickstart]

            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            // The name for the new bucket.
            string bucketName = projectId + "-test-bucket";

            try
            {
                // Creates the new bucket.
                storageClient.CreateBucket(projectId, bucketName);
                Console.WriteLine($"Bucket {bucketName} created.");
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    // The bucket already exists.  That's fine.
                    Console.WriteLine(e.Error.Message);
                }
        }
        public void ExportCsv()
        {
            // TODO: Make this simpler in the wrapper
            var    projectId      = _fixture.ProjectId;
            var    datasetId      = _fixture.GameDatasetId;
            var    historyTableId = _fixture.HistoryTableId;
            string bucket         = "bigquerysnippets-" + Guid.NewGuid().ToString().ToLowerInvariant();
            string objectName     = "table.csv";

            if (!WaitForStreamingBufferToEmpty(historyTableId))
            {
                Console.WriteLine("Streaming buffer not empty after 30 seconds; not performing export");
                return;
            }

            // Sample: ExportCsv
            BigqueryClient client = BigqueryClient.Create(projectId);

            // Create a storage bucket; in normal use it's likely that one would exist already.
            StorageClient storageClient = StorageClient.Create();

            storageClient.CreateBucket(projectId, bucket);
            string destinationUri = $"gs://{bucket}/{objectName}";

            Job job = client.Service.Jobs.Insert(new Job
            {
                Configuration = new JobConfiguration
                {
                    Extract = new JobConfigurationExtract
                    {
                        DestinationFormat = "CSV",
                        DestinationUris   = new[] { destinationUri },
                        SourceTable       = client.GetTableReference(datasetId, historyTableId)
                    }
                }
            }, projectId).Execute();

            // Wait until the export has finished.
            var result = client.PollJob(job.JobReference);

            // If there are any errors, display them *then* fail.
            if (result.Status.ErrorResult != null)
            {
                foreach (var error in result.Status.Errors)
                {
                    Console.WriteLine(error.Message);
                }
            }
            Assert.Null(result.Status.ErrorResult);

            MemoryStream stream = new MemoryStream();

            storageClient.DownloadObject(bucket, objectName, stream);
            Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
            // End sample

            storageClient.DeleteObject(bucket, objectName);
            storageClient.DeleteBucket(bucket);
        }
        public void Initialize()
        {
            var credential = GoogleCredential.GetApplicationDefault();

            _client = StorageClient.Create(credential);
            // Make an authenticated API request.
            _bucket = _client.CreateBucket(this.Connection, this.Container);
            Workbench.Instance.Repository.SetMediaStorage(this);
        }
        public TranslateFixture()
        {
            _client    = StorageClient.Create();
            BucketName = "translate-v3-bucket-" + TestUtil.RandomName();
            GlossaryId = "must-start-with-letters" + TestUtil.RandomName();

            _client.CreateBucket(ProjectId, BucketName);
            CreateGlossary.CreateGlossarySample(ProjectId, GlossaryId, _glossaryInputUri);
        }
Beispiel #11
0
 public BigQueryTest()
 {
     _projectId  = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
     _client     = BigQueryClient.Create(_projectId);
     _storage    = StorageClient.Create();
     _bucketName = TestUtil.RandomName();
     _storage.CreateBucket(_projectId, _bucketName);
     _stringOut = new StringWriter();
     Console.SetOut(_stringOut);
 }
        public async Task SetBucketIamPolicy()
        {
            var projectId  = _fixture.ProjectId;
            var bucketName = IdGenerator.FromGuid();

            _fixture.RegisterBucketToDelete(bucketName);

            // Snippet: SetBucketIamPolicy(string, *, *)
            // Create a new bucket and an empty file within it
            StorageClient client = StorageClient.Create();
            Bucket        bucket = client.CreateBucket(projectId, bucketName);
            var           obj    = client.UploadObject(bucketName, "empty.txt", "text/plain", new MemoryStream());

            // Demonstrate that without authentication, we can't download the object
            HttpClient          httpClient = new HttpClient();
            HttpResponseMessage response1  = await httpClient.GetAsync(obj.MediaLink);

            Console.WriteLine($"Response code before setting policy: {response1.StatusCode}");

            // Fetch the current IAM policy, and modify it in memory to allow all users
            // to view objects.
            Policy policy = client.GetBucketIamPolicy(bucketName);
            string role   = "roles/storage.objectViewer";

            Policy.BindingsData binding = policy.Bindings
                                          .Where(b => b.Role == role)
                                          .FirstOrDefault();
            if (binding == null)
            {
                binding = new Policy.BindingsData {
                    Role = role, Members = new List <string>()
                };
                policy.Bindings.Add(binding);
            }
            binding.Members.Add("allUsers");

            // Update the IAM policy on the bucket.
            client.SetBucketIamPolicy(bucketName, policy);

            // Wait 10 seconds to allow the policy to be applied.
            // (Normally the policy change is visible pretty much immediately, but
            // 10 seconds makes this very reliable.)
            await Task.Delay(TimeSpan.FromSeconds(10));

            // Download the object again: this time the response should be OK
            HttpResponseMessage response2 = await httpClient.GetAsync(obj.MediaLink);

            Console.WriteLine($"Response code after setting policy: {response2.StatusCode}");

            // End snippet

            StorageSnippetFixture.SleepAfterBucketCreateDelete();
            Assert.Equal(HttpStatusCode.Unauthorized, response1.StatusCode);
            Assert.Equal(HttpStatusCode.OK, response2.StatusCode);
        }
        public Material Transcribe(string fnAudio, string langCode)
        {
            // Keep storage client for full operation; will be removing file at the end
            StorageClient sc     = null;
            Bucket        bucket = null;

            try
            {
                sc = StorageClient.Create(GoogleCredential.FromFile(fnCredential));
                // Get out bucket, create on demand
                var buckets = sc.ListBuckets(projectId);
                foreach (var x in buckets)
                {
                    if (x.Name == bucketName)
                    {
                        bucket = x;
                    }
                }
                if (bucket == null)
                {
                    bucket = sc.CreateBucket(projectId, bucketName);
                }
                // Kill all existing objects
                var objs = sc.ListObjects(bucketName);
                foreach (var x in objs)
                {
                    sc.DeleteObject(x);
                }
                // Upload the damned thing
                using (var f = File.OpenRead(fnAudio))
                {
                    sc.UploadObject(bucketName, objName, null, f);
                }
                // NOW RECOGNIZE
                var mat = transcribeFromObject("gs://" + bucketName + "/" + objName, langCode);
                return(mat);
            }
            finally
            {
                // Delete all objects in bucket
                if (bucket != null)
                {
                    var objs = sc.ListObjects(bucketName);
                    foreach (var x in objs)
                    {
                        sc.DeleteObject(x);
                    }
                }
                // Adios storage jerk
                if (sc != null)
                {
                    sc.Dispose();
                }
            }
        }
 public void Connect()
 {
     try
     {
         storageClient.CreateBucket(projectId, bucketName);
     }
     catch (Google.GoogleApiException e)
         when(e.Error.Code == 409)
         {
         }
 }
        public FileUploader(FileConverter fc)
        {
            _fileConverter = fc;

            var credentials = GoogleCredential.FromFile("auth.json");

            _client = StorageClient.Create(credentials);

            var now = DateTime.UtcNow;

            _bucket = _client.CreateBucket("kateSpeechRecognition", $"{now.Day}_{now.Month}_{now.Year}_{now.Hour}_{now.Minute}_{now.Second}");
        }
Beispiel #16
0
 public bool CreateBucket(string bucket)
 {
     try
     {
         _client.CreateBucket(_projectId, bucket);
         return(true);
     }
     catch (GoogleApiException e)
     {
         // Bucket already exist
         return(e.HttpStatusCode == HttpStatusCode.Conflict);
     }
 }
Beispiel #17
0
 static void CreateBucket(string projectId, string bucketName)
 {
     try
     {
         storageClient.CreateBucket(projectId, bucketName);
         Console.WriteLine($"{bucketName} oluşturuldu.");
     }
     catch (Google.GoogleApiException e)
         when(e.Error.Code == 409)
         {
             Console.WriteLine(e.Error.Message);
         }
 }
        public void PartiallyBrokenQuery()
        {
            string[] csvRows =
            {
                "First,Last,Age,Gender",
                "Victor,Mota,23,M",
                "v,m,break,M", // Not a valid age
                "\"\",Blank,23,F",
                "foo,bar,32,M"
            };

            // Create the object in GCS
            byte[]        bytes      = Encoding.UTF8.GetBytes(string.Join("\n", csvRows));
            StorageClient storage    = StorageClient.Create();
            string        bucketName = "bigquerytests-" + Guid.NewGuid().ToString().ToLowerInvariant();
            string        objectName = "file-" + Guid.NewGuid().ToString();

            storage.CreateBucket(_fixture.ProjectId, bucketName);
            storage.UploadObject(bucketName, objectName, "text/csv", new MemoryStream(bytes));

            // Create the table associated with it
            var client = BigQueryClient.Create(_fixture.ProjectId);
            var schema = new TableSchemaBuilder
            {
                { "First", BigQueryDbType.String },
                { "Last", BigQueryDbType.String },
                { "Age", BigQueryDbType.Int64 },
                { "Gender", BigQueryDbType.String },
            }.Build();
            var configuration = new ExternalDataConfiguration
            {
                SourceFormat = "CSV",
                CsvOptions   = new CsvOptions {
                    SkipLeadingRows = 1
                },
                MaxBadRecords = 1,
                SourceUris    = new[] { $"gs://{bucketName}/{objectName}" },
            };
            var table = client.CreateTable(_fixture.DatasetId, _fixture.CreateTableId(),
                                           schema, new CreateTableOptions {
                ExternalDataConfiguration = configuration
            });

            // Run a query
            var results = client.ExecuteQuery($"SELECT * FROM {table:legacy}", new QueryOptions {
                UseLegacySql = true
            });

            Assert.Equal(3, results.Count());
            Assert.Throws <GoogleApiException>(() => results.ThrowOnAnyError());
        }
 /// <summary>
 /// Creates the bucket
 /// </summary>
 /// <param name="client">Google storage client</param>
 /// <param name="projectId">Project ID to create the bucket in</param>
 /// <param name="bucketName">Name of the bucket</param>
 private static void CreateBucket(StorageClient client, string projectId, string bucketName)
 {
     try
     {
         client.CreateBucket(projectId, bucketName);
     }
     catch (GoogleApiException e)
     {
         if (e.HttpStatusCode != HttpStatusCode.Conflict)
         {
             throw new RebusApplicationException(e, "Unexpected Google Cloud Storage exception occurred");
         }
     }
 }
Beispiel #20
0
        private void MaybeCreateBucket()
        {
            try
            {
                _client.GetBucket(_bucket);
                return;
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                // Need to create the bucket...
                // (But let's do so outside the catch.)
            }
            Console.WriteLine($"Creating bucket '{_bucket}'");
            var serviceAccount = GoogleCredential.GetApplicationDefault().UnderlyingCredential as ServiceAccountCredential;

            if (serviceAccount is null)
            {
                throw new InvalidOperationException("Default credentials are not for a service account, so we can't determine the project ID");
            }
            _client.CreateBucket(serviceAccount.ProjectId, _bucket);
        }
Beispiel #21
0
 public StorageFixture()
 {
     Client = StorageClient.Create();
     Bucket = IdGenerator.FromDateTime(prefix: "tests-", suffix: "-data-protection");
     Client.CreateBucket(ProjectId, Bucket);
 }
 public GoogleClodStorageXmlRepository(GoogleCredential credential, string projectId, string bucketName)
 {
     _bucketName = bucketName;
     _client     = StorageClient.Create(credential);
     _bucket     = _client.GetBucket(_bucketName) ?? _client.CreateBucket(_bucketName, projectId);
 }
 public RandomBucketFixture()
 {
     BucketName = RandomBucketName();
     _storage.CreateBucket(_projectId, BucketName);
 }
Beispiel #24
0
 private RandomBucketFixture(string projectId)
 {
     BucketName = RandomBucketName();
     _storage.CreateBucket(projectId, BucketName);
 }
Beispiel #25
0
        private bool WriteDataTableToCSV(DataTable dt, string filename)
        {
            bool   result    = false;
            string projectID = "gocery-1098";

            //Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"‪C:\certs\gocery-1098-1b746097984e.json");
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "e:\\gcpkeys.json");
            //var credential = GoogleCredential.FromFile(@"‪‪E:/gocery-1098-c95031a7e4cb.p12");
            //var credential = GoogleCredential.GetApplicationDefault();
            StorageClient storageClient = StorageClient.Create();
            string        bucketName    = "ftt_testbucket";

            ///
            try
            {
                storageClient.CreateBucket(projectID, bucketName);
            }
            catch (GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    MessageBox.Show(e.Error.Message);
                }

            ///

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(memoryStream))
                {
                    try
                    {
                        IEnumerable <String> headers = dt.Columns.OfType <DataColumn>().Select(c => String.Concat("\"", c.ColumnName, "\""));
                        writer.WriteLine(String.Join(",", headers));

                        IEnumerable <String> row = null;
                        foreach (DataRow curRow in dt.Rows)
                        {
                            row = curRow.ItemArray.Select(o => o?.ToString() ?? String.Empty);
                            writer.WriteLine(String.Join(",", row));
                        }
                        writer.Flush();
                        File.WriteAllBytes(filename, memoryStream.ToArray());
                        result = true;
                    }
                    catch (Exception e)
                    {
                        result = false;
                    }
                }
            }
            if (result)
            {
                using (var f = File.OpenRead(filename))
                {
                    var objectName = Path.GetFileName(filename);
                    storageClient.UploadObject(bucketName, objectName, null, f);
                    MessageBox.Show("Uploaded");
                }
            }
            return(result);
        }
Beispiel #26
0
        public int Executar(string nomeDoArquivo, MemoryStream arquivo)
        {
            var bucketName = BucketName.RandomName();

            var outputPrefix = "";
            var gcsSourceURI = $"gs://{bucketName}/{nomeDoArquivo}";

            SortedDictionary <string, SortedSet <string> > _garbage = new SortedDictionary <string, SortedSet <string> >();
            StorageClient _storage = StorageClient.Create();

            _storage.CreateBucket(_projectId, bucketName);

            _storage.UploadObject(bucketName, nomeDoArquivo, "application/pdf", arquivo);

            SortedSet <string> objectNames;

            if (!_garbage.TryGetValue(bucketName, out objectNames))
            {
                objectNames = _garbage[bucketName] = new SortedSet <string>();
            }
            objectNames.Add(nomeDoArquivo);

            var client = ImageAnnotatorClient.Create();

            var asyncRequest = new AsyncAnnotateFileRequest
            {
                InputConfig = new InputConfig
                {
                    GcsSource = new GcsSource
                    {
                        Uri = gcsSourceURI
                    },
                    MimeType = "application/pdf"
                },
                OutputConfig = new OutputConfig
                {
                    // How many pages should be grouped into each json output file.
                    BatchSize      = 100,
                    GcsDestination = new GcsDestination
                    {
                        Uri = $"gs://{bucketName}/{outputPrefix}"
                    }
                }
            };

            asyncRequest.Features.Add(new Feature
            {
                Type = Feature.Types.Type.DocumentTextDetection
            });

            List <AsyncAnnotateFileRequest> requests = new List <AsyncAnnotateFileRequest>
            {
                asyncRequest
            };

            var operation = client.AsyncBatchAnnotateFiles(requests);

            operation.PollUntilCompleted();

            var blobList = _storage.ListObjects(bucketName, outputPrefix);
            var output   = blobList.Where(x => x.Name.Contains(".json")).First();

            var jsonString = "";

            using (var stream = new MemoryStream())
            {
                _storage.DownloadObject(output, stream);
                jsonString = System.Text.Encoding.UTF8.GetString(stream.ToArray());
            }


            var response = JsonParser.Default.Parse <AnnotateFileResponse>(jsonString);

            int total = 0;

            for (int i = 0; i < response.Responses.Count; i++)
            {
                var pageResponses = response.Responses[i];
                if (pageResponses != null)
                {
                    var annotation        = pageResponses.FullTextAnnotation;
                    var conteudo          = annotation.Text.Replace("\n", " ");
                    var remocaoDosEspacos = conteudo.Split(' ');

                    foreach (var item in remocaoDosEspacos)
                    {
                        total += item.Length;
                    }
                }
            }

            RemoverArquivos(bucketName);

            return(total);
        }
Beispiel #27
0
        public async Task <IActionResult> PostBookPdf(int userID, int bookID, IFormFile pdf)
        {
            if (pdf == null)
            {
                return(BadRequest(new BadRequestResponse("Body is not in the correct format")));
            }

            // Instantiates a client.
            StorageClient storageClient = StorageClient.Create();

            try
            {
                // Creates the new bucket if does not exist.
                var bucket = await storageClient.GetBucketAsync("api_project_books");

                if (bucket == null)
                {
                    CreateBucketOptions createBucketOptions = new CreateBucketOptions();
                    createBucketOptions.Projection = Projection.Full;

                    storageClient.CreateBucket("assignment1-179919", "api_project_books");
                }
            }
            catch (Google.GoogleApiException e) when(e.Error.Code == 409)
            {
                // The bucket already exists. That's fine.
                Console.WriteLine(e.Error.Message);
            }

            MemoryStream ms = new MemoryStream();

            var fileName = Guid.NewGuid().ToString();

            var fileType = pdf.FileName.Substring(pdf.FileName.LastIndexOf("."));

            fileName += fileType;

            if (pdf.Length == 0)
            {
                return(BadRequest(new BadRequestResponse("file cannot be empty")));
            }

            await pdf.CopyToAsync(ms);

            var result = await storageClient.UploadObjectAsync("api_project_books", userID + "/" + bookID + "/" + fileName, pdf.ContentType, ms);

            if (result == null)
            {
                try
                {
                    Book book = await Context.GetBookByID(bookID);

                    if (result == null)
                    {
                        return(NotFound(new DataNotFoundResponse("Book not found in database")));
                    }

                    Context.DeleteBook(bookID);

                    return(Ok());
                }
                catch (Exception e)
                {
                    return(BadRequest(new BadRequestResponse("Error deleting book")));
                }
            }

            return(Ok(new { downloadURL = "https://storage.googleapis.com/api_project_books/" + userID + "/" + bookID + "/" + fileName }));
        }