Gdrive
Google Drive services support.
Capabilities
This service can be used to:
- stat
- read
- write
- delete
- create_dir
- list
- copy
- rename
- batch
Configuration
root
: Set the work directory for backend
Credentials related
Just provide Access Token (Temporary)
access_token
: set the access_token for google drive api Please notice its expiration.
Or provide Client ID and Client Secret and refresh token (Long Term)
If you want to let OpenDAL to refresh the access token automatically, please provide the following fields:
refresh_token
: set the refresh_token for google drive apiclient_id
: set the client_id for google drive apiclient_secret
: set the client_secret for google drive api
OpenDAL is a library, it cannot do the first step of OAuth2 for you. You need to get authorization code from user by calling GoogleDrive's authorize url and exchange it for refresh token.
Make sure you have enabled Google Drive API in your Google Cloud Console.
And your OAuth scope contains https://www.googleapis.com/auth/drive
.
Please refer to GoogleDrive OAuth2 Flow for more information.
You can refer to [GdriveBuilder
]'s docs for more information
Example
Via Builder
use anyhow::Result;
use opendal::services::Gdrive;
use opendal::Operator;
#[tokio::main]
async fn main() -> Result<()> {
let mut builder = Gdrive::default();
builder.root("/test");
builder.access_token("<token>");
Ok(())
}
Via Config
- Rust
- Node.js
- Python
use anyhow::Result;
use opendal::Operator;
use opendal::Scheme;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<()> {
let mut map = HashMap::new();
map.insert("root".to_string(), "/path/to/dir".to_string());
map.insert("access_token".to_string(), "your_access_token".to_string());
let op: Operator = Operator::via_map(Scheme::Gdrive, map)?;
Ok(())
}
import { Operator } from "opendal";
async function main() {
const op = new Operator("gdrive", {
root: "/path/to/dir",
access_token: "your_access_token",
});
}
import opendal
op = opendal.Operator("gdrive",
root="/path/to/dir",
access_token="your_access_token",
)