How Do I Upload Mega.nz
I received some emails request me how to upload a file anonymously using the API, since information technology is possible to upload a file from the Mega.co.nz website without logging in. I actually gave a solution in the comments of this article, but it'southward non very like shooting fish in a barrel to find, so I'm posting it hither
"Bearding" uploads use ephemeral accounts, equally described in the developer's guide:
MEGA supports ephemeral accounts to enable pre-registration file manager operations, allowing applications to present MEGA'due south functionality with a lowered barrier to entry. They naturally do not have an e-mail address associated with them, the password (master primal) is generated randomly, and they are subject to frequent purging. In a browser context, the credentials for ephemeral accounts live in the DOM storage.
Thus, we actually have to log in, but with a freshly generated ephemeral account. Permit'southward see how it works:
def login_anon( ): global sid, master_key master_key = [ random.randint ( 0 , 0xFFFFFFFF ) ] * 4 password_key = [ random.randint ( 0 , 0xFFFFFFFF ) ] * iv session_self_challenge = [ random.randint ( 0 , 0xFFFFFFFF ) ] * 4 user_handle = api_req( { 'a': 'up' , 'k': a32_to_base64(encrypt_key(master_key, password_key) ) , 'ts': base64urlencode(a32_to_str(session_self_challenge) + a32_to_str(encrypt_key(session_self_challenge, master_key) ) ) } ) print "ephemeral user handle: %due south" % user_handle res = api_req( { 'a': 'us' , 'user': user_handle} ) enc_master_key = base64_to_a32(res[ 'k' ] ) master_key = decrypt_key(enc_master_key, password_key) if 'tsid' in res: tsid = base64urldecode(res[ 'tsid' ] ) if a32_to_str(encrypt_key(str_to_a32(tsid[:xvi ] ) , master_key) ) == tsid[-sixteen:]: sid = res[ 'tsid' ]
We randomly generate a principal central, a "countersign key" (equivalent to the hash of a regular user's password) to encrypt to master cardinal, and a session cocky challenge (that will be used to bank check the generated countersign and become the session ID, since our ephemeral account does not have a RSA central pair).
Then, the getfiles() and uploadfile() functions are the aforementioned as in my first article. So, let's upload a file anonymously and become its public URL to share it on the web:
login_anon( ) getfiles( ) uploaded_file = uploadfile( '/home/julienm/mega/test_file.png' ) print getpublicurl(uploaded_file[ 'f' ] [ 0 ] )
We have to call the getfiles() function to get the ID of the root node, to which we are uploading our file. The uploadfile() method is just modified to change the last "print" into a "return" and render informations about the uploaded file.
The getpublicurl() method gets the public handle of the file (that is not the same every bit the "private" handle that you see in the 'h' attribute when listing your files, yous tin can actually enable or disable the public handle for a file, whether you lot want it to be public or non), decrypts its key, and concatenates the two informations to obtain the public URL:
def getpublicurl( file ): public_handle = api_req( { 'a': 'l' , 'n': file [ 'h' ] } ) cardinal = file [ 'grand' ] [ file [ 'k' ].index ( ':' ) + 1:] decrypted_key = a32_to_base64(decrypt_key(base64_to_a32(key) , master_key) ) return "http://mega.co.nz/#!%s!%south" % (public_handle, decrypted_key)
That's it! It works
julienm@rchand:~/mega$ python anon_upload.py ephemeral user handle: 5hq-EIBu_yc http://mega.co.nz/#!V51SVYzY!pMS4P8hyBqFBC3QdhOYNG4xEbJ8Kj8dYQFuxdDt6dMU julienm@rchand:~/mega$
I'm going to mail service part 3 of the MegaFS series very soon, as well every bit a PHP and a Java version of all my examples. Stay tuned
Source: http://julien-marchand.fr/blog/using-the-mega-api-how-to-upload-a-file-anonymously-without-logging-in/
Posted by: lopezproffecanded.blogspot.com
0 Response to "How Do I Upload Mega.nz"
Post a Comment