簡體   English   中英

如何使用python代碼將Android上的文件上傳到Google雲端硬盤

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

我正在使用以下python代碼從Linux上的命令將視頻上傳到我的Google雲端硬盤。 由於基於Linux內核的android可以在android上使用? 如果沒有,是否有任何Android服務可以在后台上傳視頻(以太坊上傳到Google雲端硬盤或DropBox)?

$ python uploader.py video_file.avi

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)

我真的不明白為什么您要在Android上使用Python? Android可以通過Google Play服務使用Google Drive API。 此處有很好的文檔記錄: https : //developers.google.com/drive/android/get-started

您也可以檢查官方的Quickstart應用程序: https : //github.com/googledrive/android-quickstart

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM