2017-08-17 73 views
1

我有這個奇怪的問題與AWS S3工作時。我正在研究可以將圖像存儲到AWS存儲桶的應用程序。使用Multer作爲中間件和S3FS庫來連接並上傳到AWS。MalformedXML:您提供的XML沒有得到很好的形成或沒有驗證對我們發佈的架構

但下面的錯誤彈出,當我嘗試上傳的內容。

「MalformedXML:您提供的XML沒有得到很好的形成或沒有驗證對我們敬上 HED模式」

Index.js

var express = require('express'); 
var router = express(); 
var multer = require('multer'); 
var fs = require('fs'); 
var S3FS = require('s3fs'); 
var upload = multer({ 
    dest: 'uploads' 
}) 
var S3fsImpl = new S3FS('bucket-name', { 
    region: 'us-east-1', 
    accessKeyId: 'XXXXXXXXXXXX', 
    secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' 
}); 

/* GET home page. */ 
router.get('/', function (req, res, next) { 
    res.render('profile', { 
    title: 'Express' 
    }); 
}); 

router.post('/testupload', upload.single('file'), function (req, res) { 
    var file = req.file; 
    console.log(file); 

    var path = req.file.path; 
    var stream = fs.createReadStream(path); 
    console.log(stream); 

    S3fsImpl.writeFile(file.name, stream).then(function() { 
    fs.unlink(file.path, function (err) { 
     if (err) { 
     console.log(err); 
     } 
    }); 
    res.redirect('/profile'); 
    }) 
}); 

module.exports = router; 

編輯 輸出:

{ fieldname: 'file', 
    originalname: '441_1.docx', 
    encoding: '7bit', 
    mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
    destination: 'uploads', 
    filename: '662dcbe544804e4f50dfef1f52b40d22', 
    path: 'uploads\\662dcbe544804e4f50dfef1f52b40d22', 
    size: 13938 } 
ReadStream { 
    _readableState: 
    ReadableState { 
    objectMode: false, 
    highWaterMark: 65536, 
    buffer: BufferList { head: null, tail: null, length: 0 }, 
    length: 0, 
    pipes: null, 
    pipesCount: 0, 
    flowing: null, 
    ended: false, 
    endEmitted: false, 
    reading: false, 
    sync: true, 
    needReadable: false, 
    emittedReadable: false, 
    readableListening: false, 
    resumeScheduled: false, 
    defaultEncoding: 'utf8', 
    ranOut: false, 
    awaitDrain: 0, 
    readingMore: false, 
    decoder: null, 
    encoding: null }, 
    readable: true, 
    domain: null, 
    _events: { end: [Function] }, 
    _eventsCount: 1, 
    _maxListeners: undefined, 
    path: 'uploads\\662dcbe544804e4f50dfef1f52b40d22', 
    fd: null, 
    flags: 'r', 
    mode: 438, 
    start: undefined, 
    end: undefined, 
    autoClose: true, 
    pos: undefined, 
    bytesRead: 0 } 

的package.json

{ 
    "name": "aws-s3-images", 
    "version": "1.0.0", 
    "private": true, 
    "scripts": { 
    "start": "node ./bin/www" 
    }, 
    "dependencies": { 
    "body-parser": "~1.17.1", 
    "connect-multiparty": "^2.0.0", 
    "cookie-parser": "~1.4.3", 
    "debug": "~2.6.3", 
    "express": "~4.15.2", 
    "hbs": "~4.0.1", 
    "morgan": "~1.8.1", 
    "multer": "^1.3.0", 
    "s3fs": "^2.5.0", 
    "serve-favicon": "~2.4.2" 
    }, 
    "description": "AWS S3 uploading images", 
    "main": "app.js", 
    "devDependencies": {}, 
    "keywords": [ 
    "javascript" 
    ], 
    "author": "reeversedev", 
    "license": "MIT" 
} 
+0

你可以包括你在提問上傳XML? – guest271314

+0

你能告訴我在哪裏可以找到它嗎? @ guest271314 –

+0

問題不在於您的XML,而在於正確設置您的AWS對象。你有沒有正確輸入你的水桶名稱和其他參數? –

回答

1

此代碼應爲你工作。你需要記住的是: 1)你的文件對象的使用,而不是「名稱」 <「ORIGINALNAME」下使用唯一的水桶名 2) - 這個屬性不存在

app.post('/testupload', function(req, res){ 


    var file = req.files[0]; 

    console.log(file.path); 
    console.log(file.name); 

    console.log('FIRST TEST: ' + JSON.stringify(file)); 

    var stream = fs.createReadStream(file.path);  

    S3fsImpl.writeFile(file.originalname, stream).then(function() 
     { 
     console.log('File has been sent - OK'); 
     }, 
     function(reason) 
     { 
      throw reason; 
     } 
    ); 

    res.redirect('/index'); 

}); 
+0

您是否在系統中測試過這些代碼? –

+0

是的。我在Windows 7上進行了測試。我還在AWS上創建了免費帳戶。 –

+0

你可以發送給我你的代碼與最小的文件列表壓縮到一個包?我會在我身邊測試它。 –

0

你可以試試這個代碼:

var S3fsImpl = new S3FS('bucket-name', { 
    region: 'us-east-1', 
    accessKeyId: 'XXXXXXXXXXXX', 
    secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' 
}); 

var fsImplStyles = S3fsImpl.getPath(file.name); 

// Change us-east-1 for your region 
var url = 'https://s3-us-east-1.amazonaws.com/' + fsImplStyles; 

如果此代碼適用於您,請發送反饋意見。

+0

昨天我做了同樣的事情。當我嘗試將圖像提交給AWS時,它不會上傳,而是我正在使用的密鑰會自動刪除,並且正在創建一個新密鑰。 –

相關問題