Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to Use Mega.nz API With Python?

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




# Enter File/Folder Name
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)
  
# FIND FILE
file = m.find('myfile.doc')
  
# THEN DOWNLOAD USING THE FILE OBJECT
m.download(file)
  
# DOWNLOAD FILE USING MEGA FILE URL
m.download_url(
    'https://mega.co.nz/#!3tUF2KQD!Rg-zOOUIs9L\
    ipsqwH9c_9ZOfRjZ48Xb5k2I1M6QTMa4')
  
# SPECIFY DOWNLOAD LOCATION
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




# Get the File
from mega import Mega
  
mega = Mega()
m = mega.login(email, password)
  
file = m.find("data.csv")
  
# Use it in get_link function
link = m.get_link(file)
  
# It will print the link
print(link)


Output:

RELATED ARTICLES

Most Popular

Recent Comments