简体   繁体   English

如何使用python代码将Android上的文件上传到Google云端硬盘

[英]How to upload file on android to Google Drive using python code

I am using the following python code to upload video to my Google Drive from the command on Linux. 我正在使用以下python代码从Linux上的命令将视频上传到我的Google云端硬盘。 Is it possible to used on android since android based on Linux kernel? 由于基于Linux内核的android可以在android上使用? if not, is there any android service to upload the video in background (ether to Google Drive or DropBox)? 如果没有,是否有任何Android服务可以在后台上传视频(以太坊上传到Google云端硬盘或DropBox)?

$ python uploader.py video_file.avi $ python uploader.py video_file.avi

uploader.py : uploader.py:

#!/usr/bin/python
import smtplib
import os.path
import sys
import gdata.data
import gdata.docs.data
import gdata.docs.client
import ConfigParser

from curses import ascii              


class MotionUploader:
    def __init__(self):


        self.password = "my Gmail password"
        self.sender   = "myGmail@gmail.com"                 

        # Folder (or collection) in Docs where you want the videos to go
        self.folder = 'motion'


        self._create_gdata_client()

    def _create_gdata_client(self):
        """Create a Documents List Client."""
        self.client = gdata.docs.client.DocsClient(source='motion_uploader')
        self.client.http_client.debug = False
        self.client.client_login(self.sender, self.password, service=self.client.auth_service, source=self.client.source)

    def _get_folder_resource(self):
        """Find and return the resource whose title matches the given folder."""
        col = None
        for resource in self.client.GetAllResources(uri='/feeds/default/private/full/-/folder'):
            if resource.title.text == self.folder:
                col = resource
                break    
        return col


    def _upload(self, video_file_path, folder_resource):
        '''Upload the video and return the doc'''
        doc = gdata.docs.data.Resource(type='video', title=os.path.basename(video_file_path))
        media = gdata.data.MediaSource()
        media.SetFileHandle(video_file_path, 'video/avi')
        doc = self.client.CreateResource(doc, media=media, collection=folder_resource)
        return doc

    def upload_video(self, video_file_path):
        """Upload a video to the specified folder"""

        folder_resource = self._get_folder_resource()
        if not folder_resource:
            raise Exception('Could not find the %s folder' % self.folder)

        doc = self._upload(video_file_path, folder_resource)


        video_link = None
        for link in doc.link:             
             if 'docs.google.com/file/d' in link.href:
                 video_link = link.href
                 break  


if __name__ == '__main__':         
    try:
        if len(sys.argv) < 2:
            exit('Usage: uploader.py {config-file-path} {video-file-path}')

        vid_path = sys.argv[1]        
        if not os.path.exists(vid_path):
            exit('Video file does not exist [%s]' % vid_path)    
        MotionUploader().upload_video(vid_path)        
    except gdata.client.BadAuthentication:
        exit('Invalid user credentials given.')
    except gdata.client.Error:
        exit('Login Error')
    except Exception as e:
        exit('Error: [%s]' % e)

I do not really see why you would want to use Python on Android? 我真的不明白为什么您要在Android上使用Python? Android can use the Google Drive API through the Google Play Services. Android可以通过Google Play服务使用Google Drive API。 It is very well documented here: https://developers.google.com/drive/android/get-started . 此处有很好的文档记录: https : //developers.google.com/drive/android/get-started

You could check the official Quickstart App as well: https://github.com/googledrive/android-quickstart . 您也可以检查官方的Quickstart应用程序: https : //github.com/googledrive/android-quickstart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM