2015-11-19 62 views
4

我使用以下格式上傳的圖片文件:爲什麼我的S3上傳無法正確上傳?

var body = fs.createReadStream(tempPath).pipe(zlib.createGzip()); 
var s3obj = new AWS.S3({params: {Bucket: myBucket, Key: myKey}}); 
var params = { 
    Body: body, 
    ACL: 'public-read', 
    ContentType: 'image/png' 
}; 

s3obj.upload(params, function(err, data) { 
    if (err) console.log("An error occurred with S3 fig upload: ", err); 
    console.log("Uploaded the image file at: ", data.Location); 
}); 

圖像成功上傳到我的S3桶(有沒有錯誤消息,我看到它在S3遊戲機),但是當我嘗試顯示它在我的網站上,它返回一個破碎的img圖標。當我使用S3控制檯文件下載器下載圖像時,我無法打開文件「損壞或損壞」的錯誤。

如果我使用S3控制檯手動上傳文件,我可以在我的網站上正確顯示它,所以我很確定我上傳的方式有問題。

什麼問題?

回答

6

我終於找到了答案,我的問題。我需要發佈一個參數,因爲該文件是gzip'd(使用var body = ... zlib.createGzip())。這解決了我的問題:

var params = { 
    Body: body, 
    ACL: 'public-read', 
    ContentType: 'image/png', 
    ContentEncoding: 'gzip' 
}; 
0

即使世界一個非常好的節點模塊s3-upload-stream上傳(和第一壓縮)圖像S3,這裏是他們的示例代碼這是非常有據可查:

var AWS  = require('aws-sdk'), 
    zlib  = require('zlib'), 
    fs  = require('fs'); 
    s3Stream = require('s3-upload-stream')(new AWS.S3()), 

// Set the client to be used for the upload. 
AWS.config.loadFromPath('./config.json'); 
// or do AWS.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'}); 

// Create the streams 
var read = fs.createReadStream('/path/to/a/file'); 
var compress = zlib.createGzip(); 
var upload = s3Stream.upload({ 
    "Bucket": "bucket-name", 
    "Key": "key-name" 
}); 

// Optional configuration 
upload.maxPartSize(20971520); // 20 MB 
upload.concurrentParts(5); 

// Handle errors. 
upload.on('error', function (error) { 
    console.log(error); 
}); 

/* Handle progress. Example details object: 
    { ETag: '"f9ef956c83756a80ad62f54ae5e7d34b"', 
    PartNumber: 5, 
    receivedSize: 29671068, 
    uploadedSize: 29671068 } 
*/ 
upload.on('part', function (details) { 
    console.log(details); 
}); 

/* Handle upload completion. Example details object: 
    { Location: 'https://bucketName.s3.amazonaws.com/filename.ext', 
    Bucket: 'bucketName', 
    Key: 'filename.ext', 
    ETag: '"bf2acbedf84207d696c8da7dbb205b9f-5"' } 
*/ 
upload.on('uploaded', function (details) { 
    console.log(details); 
}); 

// Pipe the incoming filestream through compression, and up to S3. 
read.pipe(compress).pipe(upload); 
相關問題