python 用于将文件夹上传到Google云端硬盘的Python脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 用于将文件夹上传到Google云端硬盘的Python脚本相关的知识,希望对你有一定的参考价值。

client_config_backend: file
client_config_file: client_secrets.json

save_credentials: True
save_credentials_backend: file
save_credentials_file: credentials.txt

get_refresh_token: True
oauth_scope:
  - https://www.googleapis.com/auth/drive
  - https://www.googleapis.com/auth/drive.install
google-api-python-client
PyDrive
# -*- coding: utf-8 -*-

"""
    Upload folder to Google Drive
"""

# Enable Python3 compatibility
from __future__ import (unicode_literals, absolute_import, print_function,
                        division)

# Import Google libraries
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from pydrive.files import GoogleDriveFileList
import googleapiclient.errors

# Import general libraries
from argparse import ArgumentParser
from os import chdir, listdir, stat
from sys import exit
import ast


def parse_args():
    """ 
		Parse arguments
	"""

    parser = ArgumentParser(
        description="Upload local folder to Google Drive")
    parser.add_argument('-s', '--source', type=str, 
						help='Folder to upload')
    parser.add_argument('-d', '--destination', type=str, 
						help='Destination Folder in Google Drive')
    parser.add_argument('-p', '--parent', type=str, 
						help='Parent Folder in Google Drive')

    return parser.parse_args()


def authenticate():
    """ 
		Authenticate to Google API
	"""

    gauth = GoogleAuth()

    return GoogleDrive(gauth)


def get_folder_id(drive, parent_folder_id, folder_name):
    """ 
		Check if destination folder exists and return it's ID
	"""

    # Auto-iterate through all files in the parent folder.
    file_list = GoogleDriveFileList()
    try:
        file_list = drive.ListFile(
			{'q': "'{0}' in parents and trashed=false".format(parent_folder_id)}
		).GetList()
	# Exit if the parent folder doesn't exist
    except googleapiclient.errors.HttpError as err:
		# Parse error message
        message = ast.literal_eval(err.content)['error']['message']
        if message == 'File not found: ':
            print(message + folder_name)
            exit(1)
		# Exit with stacktrace in case of other error
        else:
            raise

	# Find the the destination folder in the parent folder's files
    for file1 in file_list:
        if file1['title'] == folder_name:
            print('title: %s, id: %s' % (file1['title'], file1['id']))
            return file1['id']


def create_folder(drive, folder_name, parent_folder_id):
    """ 
		Create folder on Google Drive
	"""
    
	folder_metadata = {
        'title': folder_name,
        # Define the file type as folder
        'mimeType': 'application/vnd.google-apps.folder',
		# ID of the parent folder        
		'parents': [{"kind": "drive#fileLink", "id": parent_folder_id}]
    }

    folder = drive.CreateFile(folder_metadata)
    folder.Upload()

    # Return folder informations
    print('title: %s, id: %s' % (folder['title'], folder['id])
    return folder[(id']


def upload_files(drive, folder_id, src_folder_name):
    """ 
		Upload files in the local folder to Google Drive 
	"""

	# Enter the source folder
    try:
        chdir(src_folder_name)
	# Print error if source folder doesn't exist
    except OSError:
        print(src_folder_name + 'is missing')
	# Auto-iterate through all files in the folder.
    for file1 in listdir('.'):
		# Check the file's size
        statinfo = stat(file1)
        if statinfo.st_size > 0:
            print('uploading ' + file1)
            # Upload file to folder.
            f = drive.CreateFile(
                {"parents": [{"kind": "drive#fileLink", "id": folder_id}]})
            f.SetContentFile(file1)
            f.Upload()
		# Skip the file if it's empty
        else:
            print('file {0} is empty'.format(file1))


def main():
    """ 
		Main
	"""
    args = parse_args()

    src_folder_name = args.source
    dst_folder_name = args.destination
    parent_folder_name = args.parent

	# Authenticate to Google API
	drive = authenticate()
	# Get parent folder ID
    parent_folder_id = get_folder_id(drive, 'root', parent_folder_name)
	# Get destination folder ID
    folder_id = get_folder_id(drive, parent_folder_id, dst_folder_name)
	# Create the folder if it doesn't exists
    if not folder_id:
        print('creating folder ' + dst_folder_name)
        folder_id = create_folder(drive, dst_folder_name, parent_folder_id)
    else:
        print('folder {0} already exists'.format(dst_folder_name))

	# Upload the files    
	upload_files(drive, folder_id, src_folder_name)


if __name__ == "__main__":
    main()

以上是关于python 用于将文件夹上传到Google云端硬盘的Python脚本的主要内容,如果未能解决你的问题,请参考以下文章

使用Visual Basic将文件上传到Google云端硬盘

markdown 使用HTML表单将本地文件上传到Google云端硬盘而无需授权

使用javascript sdk将文件上传到Google云端硬盘

使用VBA上传到Google云端硬盘?

Google Forms文件上传完整示例

python 适用于Google云端硬盘的Python终端客户端,可轻松上传,删除,列出,共享文件或文件夹。 (参见评论中的用法)