The FastAPI Cloud Drives module supports Google Drive, OneDrive, Dropbox cloud storage providers. You can easily search, upload, download files from this cloud providers.
Go to link https://developers.google.com/drive/api/v3/quickstart/python Enable Drive Api. Download credentials.json file
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.
Run application:
uvicorn main:app --reload
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