How to rename Google Drive file with Python - python

how are you doing?
I'm trying to rename some files in my Google Drive folder, following the instructions listed in documentation.
However I'm getting an error in one of the arguments (FileNotFoundError: [Errno 2] No such file or directory: 'trying_new_filename_string')
I ONLY NEED TO RENAME THE DOCUMENT, NOTHING ELSE.
This is my code:
service = build('drive', 'v3', credentials=credentials)
file_id = '1TKxcYDzEK3SUjSv6dCMM6WkKRmZwm84SPWVhR1F2DEc'
new_title = 'trying_new_title'
new_mime_type = 'trying_new_mime_type'
new_filename = 'trying_new_filename_string'
from apiclient import errors
from apiclient.http import MediaFileUpload
def update_file(service, file_id, new_title, new_mime_type, new_filename):
# First retrieve the file from the API.
file = service.files().get(fileId=file_id).execute()
# File's new metadata.
file['name'] = new_title
file['mimeType'] = new_mime_type
# 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()
return updated_file

Seems related to the fact the new_filename does not exist. As the MediaUploadFile tries to open and does not find a file with the trying_new_filename_string name.
As you only need to rename that file, don't change its content, you should remove the MediaUploadFile method.
Try:
def renameFile(service, fileId, newTitle):
body = {'name': newTitle}
return service.files().update(fileId=fileId, body=body).execute()

Related

Cannot upload certain type in Drive using Google Drive API

