public async Task <bool> ExistBucket(string key)
        {
            try
            {
                Bucket bucket = await _client.GetBucketAsync(key);

                return(bucket != null);
            }
            catch (GoogleApiException e)
                when(e.Error.Code == 404)
                {
                    return(false);
                }
        }
 /// <summary>
 /// Asychronously get the bucket.
 /// </summary>
 /// <param name="bucket">The bucket name.</param>
 /// <returns>The bucket or null if does not exist.</returns>
 public async Task <Bucket> GetBucketAsync(string bucket)
 {
     try
     {
         return(await _client.GetBucketAsync(bucket));
     }
     catch
     {
         return(default);
        private static async Task GetDataAsync(string path, GoogleCredential credential)
        {
            try
            {
                int           pos           = path.LastIndexOf("\\");
                string        strFileName   = path.Substring(pos + 1, path.Length - pos - 1);
                StorageClient storageClient = await StorageClient.CreateAsync(credential);

                storageClient.Service.HttpClient.Timeout = new TimeSpan(0, 10, 0);
                var bucket = await storageClient.GetBucketAsync("bucket ocr");

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);

                using (MemoryStream memStream = new MemoryStream())
                {
                    await fs.CopyToAsync(memStream);

                    Google.Apis.Storage.v1.Data.Object googleDataObject;
                    googleDataObject = await storageClient.UploadObjectAsync(bucket.Name, "sinProcesar/" + strFileName, "application/pdf", memStream);
                }
                var asyncRequest = new AsyncAnnotateFileRequest
                {
                    InputConfig = new InputConfig
                    {
                        GcsSource = new GcsSource
                        {
                            Uri = $"gs://{bucket.Name}/sinProcesar/{strFileName}"
                        },
                        MimeType = "application/pdf"
                    },
                    OutputConfig = new OutputConfig
                    {
                        BatchSize      = 2,
                        GcsDestination = new GcsDestination
                        {
                            Uri = $"gs://{bucket.Name}/procesados/{strFileName.Split('.')[0]}"
                        }
                    }
                };
                asyncRequest.Features.Add(new Feature
                {
                    Type = Feature.Types.Type.DocumentTextDetection
                });

                List <AsyncAnnotateFileRequest> requests = new List <AsyncAnnotateFileRequest>();
                requests.Add(asyncRequest);
                var client = new ImageAnnotatorClientBuilder
                {
                    CredentialsPath = @"D:\Google_API_key_TFM.json"
                }.Build();
                var operation = client.AsyncBatchAnnotateFiles(requests);
                operation.PollUntilCompleted();
            }
            catch (Exception e) { }
        }
        public async Task InitializeAsync(CancellationToken ct = default)
        {
            try
            {
                storageClient = StorageClient.Create();

                await storageClient.GetBucketAsync(bucketName, cancellationToken : ct);
            }
            catch (Exception ex)
            {
                throw new ConfigurationException($"Cannot connect to google cloud bucket '{bucketName}'.", ex);
            }
        }
        private static async Task <Bucket> SetUpGCP(StorageClient client, Bucket bucket)
        {
            try
            {
                bucket = await client.CreateBucketAsync("YOURPROJECTID", "BUCKETNAME", new CreateBucketOptions { PredefinedAcl = PredefinedBucketAcl.PublicReadWrite });
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 409)
                {
                    // The bucket already exists.  That's fine.
                    bucket = await client.GetBucketAsync("BUCKETNAME");
                }

            return(bucket);
        }
Beispiel #6
0
        public override async Task <string> SaveAsync(FileSetOptions fileSetOptions)
        {
            GoogleCredential credential = await AuthorizedAsync(fileSetOptions);

            StorageClient storageClinet = await StorageClient.CreateAsync(credential);

            PredefinedObjectAcl predefinedObjectAcl = PredefinedObjectAcl.ProjectPrivate;
            PredefinedBucketAcl predefinedBucketAcl = PredefinedBucketAcl.ProjectPrivate;

            switch (fileSetOptions.FileAccess)
            {
            case FileAccessLevel._private:
                predefinedObjectAcl = PredefinedObjectAcl.AuthenticatedRead;
                predefinedBucketAcl = PredefinedBucketAcl.AuthenticatedRead;
                break;

            case FileAccessLevel._public:
                predefinedObjectAcl = PredefinedObjectAcl.PublicRead;
                predefinedBucketAcl = PredefinedBucketAcl.PublicRead;
                break;
            }


            if (fileSetOptions.folderOptions == FolderOptions.CreateIfNull)
            {
                var folder = await storageClinet.GetBucketAsync(fileSetOptions.Folder);

                if (folder == null)
                {
                    await storageClinet.CreateBucketAsync(fileSetOptions.ProjectId, fileSetOptions.Folder, new CreateBucketOptions()
                    {
                        PredefinedAcl = predefinedBucketAcl, PredefinedDefaultObjectAcl = predefinedObjectAcl
                    });
                }
            }

            fileSetOptions._stream.Position = 0;

            await storageClinet.UploadObjectAsync(fileSetOptions.Folder, fileSetOptions.Key, fileSetOptions.ContentType, fileSetOptions._stream, new UploadObjectOptions()
            {
                PredefinedAcl = predefinedObjectAcl
            });

            return(fileSetOptions.Key);
        }
        private static async Task downloadFilesGoogle()
        {
            var           credential    = GoogleCredential.FromFile(@"D:\Google_API_key_TFM.json");
            StorageClient storageClient = await StorageClient.CreateAsync(credential);

            storageClient.Service.HttpClient.Timeout = new TimeSpan(0, 10, 0);
            var bucket = await storageClient.GetBucketAsync("bucket-ocr-tfm");

            var    blobList = storageClient.ListObjects(bucket.Name, "procesados/");
            string strPath  = @"D:\json\";

            foreach (var outputOcr in blobList.Where(x => x.Name.Contains(".json")))
            {
                using (var stream = File.OpenWrite(strPath + outputOcr.Name.Split('/')[1]))
                {
                    storageClient.DownloadObject(outputOcr, stream);
                }
            }
        }
Beispiel #8
0
        private async Task <bool> CreateBucketIfNotExists(StorageClient storage)
        {
            Bucket bucket = null;
            var    code   = -1;

            try
            {
                bucket = await storage.GetBucketAsync(IMAGES_BUCKET_NAME);
            }
            catch (GoogleApiException gae)
            {
                code = gae.Error.Code;
            }

            if (code == 404)
            {
                bucket = await storage.CreateBucketAsync("titan-185301", IMAGES_BUCKET_NAME);
            }

            return(bucket != null);
        }
Beispiel #9
0
        public async Task TestConnection()
        {
            try
            {
                await _client.GetBucketAsync(_bucketName, cancellationToken : CancellationToken);

                if (await _client.TestBucketIamPermissionsAsync(_bucketName, new[] { "storage.objects.create" }, cancellationToken: CancellationToken) == null)
                {
                    throw new InvalidOperationException(
                              $"Can't create an object in bucket '{_bucketName}', " +
                              $"The permission 'storage.objects.create' is missing");
                }
            }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 403)
                {
                    throw new InvalidOperationException($"Google credentials json does not have access to project {_projectId ?? "N/A"}", e);
                }
            catch (Google.GoogleApiException e)
                when(e.Error.Code == 404)
                {
                    throw new InvalidOperationException($"Bucket {_bucketName} not found", e);
                }
        }
Beispiel #10
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 }));
        }