简体   繁体   English

将xlsx上载到Google云端硬盘共享文件夹

[英]Upload xlsx into Google Drive shared folder

I would like to upload the file risk_pos.xlsx which is in the same directory as the .py file, into a shared drive. 我想将与.py文件位于同一目录中的文件risk_pos.xlsx上传到共享驱动器中。 Any help would be appreciated please. 任何帮助将不胜感激。

My code: 我的代码:

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
os.chdir(sys.path[0])
creds = ServiceAccountCredentials.from_json_keyfile_name("client_secret1.json", scope)
client = gspread.authorize(creds)

DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

files = DRIVE.files().list().execute().get('files', [])
for f in files:
    print(f['name'], f['mimeType'])


file_metadata = {'inventory': 'risk_pos.xlsx', 'parents': ['XXXXXXXXXXXXXXXX']}
media = MediaFileUpload('risk_pos.xlsx', mimetype='application/vnd.ms-excel')
files = DRIVE.files().create(body=file_metadata, supportsTeamDrives=True, media_body=media, fields='id').execute()

The error: 错误:

File "risk_pos3.py", line 59, in <module>
    files = DRIVE.files().create(body=file_metadata, supportsTeamDrives=True, media_body=media, fields='id').execute()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/googleapiclient/http.py", line 851, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting 
https://www.googleapis.com/upload/drive/v3/files?supportsTeamDrives=true&fields=id&alt=json&uploadType=multipart returned "File not found: XXXXXXXXXXXXXXXXXX.">

EDIT: 编辑:

I don't have error now. 我现在没有错误。 I just removed the brackets around the 'parents': ['XXXXXXXXXXXXXXXX'] But I don't see the file showing up in Drive. 我只是删除了'parents': ['XXXXXXXXXXXXXXXX']周围的括号'parents': ['XXXXXXXXXXXXXXXX']但我没有看到文件显示在云端硬盘中。 Still every time i run the process it seems that something new appear. 每次我运行该过程时,似乎都会出现一些新的东西。 A file named untitled... as below you can see the number of file increasing. 名为untitled...的文件如下所示,您可以看到文件数量在增加。 But no untitled... files in Drive. 但云端硬盘中没有untitled...文件。

 JRACMB-MahfoudD:market_risk delalma$ python3 risk_pos3.py 
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Market_data application/vnd.google-apps.spreadsheet
 Market_data_volume application/vnd.google-apps.spreadsheet
 Getting started application/pdf
JRACMB-MahfoudD:market_risk delalma$ python3 risk_pos3.py 
Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Market_data application/vnd.google-apps.spreadsheet
 Market_data_volume application/vnd.google-apps.spreadsheet
 Getting started application/pdf
 JRACMB-MahfoudD:market_risk delalma$ python3 risk_pos3.py 
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Untitled application/vnd.ms-excel
 Market_data application/vnd.google-apps.spreadsheet
 Market_data_volume application/vnd.google-apps.spreadsheet
 Getting started application/pdf

Service accounts are tricky. 服务帐户非常棘手。 If there is no error with the upload then its probably being uploaded the question is where is it being uploaded to? 如果上传没有错误,那么它可能正在上传,问题是将其上传到哪里?

A service account is not you its a dummy user with its own drive account. 服务帐户不是您具有自己的驱动器帐户的虚拟用户。 You may have uploaded the file to the service accounts drive account. 您可能已将文件上传到服务帐户驱动器帐户。 The first thing i would do is run a file.list can you see the file then? 我要做的第一件事是运行file.list,然后可以看到该文件吗?

The thing is you say that you are using parents in your request. 问题是您说您在要求中使用父母。 Which means either you created a new folder on the service account and are using that, or you have shared a folder on your personal drive account and are trying to upload to that. 这意味着您要么在服务帐户上创建了一个新文件夹并正在使用该文件夹,要么您已经在个人驱动器帐户上共享了一个文件夹并尝试上载到该文件夹​​。 Either way a file.list will help you figure out where the file is if the service account uploaded it it has access to it its going to be the best easiest way to find the file. 无论哪种方式,file.list都会帮助您确定文件在哪里,如果服务帐户上传了该文件,则它可以访问该文件,这将是查找文件的最简便方法。

untitled 无标题

Files are uploaded in two parts. 文件分为两部分上传。 First the metadata containing things like the files name and second the actual data of the file itself. 首先,元数据包含文件名之类的内容,其次是文件本身的实际数据。 Your problem is that you are not uploading the metadata. 您的问题是您没有上传元数据。

file_metadata = {'name': 'photo.jpg'}
media = MediaFileUpload('files/photo.jpg',
                        mimetype='image/jpeg')
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print 'File ID: %s' % file.get('id')

You may want to read managing uploads 您可能需要阅读管理上传

As for the always creating a new file that is logical considering that you are sending file.create then a new file will always be created if you want it to over write an existing file then you will need to do file.update 至于总是创建一个逻辑上的新文件(考虑到您正在发送file.create),那么如果您要覆盖现有文件,则总是会创建一个新文件,那么您需要执行file.update

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

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