https://github.com/fclairamb/afero-gdrive
https://github.com/fclairamb/afero-gdrive
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/fclairamb/afero-gdrive
- Owner: fclairamb
- License: mit
- Created: 2020-11-20T23:34:15.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-05T18:27:24.000Z (about 1 year ago)
- Last Synced: 2025-04-10T05:48:19.493Z (about 1 year ago)
- Language: Go
- Size: 3.58 MB
- Stars: 3
- Watchers: 2
- Forks: 6
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Afero Google Drive Driver

[](https://codecov.io/gh/fclairamb/afero-gdrive)
[](https://goreportcard.com/report/fclairamb/afero-gdrive)
[](https://godoc.org/github.com/fclairamb/afero-gdrive)
## About
It provides an [afero filesystem](https://github.com/spf13/afero/) implementation of a [Google Drive](https://developers.google.com/drive) backend.
This was created to provide a backend to the [ftpserver](https://github.com/fclairamb/ftpserver) but can definitely be used in any other code.
I'm very opened to any improvement through issues or pull-request that might lead to a better implementation or even better testing.
## Key points
- Download & upload file streaming
- 60% coverage: This isn't great, I intend to improve it to reach 80%. As it's a third-party API more would take way too much time.
- Very carefully linted
## Known limitations
- File appending / seeking for write is not supported because Google Drive doesn't support it, it could be simulated by rewriting entire files.
- Chmod is saved as a property and not used at this time.
- No files listing cache. This means that every directory of a path results in a request that happens every single time a path is analyzed. In other word opening `/a/b/c/file.txt` for writing will create at least 4 request, and the same will happen for reading.
## How to use
Note: Errors handling is skipped for brevity but you definitely have to handle it.
```golang
import (
"github.com/fclairamb/afero-gdrive/oauthhelper"
)
func main() {
// We declare the OAuh2 app
helper := oauthhelper.Auth{
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
Authenticate: func(url string) (string, error) {
return "", ErrNotSupported
},
}
// Pass a token
token, _ := base64.StdEncoding.DecodeString(os.Getenv("GOOGLE_TOKEN"))
helper.Token = new(oauth2.Token)
json.Unmarshal(token, helper.Token)
// Initialize the authenticated client
client, _ := helper.NewHTTPClient(context.Background())
// Initialize the FS from the athenticated http client
fs, _ := New(client)
// And use it
file, _ := fs.OpenFile("my_file.txt", os.O_WRONLY, 0777)
file.WriteString("Hello world !")
file.Close()
}
```
## How to run the tests
Follow [these instructions](https://github.com/fclairamb/afero-gdrive/tree/main/testenvhelper).
## Credits
This is a fork from [T4cC0re/gdriver](https://github.com/T4cC0re/gdriver) which is itself a fork of [eun/gdriver](https://github.com/eun/gdriver).
The code was massively modified. Most of the changes are improvements. The file listing API from the original implemented based on callbacks is much better though, but this was needed to respect the afero API.