Ejemplo n.º 1
0
        public async Task<string> getBucketImage(string eventImageName,string savingPath, string bucketName)
        {
            string imagePath = savingPath + eventImageName;
            try
            {
                if (eventImageName.Length != 0 && savingPath.Length != 0)
                {
                    string googleCloudEnvVar = SetEnvironmentVariable.getGoogleCloudEnvironmentVariable();
                    GoogleCredential credential = GoogleCredential.FromFile(googleCloudEnvVar);
                    var storage = StorageClient.Create(credential);

                    using (var outputFile = File.OpenWrite(savingPath+ @"\" + eventImageName))
                    {
                        
                        await storage.DownloadObjectAsync(bucketName, eventImageName, outputFile);
                    }
                    Console.WriteLine($"downloaded {eventImageName} to {savingPath}.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            return imagePath;
        }
Ejemplo n.º 2
0
        public async Task<bool> deleteImageFromCloud(ImageEntity imageEntity,string bucketName)
        {
            bool imageDeleted = false;
            try
            {
                if (imageEntity != null)
                {

                    string googleCloudEnvVar = SetEnvironmentVariable.getGoogleCloudEnvironmentVariable();
                    GoogleCredential credential = GoogleCredential.FromFile(googleCloudEnvVar);
                    var storageClient = StorageClient.Create(credential);

                    await storageClient.DeleteObjectAsync(bucketName, imageEntity.link);
                    Console.WriteLine("uploaded the file successfully");
                    imageDeleted = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return imageDeleted;
        }
Ejemplo n.º 3
0
        public string addImage(string imagePath, string imageId,string bucketName)
        {
            SetEnvironmentVariable.setGoogleCloudEnvironmentVariable();
            string eventImageLink = "";
            try
            {
                if (imagePath.Length != 0)
                {
                    string sharedkeyFilePath = SetEnvironmentVariable.getGoogleCloudEnvironmentVariable();
                    GoogleCredential credential = null;
                    using (var jsonStream = new FileStream(sharedkeyFilePath, FileMode.Open,
                        FileAccess.Read, FileShare.Read))
                    {
                        credential = GoogleCredential.FromStream(jsonStream);
                    }
                    var storageClient = StorageClient.Create(credential);
                    string[] spliters = { @"\" };

                    string filetoUpload = imagePath;
                    string[] splitedString = imagePath.Split(spliters, StringSplitOptions.RemoveEmptyEntries);
                    string fileName = splitedString[splitedString.Length - 1];


                    string[] spliters1 = { "." };

                    string[] splitedFileName = fileName.Split(spliters1, StringSplitOptions.RemoveEmptyEntries);
                    string newFileName = "";
                    for (int i = 0; i < splitedFileName.Length; i++)
                    {
                        if (i != 0)
                        {
                            newFileName += ".";
                        }
                        newFileName += splitedFileName[i];
                        if (i == splitedFileName.Length - 2)
                        {
                            newFileName = newFileName + imageId;
                        }
                    }
                    eventImageLink = newFileName;
                    //check if object with name like this exists
                    try
                    {
                        Google.Apis.Storage.v1.Data.Object GoogleFirestoreObject = storageClient.GetObject(bucketName, fileName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("GoogleFirestoreObject does not exist");
                    }
                    using (var fileStream = new FileStream(filetoUpload, FileMode.Open,
                        FileAccess.Read, FileShare.Read))
                    {
                        storageClient.UploadObject(bucketName, newFileName, "text/plain", fileStream);

                    }
                    Console.WriteLine("uploaded the file successfully");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            SetEnvironmentVariable.setFirestoreEnvironmentVariable();
            return eventImageLink;
        }