Forums

Problem of uploading a file with mega.py

Hello, when i try to upload a file on my mega, i got this error mega.errors.RequestError: EARGS, You have passed invalid arguments to this command. But strangely, on my PC , the uploading from my PC to mega work well. mega.py use g.api.mega.co.nz and it's present in the whitelist. NB: i use a free account. Please help me.

Is that the full error traceback? Are you using the same version of package/Python on your local machine?

I use the same version.

2021-08-27 14:11:53,773: Internal Server Error: /api/moderator/media/add Traceback (most recent call last): File "/home/pythonbrad/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home/pythonbrad/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/pythonbrad/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 124, in _get_response response = wrapped_callback(request, callback_args, callback_kwargs) File "/home/pythonbrad/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(args, kwargs) File "/home/pythonbrad/student_agenda/backend/api/views/moderator.py", line 68, in add_media_view mega_file.write(data) File "/home/pythonbrad/student_agenda/backend/api/views/tools.py", line 50, in write cloud_file = self.mega_api.upload(self.tmp_folder+'/'+filename, self.cloud_folder) File "/home/pythonbrad/venv/lib/python3.8/site-packages/mega/mega.py", line 832, in upload data = self._api_request({ File "/home/pythonbrad/venv/lib/python3.8/site-packages/tenacity/init.py", line 293, in wrapped_f return self.call(f, *args, kw) File "/home/pythonbrad/venv/lib/python3.8/site-packages/tenacity/init.py", line 359, in call do = self.iter(retry_state=retry_state) File "/home/pythonbrad/venv/lib/python3.8/site-packages/tenacity/init.py", line 320, in iter return fut.result() File "/usr/lib/python3.8/concurrent/futures/_base.py", line 432, in result return self.get_result() File "/usr/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result raise self._exception File "/home/pythonbrad/venv/lib/python3.8/site-packages/tenacity/__init.py", line 362, in call result = fn(args, *kwargs) File "/home/pythonbrad/venv/lib/python3.8/site-packages/mega/mega.py", line 188, in _api_request raise RequestError(int_resp) mega.errors.RequestError: EARGS, You have passed invalid arguments to this command

From the traceback, I would guess that either self.tmp_folder or self.cloud_folder do not exist. Check that both of the directories that you're passing in actually exist.

the tmp_folder is /tmp on my pythonanywhere the clould_folder is a folder on my mega

Both folder exists.

It looks like there's some sort of API request. If that is to a service that is not on our whitelist, then you will not be able to reach it from a free account. If it is on the whitelist, you may need to configure the library that you're using to use a proxy - check the documentation for the library to find out how to do that. The details you need to use are here

OK Thanks.

I have set this proxy http://proxy.server:3128 but i have the same error.

I try to do a direct connection with requests and i got this error with proxy and without proxy.

import requests as r

r.get("https://g.api.mega.co.nz", proxies={"http": "http://proxy.server:3128", "https": "http://proxy.server:3128"})

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 594, in urlopen self._prepare_proxy(conn) File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy conn.connect() File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 308, in connect self._tunnel() File "/usr/lib/python3.8/http/client.py", line 898, in _tunnel raise OSError("Tunnel connection failed: %d %s" % (code, OSError: Tunnel connection failed: 403 Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 637, in urlopen retries = retries.increment(method, url, error=e, _pool=self, File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mega.nz', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.8/site-packages/requests/api.py", line 75, in get return request('get', url, params=params, kwargs) File "/usr/lib/python3.8/site-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, kwargs) File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, send_kwargs) File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 668, in send history = [resp for resp in gen] if allow_redirects else [] File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 668, in <listcomp> history = [resp for resp in gen] if allow_redirects else [] File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 239, in resolve_redirects resp = self.send( File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, kwargs) File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='mega.nz', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

g.api.mega.co.nz is not on our whitelist. Send us a link to the API documentation for the site and we'll consider it for the whitelist.

https://mega.nz/doc

ok, it's already whitelisted.

thanks, but i have always the same problem.

Do you try to connect to g.api.mega.co.nz or just mega.nz?

g.api.mega.co.nz

Ok. I have added that to the whitelist..

import requests as r

r.get("https://g.api.mega.co.nz")

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 594, in urlopen self._prepare_proxy(conn) File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy conn.connect() File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 308, in connect self._tunnel() File "/usr/lib/python3.8/http/client.py", line 898, in _tunnel raise OSError("Tunnel connection failed: %d %s" % (code, OSError: Tunnel connection failed: 403 Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 637, in urlopen retries = retries.increment(method, url, error=e, _pool=self, File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py", line 399, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mega.nz', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.8/site-packages/requests/api.py", line 75, in get return request('get', url, params=params, kwargs) File "/usr/lib/python3.8/site-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, kwargs) File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, send_kwargs) File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 668, in send history = [resp for resp in gen] if allow_redirects else [] File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 668, in <listcomp> history = [resp for resp in gen] if allow_redirects else [] File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 239, in resolve_redirects resp = self.send( File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, kwargs) File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='mega.nz', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 403 Forbidden')))

Try again now.

Hello, i have tested, it's work with requests.get("https://g.api.mega.co.nz");

But i have always an error with mega.py.

I have analysed the code of mega.py and i have got it.

1- To upload a file on mega, g.api.mega.co.nz return a URL after the instructions

ul_url = self._api_request({'a': 'u', 's': file_size})['p']

2- The url look like http://xxxxxxx.userstorage.mega.co.nz/ul/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

3- While the uploading, Pythonanywhere return Access to arbitrary websites is not available from free accounts; you can only access sites that are on our whitelist

Please it's possible to add this kind of url: http://*.userstorage.mega.co.nz

ok, we whitelisted that

Thanks very much, It's work now.

Glad to hear that!