Skip to content

Latest commit

 

History

History
85 lines (63 loc) · 2.57 KB

google_drive.md

File metadata and controls

85 lines (63 loc) · 2.57 KB

FastAPI Cloud Drives

Status GitHub Issues GitHub Pull Requests License

🧐 About

The FastAPI Cloud Drives module supports Google Drive, OneDrive, Dropbox cloud storage providers. You can easily search, upload, download files from this cloud providers.

Step 1

Go to link https://developers.google.com/drive/api/v3/quickstart/python Enable Drive Api. Download credentials.json file

Step 2

Before deploying app to production you need one time approve and give permission. Run:

python main.py --noauth_local_webserver

Follow instruction, get verification code from Google and paste it in terminal. After successful authentication, module will create storage.json file.

If you change permissions in Google Cloud for the application, you need repeat Step 2 again.

Step 3

Run application:

uvicorn main:app --reload

Example:

from fastapi_cloud_drives import GoogleDrive
from fastapi_cloud_drives import GoogleDriveConfig

from fastapi import FastAPI
from fastapi.responses import JSONResponse

google_conf = {
    "CLIENT_ID_JSON" : "client_id.json",
    "SCOPES": [
        "https://www.googleapis.com/auth/drive"
        ]
}

config = GoogleDriveConfig(**google_conf)

app = FastAPI()

@app.get("/list_files")
async def list_files():
    f = await gdrive.list_files()
    return JSONResponse(status_code=200, content=f)

@app.get("/upload_file")
async def upload_file():
    resp = await gdrive.upload_file(
        filename = "photo.jpg",
        filepath = "files/photo.jpg",
    )
    return JSONResponse(status_code=200, content=resp)

@app.get("/create_folder")
async def create_folder():
    resp = await gdrive.create_folder(folder_name="Examples")
    return JSONResponse(status_code=200, content=resp)

@app.get("/download_file")
async def download_file():
    resp = await gdrive.download_file(file_name = "photo.jpeg")
    return JSONResponse(status_code=200, content=resp)

CLIENT_ID_JSON is a file that you download from Google Cloud.

For more information about SCOPES go to: https://developers.google.com/identity/protocols/oauth2/scopes