Hi, I have an app that use CKeditor who send images to the server and the
function who catch this file is more or less like this:
Server TWO
def ImgUpload():
a = stuffs.Stuffs() #Custom library
name = a.password(20)
rand_name = a.password(20)
#path where the files will be saved
path_img = '/some/folder/here/'
#save the data of the image in db (if you need it)
db.images.insert(project_id=session.project_id, path=path_img,
rand_name=rand_name, name=str(name)+'.png')
#save the file uploaded in disk
open(path_img+str(name)+'.png',
'wb').write(request.vars['upload'].file.read())
in the server ONE goes the form that send the file to the server two.
this example does not contain any token to avoid that anyone can upload
files.
Using sftp is good, but maybe can be a slower due the use of credentials
Cheers.
Chris.
El vie., 18 oct. 2019 a las 1:22, Auden RovelleQuartz (<
[email protected]>) escribió:
> I found a solution
>
> step one: install the Paramiko library on the application server
> (SERVER_ONE)
>
> step two: import the paramiko library
>
> step three: use the "putfo" method
>
> here is an example
>
> def file_upload():
> import paramiko
> form = FORM(
> DIV(
> INPUT(
> _type = "file",
> _name = "file",
> )
> ),
> DIV(
> INPUT(
> _type = "submit",
> _value = "UPLOAD IMAGE"
> )
> ),
> _enctype="multipart/form-data",
> )
> if (form.process().accepted):
> client = paramiko.SSHClient()
> client.load_system_host_keys()
> t = paramiko.Transport("www.server_two_url.com", 22)
> t.connect(username = SERVER_TWO_LOGIN_STRING, password =
> SERVER_TWO_PASSWORD_STRING) # for example:
> t.connect(username = "root", password = "abc123")
> sftp = paramiko.SFTPClient.from_transport(t)
> sftp.putfo(form.vars.file.file, "/root/auden.jpg")
> sftp.close()
> t.close()
> return dict(
> form = form,
> )
>
> THAT WORKS FINE! (on SERVER_TWO server using WinSCP I go to the /root
> folder on the VPS and there is the file)
>
>
>
>
>
>
> On Monday, October 14, 2019 at 8:23:25 AM UTC-5, Auden RovelleQuartz wrote:
>>
>> Please let me know how to do this
>>
>> SERVER_ONE: this is the application server
>> SERVER_TWO: this is the file storage server
>>
>> The objective is to
>>
>> >>> have the user select a file via a form (this is via a Web2py app
>> running on SERVER_ONE)
>> >>> when user clicks the submit button, the file is posted to an API
>> endpoint on SEVER_TWO
>> >>> the web2py app on SERVER_TWO then saves the file to disk
>>
>>
>> Here is my code for SERVER_ONE:
>>
>> def file_upload():
>> form = FORM(
>> DIV(
>> INPUT(
>> _type = "file",
>> _name = "file",
>> requires = IS_LENGTH(MAX_IMAGE_UPLOAD_SIZE, MIN_IMAGE_UPLOAD_SIZE),
>> )
>> ),
>> DIV(
>> INPUT(
>> _type = "submit",
>> _value = "UPLOAD IMAGE"
>> )
>> ),
>> _method = "post",
>> _enctype="multipart/form-data",
>> )
>> if (form.process().accepted):
>> r = requests.post(
>> SERVER_TWO_APP_BASE_URL + "/default/get_file",
>> files={'file': form.vars.file.file.read()}
>> )
>> return dict(
>> form = form,
>> )
>>
>>
>>
>> Here is my code for in default.py controller of SERVER_TWO:
>>
>> import requests
>>
>> def get_file():
>> try:
>> file = request.files['file']
>> file.save('', file.filename)
>> except Exception as e:
>> with open("_probe_20191013_001", "w") as f:
>> f.write(str(e))
>> with open("_probe_20191013_002", "w") as f:
>> f.write("\n\nthis is a test\n\n")
>> f.write(str(request))
>> return dict()
>>
>> ==============================================================
>>
>> The file is not getting saved on SERVER_TWO
>>
>> does anyone have any advice on how to accomplish that?
>>
> --
> Resources:
> - http://web2py.com
> - http://web2py.com/book (Documentation)
> - http://github.com/web2py/web2py (Source code)
> - https://code.google.com/p/web2py/issues/list (Report Issues)
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/web2py/ee5f2127-18b8-4840-a298-a9d806100710%40googlegroups.com
> <https://groups.google.com/d/msgid/web2py/ee5f2127-18b8-4840-a298-a9d806100710%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/CA%2Bs%2BuJuOca-L7KdEWaxf1OgM2OsqbsPATcu5Wo89s80Lxe63FA%40mail.gmail.com.