How to Use Mega.nz API With Python?
In this article, we are going to see how to use mega.nz API with Python. MEGA.NZ is End-to-end encrypted and the encryption keys are owned by us. It means that mega.NZ employees won’t be able to read personal data. Mega.py is a great Python module for interacting with mega.nz API. It provides easy to use functions that can be used for functions like –
- login
- uploading
- downloading
- deleting
- searching
- sharing
- renaming
Installing the Module
Mega. NZ provides an easy-to-use SDK for Python and hence we are going to use it for this tutorial. So First we have to install the SDK.
pip install mega.py
Run this above command to install the SDK. Now move to your favorite IDE.
To Upload File or Folder
Here we are going to upload files in our mega.nz cloud. using upload() function.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
filename = "data.csv"
m.upload(filename)
|
Output:
Get Information about your Mega Account
Get user details
get_user() method returns a dictionary containing the data about the user.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
details = m.get_user()
print (details)
|
Output:
Get account disk quota
get_quota() method return the unused disk space.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
quota = m.get_quota()
print ( "Total Space: " , quota)
|
Output:
Download Files From Mega
Here we are going to download files from mega using download_url() methods.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
file = m.find( 'myfile.doc' )
m.download( file )
m.download_url(
'https: / / mega.co.nz /
ipsqwH9c_9ZOfRjZ48Xb5k2I1M6QTMa4')
m.download( file , '/home/john-smith/Desktop' )
|
Output:
Searching File
Searching file requires the use of the find() function, Here if the file is found it will be a dictionary containing various info about the file, else the file will just be a none type
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
filename = ""
file = m.find(filename)
print ( file )
|
Output:
Rename a file or a folder
Here we will rename a file using rename() methods.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
oldFilename = "data.csv"
newFilename = "renamed_data.csv"
file = m.find(oldFilename)
m.rename( file , newFilename)
|
Output:
How to share a public link of the file so that anyone can download?
get_link() method to return the link of the specific file.
Python3
from mega import Mega
mega = Mega()
m = mega.login(email, password)
file = m.find( "data.csv" )
link = m.get_link( file )
print (link)
|
Output:
Last Updated :
22 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...