google_cloud_service.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import os
  2. from pprint import pprint
  3. from google.cloud import storage
  4. import json
  5. warnings.filterwarnings("ignore")
  6. class Goggle_cloud_operations(Cloud_operations):
  7. """Goggle_cloud_operations are dervived class from Cloud_operations
  8. This class need credentials like access_key,secret_access_key, bucket name, region which is manatory,
  9. client scret key json which should be available inthe local path
  10. To create client.json file please refer link below
  11. Creating and managing service account keys:
  12. https://cloud.google.com/iam/docs/creating-managing-service-account-keys
  13. Set up your first Google Cloud Project and download Client File (JSON file):
  14. https://www.dezyre.com/recipes/upload-files-to-google-drive-using-python
  15. https://d35mpxyw7m7k7g.cloudfront.net/bigdata_1/Get+Authentication+for+Google+Service+API+.pdf
  16. Manage Files in Google Cloud Storage With Python
  17. https://hackersandslackers.com/manage-files-in-google-cloud-storage-with-python/
  18. Storage classes
  19. https://cloud.google.com/storage/docs/storage-classes
  20. """
  21. def __init__(self, bucket, region, client_config_path):
  22. """Intilizing all the needed credentials during object creation and validating it"""
  23. self.__bucket = bucket
  24. self.__region = region
  25. # self.__storage_client = storage_client
  26. self.__client_config_path = client_config_path
  27. self.validate_login_credentials()
  28. print("Google cloud operations started")
  29. def set_credentails_with_env(self, client_config_path):
  30. """environ varialbe GOOGLE_APPLICATION_CREDENTIALS is set, this is one time activity """
  31. try:
  32. os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.join(
  33. self.__client_config_path)
  34. print("Google Client config path set success")
  35. return True
  36. except:
  37. raise Exception(
  38. 'Faild to set up environment varialbe for Google cloud storage')
  39. def validate_login_credentials(self):
  40. """validate_login_credentials validates the given credentials from user,
  41. This method is from parent class Cloud_operations"""
  42. self.set_credentails_with_env(self.__client_config_path)
  43. try:
  44. self.__storage_client = storage.Client() # try to validate credentials if success method return true else stop here
  45. print("Google Client Authentication set success")
  46. except:
  47. raise Exception('Authentication failed for Google cloud storage')
  48. def upload_files(self, list_of_files):
  49. self.count = 0
  50. print("Sample of file to be uploaded to Google storage are \t {}".format(
  51. list_of_files))
  52. bucket = self.__storage_client.get_bucket(self.__bucket)
  53. for each_file in tqdm(list_of_files):
  54. try:
  55. blob_name = os.path.split(each_file)[-1]
  56. blob = bucket.blob(blob_name)
  57. blob.upload_from_filename(
  58. each_file) # .upload_file from google cloud api package uploads file to google storage
  59. self.count += 1
  60. except Exception as e:
  61. print("Failed to upload file {} \n{}".format(each_file, e))
  62. print("Total file succefully uploaded:\t {}".format(self.count))
  63. return True