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'
#the above file is service account's json file, not the outh key 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)
# Upload a file to a specific folder
file_path_to_upload = 'C:/Users/viral/Documents/instavideo.mp4' # Replace with the actual file path
file_name = os.path.basename(file_path_to_upload)
# Set the ID of the target folder where you want to upload the file
target_folder_id = '11cAJ4uhvAm7zpD0LdcfNWIgHMyD-IRxP' # Replace with the actual folder ID
file_metadata = {
'name': file_name,
'parents': [target_folder_id]
}
media = MediaFileUpload(file_path_to_upload, mimetype='application/octet-stream')
uploaded_file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print(f'Uploaded {file_name} with ID: {uploaded_file.get("id")}')
0 comments:
Post a Comment