I am trying to update some files in my drive using the following piece of code
from apiclient.discovery import build
from apiclient.http import MediaIoBaseDownload, MediaFileUpload
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import io
import pandas as pd
PATH_FOLDER = './'
scopes = ['https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scopes)
http_auth = credentials.authorize(Http())
drive = build('drive', 'v3', http=http_auth)
def upload_file(file_name):
""" This function purpose is to find
a file in the drive having the same name as the
file_name parameter then uploads a new version
using the local file having the same name
param:
file_name(str) : name of the file to update in drive (also
name of the local file to use as new version)
"""
files = drive.files().list().execute()['files']
for file in files:
if file['name'] == file_name:
file_id = file['id']
break
file_metadata = {
'name': file_name,
'mimeType': file['mimeType']
}
media = MediaFileUpload(PATH_FOLDER + file_name, mimetype=file['mimeType'], resumable=True)
drive.files().update(
fileId=file_id,
body=file_metadata,
media_body=media,
).execute()
When I try to upload certain file type (pkl, csv):
upload_file('universe.pkl')
upload_file('list.csv')
the code runs without any error but the files do not change in my drive
whereas when I apply the same function for a different type (xlsx, txt):
upload_file('info.xlsx')
upload_file('test.txt')
it works fine and my files get updated. Did anyone face the same problem? and if so what was the solution?
Thanks.
In the following line of your upload_file function you have hard-coded the mime-type:
media = MediaFileUpload(PATH_FOLDER + file_name, mimetype='text/csv', resumable=True)
Make sure that the mime-type you specify on file-metadata equals the one in media

Cannot Interact with Shared Drives & Files | Python Google Drive API v3

I'm working on a python script that would manage a file on a shared drive using Google Drive API v3. However, when I try to download or replace that file, I get the following error:
An error occurred: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v3/files/1HhCxshcR17I4rM0gtBIvHzEH_jzd7nV2?alt=json&uploadType=multipart returned "File not found: 1HhCxshcR17I4rM0gtBIvHzEH_jzd7nV2.">
Here is my code
def update_file(service, file_id, new_title, new_description, new_mime_type,
new_filename):
"""Update an existing file's metadata and content.
Args:
service: Drive API service instance.
file_id: ID of the file to update.
new_title: New title for the file.
new_description: New description for the file.
new_mime_type: New MIME type for the file.
new_filename: Filename of the new content to upload.
new_revision: Whether or not to create a new revision for this file.
Returns:
Updated file metadata if successful, None otherwise.
"""
try:
# File metadata
file_metadata = {
'name': new_title,
'description': new_description
}
# File's new content.
media_body = MediaFileUpload(
new_filename, mimetype=new_mime_type) # In this case the file is small so no resumable flag needed
# Send the request to the API.
updated_file = service.files().update(
fileId=file_id,
body=file_metadata,
media_body=media_body).execute()
return updated_file
except Exception as e:
print ('An error occurred: %s' % e)
return None
#Update an existing entry in database
def updateItem(dataIndex):
userInput = input(header[2]) #Update quantity
if userInput[0] == '+':
ws['C' + str(dataIndex)] = str( int(ws['C' + str(dataIndex)].value) + int(userInput[1:]) )
elif userInput[0] == '-':
ws['C' + str(dataIndex)] = str( int(ws['C' + str(dataIndex)].value) - int(userInput[1:]) )
else:
ws['C' + str(dataIndex)] = str( userInput )
ws['D' + str(dataIndex)] = today #Update date
wb.save(filepath)
How do I use Google Drive API on a shared drive or document? This works fine in my personal drive.
If you want to use the file of file_id in your shared Drive with files().update() in Drive API v3, how about the following modification?
Modified script:
updated_file = service.files().update(
fileId=file_id,
body=file_metadata,
media_body=media_body,
supportsAllDrives=True # <--- Added
).execute()
The default value of supportsAllDrives is false. So in this case, true is used for using the file in the shared Drive.
Reference:
Files: update

Google Picker and download the selected file

I have managed to integrate the google Picker. Now I have the FileId and the access_token. I need to download the selected file in my back end which is in Python.
I have followed the documentation on google developers but If i use the python library then i have to authenticate the user again which is not suitable in my case.
Please enlighten me if I can download the file with any format.
Thanks in advance.
This is a very simple sample script for downloading files using access token and file id without Google library. And this sample supposes files (images and videos) except for Google Docs, as you said. The flow is as follows.
Flow :
Retrieve filename and mimeType.
Create filename from retrieved filename and mimeType. If the filename doesn't have the extension on Google Drive, this script adds the extension to the filename using mimeType and save it. If the filename has the extension on Google Drive, this script uses the original filename and save it.
Download a file and save it as the created filename. If you want to save the file to the specific directory, please set it by yourself.
Sample script :
import mimetypes
import os.path
import requests
accessToken = "### access token ###"
fileId = "### file id ###"
fileInf = requests.get(
"https://www.googleapis.com/drive/v3/files/" + fileId,
headers={"Authorization": "Bearer " + accessToken},
)
filename = fileInf.json()["name"]
temp, ext = os.path.splitext(filename)
filename = filename if ext != "" else filename + mimetypes.guess_extension(fileInf.json()["mimeType"])
r = requests.get(
"https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media",
headers={"Authorization": "Bearer " + accessToken},
)
with open(filename, "wb") as f:
f.write(r.content)
If this was not helpful for you, I'm sorry.

Django - permission denied for text file created on the fly

I am trying to make a downloadable text file on the fly, i think i have achieved this but when i run the code i get a permission denied error.
also when i open this text file, does it get created anywhere in the file system? as i dont want to store these files, just create them and have them downloaded to users machine
IOError at /networks/configs/STR-RTR-01/7
[Errno 13] Permission denied: u'STR-CARD-RTR-01.txt'
config:
def configs(request, device, site_id):
site = get_object_or_404(ShowroomConfigData, pk=site_id)
config_template = get_object_or_404(ConfigTemplates, device_name=device)
file_name = device[:4] + site.location.upper()[:4] + "-" + device[4:] + ".txt"
device_config = None
with open(file_name, 'w') as config_file:
device_config = env.from_string(config_template.config)
device_config.stream(
STR = site.location.upper()[:4],
IP = site.subnet,
BGPASNO = site.bgp_as,
LOIP = site.r1_loopback_ip,
Location = site.location,
Date = site.opening_date,
).dump(config_file)
return render(request, file_name, {
})
If the goal is to provide a link where the user can download a file which is automatically generated, there's no need to write anything to disk.
You can just build the desired content in a Python string, and use the Content-Disposition header to suggest that a user's browser should download the file rather than display it, and its filename parameter to specify a default filename for the user to save the file as.
A slightly simpler example of a view function which does this...
from django.http import HttpResponse
def get_config_file(request):
filename = 'config.txt'
content = 'This is the content of my config file'
content_type = 'text/plain'
content_disposition = 'attachment; filename=%r' % filename
response = HttpResponse(content, content_type=content_type)
response['Content-Disposition'] = content_disposition
return response

python + google drive: upload xlsx, convert to google sheet, get sharable link

The flow of my desired program is:
Upload an xlsx spreadsheet to drive (it was created using pandas to_excel)
Convert it to Google Sheets format
Specify that it is editable by anyone with the link
Get the link and share it with someone who will enter information
Download the completed sheet
I am currently using PyDrive, which solves steps 1 and 5, but there are a few unsolved problems.
How can I convert to google sheets format? I tried to just specify the mimeType as 'application/vnd.google-apps.spreadsheet' when I created the file to upload with PyDrive, but that gave me an error.
How can I set the file to be editable by anyone with the link? Once that is set, I can get the sharing link easily enough with PyDrive.
UPDATE: conversion from xlsx to google sheets is easy with a convert=True flag. See below. I am still seeking a way to set the sharing settings of my new file to "anyone with the link can edit".
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
test_file = drive.CreateFile({'title': 'testfile.xlsx'})
test_file.SetContentFile('testfile.xlsx')
test_file.Upload({'convert': True})
There is an Optional query parameter of "convert", for both the "INSERT" and "COPY" method;
convert=true,
Whether to convert this file to the corresponding Google Docs format. (Default: false)
There is a python example here:
Google Documentation - Copy
You need to use the Python client library for the code to work.
from apiclient import errors
from apiclient.http import MediaFileUpload
# ...
def insert_file(service, title, description, parent_id, mime_type, filename):
"""Insert new file.
Args:
service: Drive API service instance.
title: Title of the file to insert, including the extension.
description: Description of the file to insert.
parent_id: Parent folder's ID.
mime_type: MIME type of the file to insert.
filename: Filename of the file to insert.
Returns:
Inserted file metadata if successful, None otherwise.
"""
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
try:
file = service.files().insert(
body=body,
convert=true,
media_body=media_body).execute()
# Uncomment the following line to print the File ID
# print 'File ID: %s' % file['id']
return file
except errors.HttpError, error:
print 'An error occured: %s' % error
return None
I haven't tried this, so you'll need to test it.
In order to set the file to be editable for anyone with the link , you have to insert a new permission with the following information:
from apiclient import errors
# ...
def share_with_anyone(service, file_id):
"""Shares the file with anyone with the link
Args:
service: Drive API service instance.
file_id: ID of the file to insert permission for.
Returns:
The inserted permission if successful, None otherwise.
"""
new_permission = {
'type': "anyone",
'role': "writer",
'withLink': True
}
try:
return service.permissions().insert(
fileId=file_id, body=new_permission).execute()
except errors.HttpError, error:
print 'An error occurred: %s' % error
return None
then to get the link you go to : file["alternateLink"]

Categories