How to Upload an mp3 File Produced with AudioStack to Google Drive

In this example we show you how to upload an mp3 file produced with AudioStack to Google Drive.

First create the mp3 file

Download the file to your folder

Install the packages you need


Sure! Below is a Python script that demonstrates how to upload a file to Google Drive using the Google
Drive API. This script uses the google-auth, google-auth-oauthlib, google-auth-httplib2, and
googleapiclient libraries, which you need to install if you haven't already:

pip install google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client

Connect to Google Cloud/Google Drive.

Steps to Use the Script:

Create a Google Cloud Project:

  • Go to the Google Cloud Console. Google Cloud Console

  • Create a new project.

  • Enable the Google Drive API for this project.

  • Set Up OAuth 2.0 Client IDs:

In the Google Cloud Console, go to the "Credentials" page.


  • Create an OAuth 2.0 Client ID. For the application type, choose "Desktop app".
  • Download the credentials.json file and place it in the same directory as your script.

πŸ“˜

Some things to watch out for

When you run the script for the first time, it will open a browser window for you to log in with your Google account and authorize the application.
The script will save the authorization token in a file named token.json, which will be used for subsequent runs.

🚧

File path name

Make sure to replace 'your_file_path_here' with the path to the file you want to upload and update the MIME type accordingly.

Here is the script

You'll need to run this script if everything above is created correctly.

import os
import google.auth
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload

# If modifying these SCOPES, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/drive.file']

def authenticate():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds, _ = google.auth.load_credentials_from_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())
    return creds

def upload_file(service, file_path, mime_type):
    file_metadata = {'name': os.path.basename(file_path)}
    media = MediaFileUpload(file_path, mimetype=mime_type)
    file = service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
    print(f"File ID: {file.get('id')}")

def main():
    creds = authenticate()
    service = build('drive', 'v3', credentials=creds)
    
    file_path = 'media_file_example.mp3'  # Update this to the file you want to upload
    mime_type = 'application/octet-stream'  # Update this to the correct MIME type
    upload_file(service, file_path, mime_type)

if __name__ == '__main__':
    main()



You'll see in your Google drive home the file you uploded