Upload to Google Drive without any UI?

后端 未结 4 482
北荒
北荒 2021-02-03 21:57

I am looking to write a server application to upload files to Google Drive. I was previously using the Documents List API, but I see that is deprecated. I\'d like to move to the

相关标签:
4条回答
  • 2021-02-03 22:09

    Your application needs the user's permission to use the API against his files. That authorisation needs to happen using web based Oauth. The result of that authorisation is your server app ends up with a refresh token, which it can store. At any time, your app can convert that refresh token to an access token and access the drive files.

    So, provided you accept that you need to do a one-time authorisation, you can achieve what you are looking for.

    0 讨论(0)
  • 2021-02-03 22:13

    Here are the instructions for Windows, Linux and MacOS using GitHub | gdrive.

    $ gdrive list
    Go to the url... enter the oauth verification code... OK
    $ gdrive upload file
    $ gdrive mkdir UploadDir
    ID_of_UploadDir
    $ gdrive sync upload LocalDir ID_of_UploadDir
    
    0 讨论(0)
  • 2021-02-03 22:27

    Since the gdrive tool is no longer maintained. I found an official and better way.

    Using curl.

    1. Install Curl

    sudo apt install curl

    1. Create your project credentials on google console. Make an account first if you don't have any.

    Create credentials > Configure OAth consent screen (if needed) > Application type > TV and limited input devices > Save your client id and a client secret.

    1. Verify the device

    curl -d "client_id=<client_id>&scope=https://www.googleapis.com/auth/drive.file" https://oauth2.googleapis.com/device/code

    Expected response:

    {"device_code": "<long string>",
    "user_code": "xxx-xxx-xxx",
    "expires_in": 1800,
    "interval": 5,
    "verification_url": "https://www.google.com/device"}
    

    Then go to the https://www.google.com/device --> Enter "user_code" --> Give relevant permissions.

    1. Save the "device_code" and "user_code" values.
    2. Get Bearer code

    curl -d client_id=<client id> -d client_secret=<client secret> -d device_code=<device code> -d grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code https://accounts.google.com/o/oauth2/token

    Expected Output:

    {
    "access_token": ".....",
    "expires_in": 3599,
    "refresh_token": "....",
    "scope": "https://www.googleapis.com/auth/drive.file",
    "token_type": "Bearer"
    }
    
    1. Save the "access_token" value.

    2. Start uploading

    curl -X POST -L -H "Authorization: Bearer <enter access_token here>" -F "metadata={name :'filename.zip'};type=application/json;charset=UTF-8" -F "file=@filename.zip;type=application/zip" "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"

    CAUTION

    Make zip archives of your files before uploading. The above code works for archived files. I uploaded 10gb archive with the above method successfully.

    Source

    0 讨论(0)
  • 2021-02-03 22:36

    gdrive solution is not working as of now(login problems). So you can now use rclone. You can install it with

    conda install -c conda-forge rclone
    

    Then follow the configuration doc https://rclone.org/drive/
    After configuration you will be able to copy to google drive with this command(stat flags are for progress bar)

    rclone copy <filename> remote: --stats-one-line -P --stats 2s
    

    rclone has many backends included so you can upload not only to google drive

    0 讨论(0)
提交回复
热议问题