LearnBoost/knox — Issue #54: Upload success but empty file
Author: thevinci
Created: 2011-12-23 22:12:53+00:00
State: open

Everything uploaded as planned and it showed up in S3 AWS console. I made sure permissions are set. When I double clicked the file, it takes me to nothing. Not sure what I am doing wrong here.

The local upload works.

```coffeescript
if req.method == 'POST'
Knox = require 'knox'
S3 = Knox.createClient
key: CONFIG.amazon.key
secret: CONFIG.amazon.secret
bucket: CONFIG.amazon.s3
path = req.body.path || 'etc'
path = '/'+req.subdomain+'/'+path
files = []
rFiles = []

console.log req.files

_.each req.files, (file, k) -> files.push file
async.forEachLimit files, 6,
((file, cb) -> # Upload and move the files
if acceptableFile.indexOf(file.type) != -1
fullpath = BASEPATH+'/static/uploads'+path
filename = (file.path.split('/')[2]+'.'+file.name.substr(file.name.lastIndexOf('.')+1)).toLowerCase()

### Local copy
mkdir fullpath, (err) -> # recursively create dir
fs.link file.path, fullpath+'/'+filename, (err) ->
if err then console.log err
return
rFiles.push path+'/'+filename
fs.unlink file.path
cb()
return # mkdir
###

console.log file

S3.putFile file.path, path+'/'+file.name, (err, r) ->
if err
log.error "c/user.upload - Error uploading #{file.name} -> #{path+'/'+filename} to S3 - #{err.message}"
console.error err
rFiles.push null
fs.unlink file.path
cb err
return

if r.statusCode == 200
rPath = "https://#{CONFIG.amazon.s3}.s3.amazonaws.com#{path}/#{filename}"
log.info "c/user.upload - #{rPath} uploaded"
rFiles.push rPath
fs.unlink file.path
cb()
else
log.error "c/user.upload - Error uploading #{file.name} -> #{path+'/'+filename} to S3 - #{r.statusCode} error"
rFiles.push null
fs.unlink file.path
cb new Error r.statusCode
return
return),
((err) ->
if err
res.send 'Error uploading file'
return
res.send 'File uploaded'
return)
else
res.render BASEPATH+'/v/public/user/upload',
layout: null
path: req.query.path || 'etc'
next()
```