简体   繁体   English

如何只将文件上传到谷歌驱动器一次?

[英]How to upload the files to google drive only once?

Using this code, I am uploading the files to google drive successfully.使用此代码,我将文件成功上传到谷歌驱动器。 But each time when I run this code, it uploads the same files to google drive again.但是每次我运行这段代码时,它都会再次将相同的文件上传到谷歌驱动器。 What I want is to stop this repetition of uploading the data and just upload the newly created file.我想要的是停止重复上传数据,只上传新创建的文件。

import os
import httplib2
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

http = httplib2.Http()
SCOPES = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('thermal-highway.json', SCOPES)
drive_service = build('drive', 'v3', http=credentials.authorize(http))

def upload_folders(name, folder_id):
    file_metadata = {
        'name': name,
        'mimeType': 'application/vnd.google-apps.folder',
        'parents': folder_id
    }
    file = drive_service.files().create(body=file_metadata, fields='id').execute()
    print('Folder ID: %s' % file.get('id'))
  
def list_files(path):
    folderId = ['62bewyZXHy9JfSLG9N96U1e548luuCAWR']
    for files in os.listdir(path):
        d = os.path.join(path, files)
        if os.path.isdir(d):
            upload_folders(files, folderId)

path = "/home/bilal/Videos/folder1/"
list_files(path)

Your code uses file.create which creates a new file each time.您的代码使用file.create每次都会创建一个新文件。 If you want to update an existing file then you should be using file.update如果要更新现有文件,则应使用file.update

# File's new content.
    media_body = MediaFileUpload(
        new_filename, mimetype=new_mime_type, resumable=True)

    # Send the request to the API.
    updated_file = service.files().update(
        fileId=file_id,
        body=file,
        media_body=media_body).execute()

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

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