import os

from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account

# Path to the JSON key file (replace with your actual file path)
key_file_path = 'C:/Users/viral/Documents/driveproject-395513-88dcf65a35f9.json'

# Authenticate using the service account credentials
creds = service_account.Credentials.from_service_account_file(
    key_file_path, scopes=['https://www.googleapis.com/auth/drive.file']
)

# Build the Google Drive API service
service = build('drive', 'v3', credentials=creds)

# Retrieve the user's storage quota information
about = service.about().get(fields="storageQuota").execute()
storage_quota = about.get("storageQuota")

# Get the total storage limit and usage in bytes
total_storage = int(storage_quota.get("limit"))
usage_in_bytes = int(storage_quota.get("usage"))

# Convert the bytes value to gigabytes (GB)
def convert_bytes_to_gb(bytes_val):
    gb = bytes_val / (1024 ** 3)
    return f"{gb:.2f} GB"

total_storage_gb = convert_bytes_to_gb(total_storage)
usage_gb = convert_bytes_to_gb(usage_in_bytes)
# Calculate the free storage in bytes
free_storage_in_bytes = total_storage - usage_in_bytes

# Convert the free storage to gigabytes (GB)
free_storage_gb = convert_bytes_to_gb(free_storage_in_bytes)
# Print the storage quota information
print(f"Total storage limit: {total_storage_gb}")
print(f"Storage used: {usage_gb}")
print(f"Free storage: {free_storage_gb}")

0 comments:

Post a Comment

 
Top