简体   繁体   English

谷歌驱动Python上传文件不起作用

[英]Google drive Python to upload file not working

I'm using below code,fileID is getting generated but file is not uploaded.我正在使用以下代码,正在生成文件 ID,但未上传文件。 I tried debugged but it shows same output as code output.我试过调试,但它显示的输出与代码输出相同。 I have placed the service account json file and test.csv in the folder where the python code is placed.我已经将服务帐号json文件和test.csv放在了python代码所在的文件夹中。

from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload




def uploadGdrive():
    SCOPES = ['https://www.googleapis.com/auth/drive.file']
    SERVICE_ACCOUNT_FILE = './iron-tea-266706-8a1f9ee69710.json'
    folder_id = '15xc7s-dFnpW6pjtjtUH4kT1BnZ46ffFh'
    #file_id = '1IcbtXGW1ZMmv64SqqXJZCsFrwMlE3NiQcDZR98XuFX8'
    file_location='./test.csv'

    creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
    service = build('drive', 'v3', credentials=creds)
    file_metadata = {
        'name': 'Master Summary',
        'mimeType': 'application/vnd.google-apps.spreadsheet'
    }

    media = MediaFileUpload(
        '{}'.format(file_location),
        mimetype='text/csv',
        resumable=True)

    #This is to update existing file
    #file = service.files().update(body=file_metadata,media_body=media,fileId='{}'.format(file_id)).execute()
    #print(file.get('id'))

    #this is to create new  file
    file = service.files().create(body=file_metadata,
                                   media_body=media,
                                   fields='id').execute()
    print('File ID: %s' % file.get('id'))

if __name__ == '__main__':
    uploadGdrive()

A service account is not you.服务帐户不是您。 A service account is a dummy user, it has its own drive account.服务帐户是虚拟用户,它有自己的驱动器帐户。 Files it uploads are owned by it.它上传的文件归它所有。

Your file is probably being uploaded to the service accounts drive account.您的文件可能正在上传到服务帐户驱动器帐户。 or where ever '15xc7s-dFnpW6pjtjtUH4kT1BnZ46ffFh' is assuming that the service account has access to write to that directory.或者“15xc7s-dFnpW6pjtjtUH4kT1BnZ46ffFh”假设服务帐户有权写入该目录。 You can see this by doing a file.list and seeing which files it currently has access to.您可以通过执行 file.list 并查看它当前有权访问哪些文件来查看这一点。

If the file to create was successful it will return a file.id as part of the response you can check that as well.如果要创建的文件成功,它将返回 file.id 作为响应的一部分,您也可以检查它。

You could create a directory on your own google drive account share that directory with the service account.您可以在自己的 google drive 帐户上创建一个目录,与服务帐户共享该目录。 Then have the service account upload files into that directory then you would be able to see them on your own account.然后让服务帐户将文件上传到该目录中,然后您就可以在自己的帐户上看到它们。 You would also need to grant yourself permissions to the file when its uploaded.您还需要在上传文件时授予自己对该文件的权限。 Look into permissions.create.查看permissions.create。

Alternatively you could have the service account upload the files to its own account and then add permissions for you to the file so that you would be able to see it.或者,您可以让服务帐户将文件上传到其自己的帐户,然后为您添加对该文件的权限,以便您能够查看它。 Look into permissions.create.查看permissions.create。

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

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