Hi everyone,
I am facing some issues with downloading file from URL, which might seem as repeatable question and trivial. However, I tried the suggested solutions from previous posts and I haven’t managed to make them work for me. My data is hosted in virtual drive (dropbox) and I managed to retrieve the link for the hosted files. But, when I try to convert the URL to media object, I receive an error saying can’t connect to dropbox. For this particular case, I tried the following code:
my_media_obj2=anvil.URLMedia(‘url generated from dropbox where file is located’)
anvil.media.download(my_media_obj2)
I also trierd to convert the url to Blobmedia:
my_media_obj2=anvil.URLMedia('url')
new = anvil.BlobMedia('application/octet-stream',my_media_obj2.get_bytes(),name='new_name.file')
anvil.media.download(new)
I Tried the requests approach as well as using dropbox api method in downloading files, by creating a function inside anvil server. However, both attempts were also unsuccessful. Even though when I tried them both using Jupiter, I was able to download the file with no issue.
using requests:
def download_file(url,filename=''):
try:
if filename:
pass
else:
filename=req.url[downloadurl.rfind('/')+1:]
with requests.get(url) as req:
with open(filename, 'wb') as f:
for chunk in req.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
return filename
except Exception as e:
print(e)
return None
Dropbox api method to downlaod file:
import dropbox
dbx = dropbox.Dropbox("access token")
with open("test_experimet1.xlsx", "wb") as f:
metadata, res = dbx.files_download(path="/Mariner_cost.xlsx")
f.write(res.content)
Any ideas on how to overcome this issue?
Many thanks.