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.
Related
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()
I'm using Django 1.8 with Python 3.6.
When I use my download function below, it saves the file with a name fixed to local download directory. But I really want to keep the original name.
I can change the browser to open a download manager, but I want to know how to fix this filename to the original one.
def download(request):
path = "test.jpg" # Original filename that I intend to give.
file_path = os.path.join(settings.MEDIA_ROOT,path)
print("file_path :", file_path)
if os.path.exists(file_path):
readFile = open(file_path,"rb")
response = HttpResponse(readFile.read())
response['Content-Disposition'] ='attachment; filename'+os.path.basename(file_path)
response['Content-type'] = 'image/jpg'
return response
When I download the file, it is autosaved with a name 'Download.jpg', which is the browser's default directory name.
You have missed the =in your response['Content-Disposition'].
This one should work as expected:
response['Content-Disposition'] = 'attachment; filename=' + os.path.basename(file_path)
So I'm trying out a program which downloads google street view images. The addresses are located in a .txt file. Every time i try to run the code, the HTTP Error 403: Forbidden comes up. In my actual code, i use my Google Developer API of course, and the right file paths.
I've tried to set up a user agent, but it just doesn't work. Can anyone help me, what should i do? And how do i implement it in this code?
# import os and urllib modules
# os for file path creation
# urllib for accessing web content
import urllib
import os
import requests
# this is the first part of the streetview, url up to the address, this url will return a 600x600px image
pre = "https://maps.googleapis.com/maps/api/streetview?size=600x600&location="
# this is the second part of the streetview url, the text variable below, includes the path to a text file containing one address per line
# the addresses in this text file will complete the URL needed to return a streetview image and provide the filename of each streetview image
text = r"C:\Users\.............
# this is the third part of the url, needed after the address
# this is my API key, please replace the one below with your own (google 'google streetview api key'), thanks!
suf = "&key=abcdertghjhrtrwhgrh"
# this is the directory that will store the streetview images
# this directory will be created if not present
dir = r"C:\Users\..........
headers = {'User-Agent': 'Chrome/75.0.3770.100 Safari/537.36',
'From': 'asdasd#asd.com'
}
# checks if the dir variable (output path) above exists and creates it if it does not
if not os.path.exists(dir):
os.makedirs(dir)
# opens the address list text file (from the 'text' variable defined above) in read mode ("r")
with open(text, "r") as text_file:
# the variable 'lines' below creates a list of each address line in the source 'text' file
lines = [line.rstrip('\n') for line in open(text)]
print
"THE CONTENTS OF THE TEXT FILE:\n" + str(lines)
# start a loop through the 'lines' list
for line in lines:
# string clean-up to get rid of commas in the url and filename
ln = line.replace(",", "")
print
"CLEANED UP ADDRESS LINE:\n" + ln
# creates the url that will be passed to the url reader, this creates the full, valid, url that will return a google streetview image for each address in the address text file
URL = pre + ln + suf
response = requests.get(URL, headers = headers)
"URL FOR STREETVIEW IMAGE:\n" + URL
# creates the filename needed to save each address's streetview image locally
filename = os.path.join(dir, ln + ".jpg")
print
"OUTPUT FILENAME:\n" + filename
# you can run this up to this line in the python command line to see what each step does
# final step, fetches and saves the streetview image for each address using the url created in the previous steps
urllib.urlretrieve(URL, filename)
Trying to upload a mp4 file to an Amazon S3 server, it uploads. When downloading it's exactly the same size but bytes 42, 43, 44 changes.
Looking up that's mdat. http://www.file-recovery.com/mp4-signature-format.htm
I tried changing mime types to various ones and also even changed extension to exe. No luck..
When trying to playback the video it does not work.
I'm using the boto python framework to do this.
Any ideas?
# Uploads a file to a bucket
def upload_file_to_bucket(self, file_path_on_disk, file_name_in_bucket, bucket_name):
self.remove_file_from_bucket(file_name_in_bucket, bucket_name)
if os.path.exists(file_path_on_disk):
print "Uploading " + file_path_on_disk + " to " + file_name_in_bucket + " in bucket " + bucket_name
bucket = self.get_connection().get_bucket(bucket_name)
k = bucket.new_key(file_name_in_bucket)
k.set_contents_from_filename(file_path_on_disk, headers = {'Content-Type' : 'application/octet-stream', 'Body' : 'data'}) # {'Content-Disposition': 'attachment', 'Content-Type' : 'video/mp4'}) headers = {'Content-Type' : 'application/octet-stream'}
k.set_acl('public-read')
print "Uploaded file to amazon server " + bucket_name
else:
print "File does not exist so cannot upload " + file_path_on_disk
Edit:
Looking further. seems there is a lot more corruption than that. Dodgy Load balancer?
Is it possible there a way to ensure the file is uploaded correctly? At the moment, it's always wrong.
EDIT:
This was due to the file not being fully written before being uploaded if anyone encounters this.
The right content type is video/mp4 you can set it like this from both
data = open('file.mp4', 'rb')
bucket.put_object(Key='file.mp4', Body=data, ContentType='video/mp4')
make sure the type is set correctly after upload in the s3, navigate to your file and check the properties / Metadata. Note that video/mp4 is not in the drop down list of available content type from AWS but you can force it by writing the content you want
I have currently the upload portion of my code working, how would I go about converting this into a program that will download the respective files from the box folder?
This is the upload program:
import requests
import json
#the user acces token
access_token = 'UfUNeHhv4gIxFCn5WEXHgBJwfG8gHT2o'
#the name of the file as you want it to appear in box
dst_filename = 'box_file'
#the actual file path
src_directory = 'C:\Python\cache\\'
#the name of the file to be transferred
src_filename = 'Wildlife.wmv'
#the id of the folder you want to upload to
parent_id = '0'
counter = 1
for counter in range(1, 6):
src_file = (src_directory + src_filename + '-' + str(counter))
print(src_file)
box_filename = (dst_filename + '-' + str(counter))
headers = { 'Authorization': 'Bearer {0}'.format(access_token)}
url = 'https://upload.box.com/api/2.0/files/content'
#open(src_file,'rb') - opens the source file with the buffered reader
files = { 'filename': (box_filename, open(src_file,'rb')) }
data = { "parent_id": parent_id }
response = requests.post(url, data=data, files=files, headers=headers)
#file_info = response.json()
#print(file_info)
print(response)
print(url, data, files, headers)
counter = counter + 1
This is the sample curl request that the Box API documentation gives for downloading files.
curl -L https://api.box.com/2.0/files/FILE_ID/content \
-H "Authorization: Bearer ACCESS_TOKEN" \
-o FILE_PATH/file_name.txt
Part two of this question: Is there a way to alter this program (and the download program) to process all of the files within a folder no matter what the name of the file is?
I am new to programming, so please forgive my lack of skills/knowledge in this area.
Assume you are getting your authorization correct you can download file by adding few lines to code to your Existing code.
This will copy data from box file to local file here name is FileFromBox.xlx
with open('FileFromBox.xls', 'wb') as open_file:
client.file('FileId_of_box_file').download_to(open_file)
open_file.close()
I know this was asked long back, but still I believe many people are searching for the way to do it.
Please check Box SDK for more details.
And I'm using OAuth2.0 - Custom App. You can create the credentials from the developer console.
Here's the code.
from boxsdk import OAuth2, Client
#from boxsdk import Folder
auth = OAuth2(
client_id='fbxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9',
client_secret='bPxxxxxxxxxxxxxxxxxxxxxxxxx4Or',
access_token='QExxxxxxxxxxxxxxxxxxxxxxxxxxwt',
)
client = Client(auth)
root_folder = client.root_folder().get()
items = root_folder.get_items()
for item in items:
print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))
with open(item.name, 'wb') as open_file:
client.file(item.id).download_to(open_file)
open_file.close()
Hope this will help you. Thanks to the Python boxsdk 2.0.0 Doc.
I would suggest you looking at Box SDK
As you can see in their docs, after authenticating with your client you only need to run the following line:
client.file(file_id='SOME_FILE_ID').content()
There is more information in Box SDK Docs. If this does not satisfy your necessities because you want to create your own Box SDK, then please wait for another person to give an specific response to your problem. Thanks.
You can download a file & folders into a zip like below:
name = 'test'
file = mock_client.file('466239504569')
folder = mock_client.folder('466239504580')
items = [file, folder]
output_file = open('test.zip', 'wb')
status = client.download_zip(name, items, output_file)
print('The status of the zip download is {0}'.format(status['state']))