Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload files through Python Wrapper around Mega api

Tags:

python-2.7

I'm using python 2.7 Wrapper of Mega Api on Ubuntu. With the following code I could upload a set of files to my Mega Account, however I'd like to know how I can upload files to a specific folder on my account.

from mega import Mega
import os
from os import listdir
from os.path import isfile, join

mega = Mega()
mega._login_user('email','password')

def absoluteFilePaths(directory):
   for dirpath,_,filenames in os.walk(directory):
       for f in filenames:
           yield os.path.abspath(os.path.join(dirpath, f))

directory = '/home/caioignm/test_folder'
file_path_generator = absoluteFilePaths(directory)

for file_path in file_path_generator:
    mega.upload(file_path)

On my Mega Account, I have one folder, 'Personal', with other folder inside, 'Vacations', where all my files are stored. When I upload without refering a destination path, the files are stored in root directory.

I tried to follow help instructions of mega package, but I didn't find out how to set destination folder of my files.

Help on method upload in module mega.mega:

upload(self, filename, dest=None, dest_filename=None) method of mega.mega.Mega instance
    ##########################################################################
    # UPLOAD

Doing mega.upload('filename', 'Personal/Vacations') I haven't received any error message, but my files were not uploaded too

like image 341
Kfcaio Avatar asked Sep 18 '26 10:09

Kfcaio


1 Answers

It was so easy, like this:

Folder = mega.find('my_mega_folder')
mega.upload('yourfile.txt', Folder[0])

I have added it to your code:

from mega import Mega
import os
from os import listdir
from os.path import isfile, join

mega = Mega()
mega._login_user('email','password')

def absoluteFilePaths(directory):
     for dirpath,_,filenames in os.walk(directory):
       for f in filenames:
           yield os.path.abspath(os.path.join(dirpath, f))

directory = '/home/caioignm/test_folder'
file_path_generator = absoluteFilePaths(directory)

Folder = mega.find('your_folder') #change it with the folder in your mega
for file_path in file_path_generator:
    mega.upload(file_path, Folder[0])
like image 56
Pausi Avatar answered Sep 21 '26 01:09

Pausi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!