file upload with original uploaded file name

884 views
Skip to first unread message

Suman Adak

unread,
Jul 9, 2016, 2:42:20 PM7/9/16
to openresty-en
Dear all,

I tried to create a simple file upload api for my mobile client. Mobile client will post some zip file to server. server will save it disk( may be offload to other server for processing). I also wanted to keep the original uploaded file name while I am saving the file to disk. 
I tried something like this. Not able to understand, how I would get the original uploaded file name.

Also would be interested to know other way to upload file. File size is very small like 5kb. But there might be one million mobile client request which will be sending data and accepted request rate will be nearly 500 to 1k/sec

content_by_lua_block {
                 
                 local file
                 local upload_path = '/home/test/uploads/'

ngx.req.read_body()
  local data = ngx.req.get_body_data()
  local source_file_name = ngx.req.get_body_file() -- it gives me only temp file path with some 000001 name not original file name.
  local file_name = ???????  [how to get the original uploaded file name]
  if file_name then
                       file = io.open(file_name, 'w+')
                       if not file then
                           ngx.say("failed to open file ", file_name)
                           return
                       end
              end
              file:write(data)
              file:close()
              ngx.say("file uploaded")
             }

Robert Paprocki

unread,
Jul 9, 2016, 3:39:19 PM7/9/16
to openre...@googlegroups.com
Hi,

On Jul 9, 2016, at 11:42, Suman Adak <suma...@gmail.com> wrote:

Dear all,

I tried to create a simple file upload api for my mobile client. Mobile client will post some zip file to server. server will save it disk( may be offload to other server for processing). I also wanted to keep the original uploaded file name while I am saving the file to disk. 
I tried something like this. Not able to understand, how I would get the original uploaded file name.

Also would be interested to know other way to upload file. File size is very small like 5kb. But there might be one million mobile client request which will be sending data and accepted request rate will be nearly 500 to 1k/sec

If you're reading from the buffered content off the disk, you would have to examine the file contents and examine the multipart headers. Have a look at lua-resty-upload (https://github.com/openresty/lua-resty-upload), which lets you stream multipart uploads and examine each part of the request body, including the name.

Suman Adak

unread,
Jul 11, 2016, 12:11:49 PM7/11/16
to openresty-en


Hi,

thanks for reply, I tried to parse header and got it but it seems bit complex or error prone to get filename from header. can we write more easier way or am i doing wrong?

                      local file_header = res[2]
                      local i, j = string.find(file_header,"filename=")
                      if j then
                      local file_name = string.sub(file_header,j+2,string.len(file_header)-1)
                 ngx.log(ngx.INFO,"file name is = ",file_name)
                      end

Along with that I also need to send uploaded file to rabbitMQ. I am not able to figure out how I would send file to rabbitMQ. Please help me.

Robert Paprocki

unread,
Jul 11, 2016, 12:15:06 PM7/11/16
to openre...@googlegroups.com
Hi,

On Mon, Jul 11, 2016 at 9:11 AM, Suman Adak <suma...@gmail.com> wrote:

thanks for reply, I tried to parse header and got it but it seems bit complex or error prone to get filename from header. can we write more easier way or am i doing wrong?

                      local file_header = res[2]
                      local i, j = string.find(file_header,"filename=")
                      if j then
                      local file_name = string.sub(file_header,j+2,string.len(file_header)-1)
                 ngx.log(ngx.INFO,"file name is = ",file_name)
                      end


Can you please post a full, minimal configuration/code example so we can try to diagnose the problem? Small snippets like these are not useful to a mailing list who has no idea the context of what you're doing.

Along with that I also need to send uploaded file to rabbitMQ. I am not able to figure out how I would send file to rabbitMQ. Please help me.

Have a look at https://github.com/wingify/lua-resty-rabbitmqstomp. I suggest you read through the documentation and contact the author if you have trouble with this module.

Suman Adak

unread,
Jul 11, 2016, 1:02:19 PM7/11/16
to openresty-en, rob...@cryptobells.com
dear rpaprocki,

Sorry for that, here is my full nginx conf file

worker_processes  1;
error_log logs/error.log info;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8083;
        location /upload {
            default_type multipart/form-data;
           
            content_by_lua_block {

                
           local resty_md5 = require "resty.md5"
           local upload = require "resty.upload"
           local cjson = require "cjson"

           local chunk_size = 4096
           local form = upload:new(chunk_size)

           if not form then
                    ngx.log(ngx.ERR, "failed to new upload: ", err)
                    ngx.exit(500)
                end


           local md5 = resty_md5:new()
           local file

           local upload_path = '/home/suman/uploads/'
           
           while true do
               local typ, res, err = form:read()

               if not typ then
                   ngx.say("failed to read: ", err)
                   return
               end

               if typ == "header" then
                  -- local file_name = upload_path .. ngx.time()
                  local file_header = res[2]
                           local i, j = string.find(file_header,"filename=")
                           if j then
                      local file_name = string.sub(file_header,j+2,string.len(file_header)-1)
                  ngx.log(ngx.INFO,"file name is = ",file_name)
                  file_name = upload_path..ngx.time()..'.'..file_name
                   if file_name then
                       file = io.open(file_name, 'w+')
                       if not file then
                           ngx.say("failed to open file ", file_name)
                           return
                       end
               end        
                   end
               elseif typ == "body" then
                   if file then
                       file:write(res)
                       md5:update(res)
                   end
               elseif typ == "part_end" then
                   file:close()
                   file = nil
                   startUpload = false
               elseif typ == "eof" then
                   break
               else
                   -- nothingman 
               end
           end

           local digest = md5:final()
           md5:reset()

           local str = require "resty.string"
           ngx.say("md5: ", str.to_hex(digest))
       }
   }
            
        }
    }

thanks

suman
Reply all
Reply to author
Forward
0 new messages